package com.ar.artraceartpro.ui.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.artraceartpro.topon.AdManager;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.ar.artraceartpro.R;
import com.ar.artraceartpro.databinding.ActivityMainBinding;
import com.ar.artraceartpro.databinding.TabCustomBinding;
import com.ar.artraceartpro.ui.adapter.MainAViewPagerAdapter;

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() {

        MainAViewPagerAdapter adapter = new MainAViewPagerAdapter(this);
        binding.viewpager.setAdapter(adapter);

        AdManager.loadAllAd();

    }

    private void initEvent() {
        binding.viewpager.setUserInputEnabled(false);

        binding.setting.setOnClickListener(v -> {
            Intent intent = new Intent(this, SettingActivity.class);
            this.startActivity(intent);
        });

        new TabLayoutMediator(binding.tabLayout, binding.viewpager, (tab, position) -> {
            TabCustomBinding tabCustomBinding = TabCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabCustomBinding.getRoot());
            setTab(tabCustomBinding, 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) {
                    TabCustomBinding tabBinding = TabCustomBinding.bind(tab.getCustomView());

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

                    int textColor = isSelected ? getResources().getColor(R.color.black, null) : getResources().getColor(R.color.gray, 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.favorite;
                        }
                        return R.drawable.un_favorite;
                    default:
                        if (isSelected) {
                            return R.drawable.category;
                        }
                        return R.drawable.un_category;
                }
            }

        });

    }

    private void setTab(TabCustomBinding tabCustomBinding, int position) {
        switch (position) {
            case 0:
                tabCustomBinding.imageView.setImageResource(R.drawable.category);
                tabCustomBinding.text.setText(R.string.category);
                tabCustomBinding.text.setTextColor(getResources().getColor(R.color.black, null));
                break;
            case 1:
                tabCustomBinding.imageView.setImageResource(R.drawable.un_import_image);
                tabCustomBinding.text.setText(R.string.import_image);
                tabCustomBinding.text.setTextColor(getResources().getColor(R.color.gray, null));
                break;
            case 2:
                tabCustomBinding.imageView.setImageResource(R.drawable.un_favorite);
                tabCustomBinding.text.setText(R.string.favorite);
                tabCustomBinding.text.setTextColor(getResources().getColor(R.color.gray, null));
                break;
        }
    }


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