package com.key.vibekeyboard.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.key.vibekeyboard.Adapter.CategoryViewPager2Adapter;
import com.key.vibekeyboard.R;
import com.key.vibekeyboard.Room.Category;
import com.key.vibekeyboard.Utils.Mytool;
import com.key.vibekeyboard.databinding.ActivityCategoryBinding;

import java.util.List;

public class CategoryActivity extends AppCompatActivity {

    private final List<Category> categories = Mytool.parseJsonToList("keyboard.json");
    private ActivityCategoryBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        binding = ActivityCategoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initData();
        initEvent();

    }


    public void initData() {

        CategoryViewPager2Adapter adapter = new CategoryViewPager2Adapter(this, categories);
        binding.categoryViewPager.setAdapter(adapter);


        new TabLayoutMediator(binding.categoryTab, binding.categoryViewPager, (tab, position) -> {

            // 设置自定义视图
            View customView = LayoutInflater.from(this).inflate(R.layout.category_tab_custom, null);
            tab.setCustomView(customView);

            // 设置标题文字
            TextView textView = customView.findViewById(R.id.category_tab_custom_title);
            Category category = categories.get(position);
            String dir = category.getClassName();
            String text = dir.substring(dir.lastIndexOf("_") + 1);
            text = text.substring(0, 1).toUpperCase() + text.substring(1); // 首字母大写
            textView.setText(text);


        }).attach();

// 添加 TabLayout 选中状态监听器
        binding.categoryTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 当 Tab 选中时，将文字设置为红色
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView textView = customView.findViewById(R.id.category_tab_custom_title);
                    textView.setTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_red_dark));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 当 Tab 取消选中时，恢复默认颜色
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView textView = customView.findViewById(R.id.category_tab_custom_title);
                    textView.setTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.black));
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 可选择性处理重复选中的情况
            }
        });

        // 设置第一个 Tab 默认选中并改变颜色
        TabLayout.Tab firstTab = binding.categoryTab.getTabAt(0);
        if (firstTab != null) {
            firstTab.select();  // 手动选中第一个 Tab
            View customView = firstTab.getCustomView();
            if (customView != null) {
                TextView textView = customView.findViewById(R.id.category_tab_custom_title);
                textView.setTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_red_dark));  // 选中时设置为红色
            }

        }
    }

    private void initEvent() {

        binding.categoryBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.categorySearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategoryActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });

    }

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