package com.ar.paintar.activity;

import android.content.Intent;
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.ar.paintar.R;
import com.ar.paintar.adapter.MainAdapter;
import com.ar.paintar.databinding.ActivityMainBinding;
import com.ar.paintar.databinding.MainTabCustomBinding;
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);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        EdgeToEdge.enable(this);
        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() {
        binding.setting.setOnClickListener(v -> {
            Intent intent = new Intent(this, SettingActivity.class);
            this.startActivity(intent);
        });

        new TabLayoutMediator(binding.tabLayout, binding.viewpager, (tab, position) -> {
            MainTabCustomBinding tabBinding = MainTabCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabBinding.getRoot());
            setTabIconAndDotVisibility(tabBinding, position);
        }).attach();

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

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

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

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

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

                    int textColor = isSelected ? getResources().getColor(R.color.select, null) : getResources().getColor(R.color.un_select, null);
                    tabBinding.text.setTextColor(textColor);
                }
            }

            private int getIconResource(int position, boolean isSelected) {
                switch (position) {
                    case 1:
                        if (isSelected) {
                            return R.drawable.import_image;
                        }
                        return R.drawable.un_import_image;
                    case 2:
                        if (isSelected) {
                            return R.drawable.collect;
                        }
                        return R.drawable.un_collect;
                    default:
                        if (isSelected) {
                            return R.drawable.category;
                        }
                        return R.drawable.un_category;
                }
            }

        });

    }

    private void setTabIconAndDotVisibility(MainTabCustomBinding tabBinding, int position) {
        switch (position) {
            case 0:
                tabBinding.imageView.setImageResource(R.drawable.category);
                tabBinding.text.setText(R.string.category);
                tabBinding.text.setTextColor(getResources().getColor(R.color.select, null));
                break;
            case 1:
                tabBinding.imageView.setImageResource(R.drawable.un_import_image);
                tabBinding.text.setText(R.string.import_image);
                tabBinding.text.setTextColor(getResources().getColor(R.color.un_select, null));
                break;
            case 2:
                tabBinding.imageView.setImageResource(R.drawable.un_collect);
                tabBinding.text.setText(R.string.collect);
                tabBinding.text.setTextColor(getResources().getColor(R.color.un_select, null));
                break;
        }
    }


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