package com.app.intelligent.board.uiactivity;

import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.app.intelligent.board.R;
import com.app.intelligent.board.databinding.ActivityCategoryMainBinding;
import com.app.intelligent.board.uifragment.FavoritesKeyboardFragment;
import com.app.intelligent.board.uifragment.HomeMainFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;



public class CategoryMainActivity extends AppCompatActivity {

    private ActivityCategoryMainBinding vb;

    // 定义Tab图标资源ID（未选中态）
    private final int[] tabIcons = {R.drawable.home3, R.drawable.favorites3};
    // 定义Tab对应的文字
    private final String[] tabTexts = {"Home", "Favorites"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityCategoryMainBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(vb.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;
        });

        init();
    }

    private void init() {
        List<Fragment> listFragment = new ArrayList<>();
        listFragment.add(HomeMainFragment.newInstance());
        listFragment.add(FavoritesKeyboardFragment.newInstance());

        // 配置TabLayout显示模式
        vb.tabLayout.setTabMode(TabLayout.MODE_FIXED);
        vb.tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        for (int i = 0; i < listFragment.size(); i++) {
            TabLayout.Tab tab = vb.tabLayout.newTab();
            View inflate = LayoutInflater.from(this).inflate(R.layout.layout_tab_custom, null, false);

            ImageView icon = inflate.findViewById(R.id.im_icon);
            TextView textView = inflate.findViewById(R.id.textView);
            textView.setText(tabTexts[i]); // 设置Tab文字

            if (i == 0) {
                // 首页Tab - 默认选中：只显示黑色文字，隐藏图标
                icon.setVisibility(View.GONE);
                textView.setVisibility(View.VISIBLE);
                textView.setTextColor(getResources().getColor(R.color.black));
            } else {
                // 收藏Tab - 默认未选中：只显示灰色图标，隐藏文字
                icon.setImageResource(tabIcons[i]);
                // 给图标设置灰色滤镜（核心：将图标染成灰色）
                icon.setColorFilter(getResources().getColor(R.color.gray), PorterDuff.Mode.SRC_IN);
                icon.setVisibility(View.VISIBLE);
                textView.setVisibility(View.GONE);
            }
            tab.setCustomView(inflate);
            vb.tabLayout.addTab(tab);
        }

        vb.viewpager.setUserInputEnabled(false);
        vb.viewpager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getItemCount() {
                return listFragment.size();
            }
        });
        bindTabEvent();
    }

    private void bindTabEvent() {
        vb.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = Objects.requireNonNull(tab.getCustomView());
                ImageView icon = customView.findViewById(R.id.im_icon);
                TextView textView = customView.findViewById(R.id.textView);

                // 选中状态：隐藏图标，显示黑色文字
                icon.setVisibility(View.GONE);
                textView.setVisibility(View.VISIBLE);
                textView.setTextColor(getResources().getColor(R.color.dark_gray));
                vb.viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = Objects.requireNonNull(tab.getCustomView());
                ImageView icon = customView.findViewById(R.id.im_icon);
                TextView textView = customView.findViewById(R.id.textView);

                // 未选中状态：显示灰色图标，隐藏文字
                icon.setImageResource(tabIcons[tab.getPosition()]);
                // 重置图标颜色为灰色
                icon.setColorFilter(getResources().getColor(R.color.color_gray), PorterDuff.Mode.SRC_IN);
                icon.setVisibility(View.VISIBLE);
                textView.setVisibility(View.GONE);
            }

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

        vb.viewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Objects.requireNonNull(vb.tabLayout.getTabAt(position)).select();
            }
        });
    }
}