package com.key.vibekeyboard.ui.activity;

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

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

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.key.vibekeyboard.ui.adapter.CategoryViewPager2Adapter;
import com.key.vibekeyboard.R;
import com.key.vibekeyboard.data.bean.Category;
import com.key.vibekeyboard.utils.JsonUtils;
import com.key.vibekeyboard.databinding.ActivityCategoryBinding;

import java.util.List;

public class CategoryActivity extends AppCompatActivity {

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

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

        binding = ActivityCategoryBinding.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();

    }


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

        binding.categoryTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.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) {
                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) {
            }
        });

        TabLayout.Tab firstTab = binding.categoryTab.getTabAt(0);
        if (firstTab != null) {
            firstTab.select();
            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;
    }
}