package com.ardraw.ardrawinggallery.activity;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ardraw.ardrawinggallery.R;
import com.ardraw.ardrawinggallery.adapter.MainAdapter;
import com.ardraw.ardrawinggallery.databinding.ActivityMainBinding;
import com.ardraw.ardrawinggallery.databinding.MainCustomBinding;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initData();
        initEvent();
    }

    private void initData() {
        MainAdapter adapter = new MainAdapter(this);
        binding.viewpager.setAdapter(adapter);
    }

    private void initEvent() {
        new TabLayoutMediator(binding.tabLayout, binding.viewpager, (tab, position) -> {
            MainCustomBinding mainCustomBinding = MainCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(mainCustomBinding.getRoot());
            setTab(mainCustomBinding, position);
        }).attach();

        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTab(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTab(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {}

            private void updateTab(TabLayout.Tab tab, boolean isSelected) {
                if (tab.getCustomView() != null) {
                    MainCustomBinding tabBinding = MainCustomBinding.bind(tab.getCustomView());

                    int iconResId = getTabIconResource(tab.getPosition(), isSelected);
                    tabBinding.imageView.setImageResource(iconResId);

                    int textColor = isSelected ? getColor(R.color.black) : getColor(R.color.gray);
                    tabBinding.title.setTextColor(textColor);
                }
            }

            private int getTabIconResource(int position, boolean isSelected) {
                int[] selectedIcons = {R.drawable.home, R.drawable.photo, R.drawable.collection};
                int[] unselectedIcons = {R.drawable.un_home, R.drawable.un_photo, R.drawable.un_collection};

                return isSelected ? selectedIcons[position] : unselectedIcons[position];
            }

            private int getColor(int colorResId) {
                return getResources().getColor(colorResId, null);
            }
        });
    }

    private void setTab(MainCustomBinding mainCustomBinding, int position) {
        int iconResId;
        int textColorResId = R.color.gray;
        String titleText;

        switch (position) {
            case 0:
                iconResId = R.drawable.home;
                titleText = "Home";
                textColorResId = R.color.black;
                break;
            case 1:
                iconResId = R.drawable.un_photo;
                titleText = "Import";
                break;
            case 2:
                iconResId = R.drawable.un_collection;
                titleText = "Like";
                break;
            default:
                throw new IllegalArgumentException("Invalid tab position");
        }

        mainCustomBinding.imageView.setImageResource(iconResId);
        mainCustomBinding.title.setText(titleText);
        mainCustomBinding.title.setTextColor(getColor(textColorResId));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}