package com.app.input.breeze.board.uiactivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

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 androidx.recyclerview.widget.GridLayoutManager;

import com.ad.tradpluslibrary.TPAdManager;
import com.app.input.breeze.board.App;
import com.app.input.breeze.board.R;
import com.app.input.breeze.board.bean.CategoryWrapper;
import com.app.input.breeze.board.databinding.CategoryScreenBinding;
import com.app.input.breeze.board.listutils.CategoryCardAdapter;

public class CategoryScreen extends AppCompatActivity {

    private CategoryScreenBinding vb;
    private CategoryCardAdapter categoryCardAdapter;

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

        init();
    }

    private void init() {
        setupRecyclerView();
        setupClickListeners();
    }

    private void setupRecyclerView() {
        // 使用 GridLayoutManager，一行 2 个
        categoryCardAdapter = new CategoryCardAdapter(this, App.list, new CategoryCardAdapter.OnCategoryCardClick() {
            @Override
            public void onCategoryClick(String categoryName) {
                // 点击分类卡片，跳转到分类详情页
                Intent intent = new Intent(CategoryScreen.this, ThemeListActivity.class);
                intent.putExtra(ThemeListActivity.KEY_NAME, categoryName);
                startActivity(intent);
            }
        });
        
        vb.categoryRecycler.setLayoutManager(new GridLayoutManager(this, 2));
        vb.categoryRecycler.setAdapter(categoryCardAdapter);
    }

    private void setupClickListeners() {
        setupBottomNavigation();
    }
    
    private void setupBottomNavigation() {
        // 设置默认选中分类
        updateBottomNavState(R.id.nav_category);

        // 主页点击
        View navHome = findViewById(R.id.nav_home);
        navHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CategoryScreen.this, MainActivity.class));
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }
        });

        // 分类点击
        View navCategory = findViewById(R.id.nav_category);
        navCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 已经在分类页，不需要操作
                updateBottomNavState(R.id.nav_category);
            }
        });

        // 设置点击
        View navSettings = findViewById(R.id.nav_settings);
        navSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CategoryScreen.this, SettingsScreen.class));
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });
    }

    private void updateBottomNavState(int selectedId) {
        int selectedColor = getColor(R.color.blue_primary);
        int unselectedColor = 0x80000000;

        android.widget.ImageView homeIcon = findViewById(R.id.nav_home_icon);
        android.widget.TextView homeText = findViewById(R.id.nav_home_text);
        View homeIndicator = findViewById(R.id.nav_home_indicator);
        android.widget.ImageView categoryIcon = findViewById(R.id.nav_category_icon);
        android.widget.TextView categoryText = findViewById(R.id.nav_category_text);
        View categoryIndicator = findViewById(R.id.nav_category_indicator);
        android.widget.ImageView settingsIcon = findViewById(R.id.nav_settings_icon);
        android.widget.TextView settingsText = findViewById(R.id.nav_settings_text);
        View settingsIndicator = findViewById(R.id.nav_settings_indicator);

        if (selectedId == R.id.nav_home) {
            homeIcon.setColorFilter(selectedColor);
            homeText.setTextColor(selectedColor);
            homeIndicator.setVisibility(View.VISIBLE);
            categoryIcon.setColorFilter(unselectedColor);
            categoryText.setTextColor(unselectedColor);
            categoryIndicator.setVisibility(View.GONE);
            settingsIcon.setColorFilter(unselectedColor);
            settingsText.setTextColor(unselectedColor);
            settingsIndicator.setVisibility(View.GONE);
        } else if (selectedId == R.id.nav_category) {
            homeIcon.setColorFilter(unselectedColor);
            homeText.setTextColor(unselectedColor);
            homeIndicator.setVisibility(View.GONE);
            categoryIcon.setColorFilter(selectedColor);
            categoryText.setTextColor(selectedColor);
            categoryIndicator.setVisibility(View.VISIBLE);
            settingsIcon.setColorFilter(unselectedColor);
            settingsText.setTextColor(unselectedColor);
            settingsIndicator.setVisibility(View.GONE);
        } else if (selectedId == R.id.nav_settings) {
            homeIcon.setColorFilter(unselectedColor);
            homeText.setTextColor(unselectedColor);
            homeIndicator.setVisibility(View.GONE);
            categoryIcon.setColorFilter(unselectedColor);
            categoryText.setTextColor(unselectedColor);
            categoryIndicator.setVisibility(View.GONE);
            settingsIcon.setColorFilter(selectedColor);
            settingsText.setTextColor(selectedColor);
            settingsIndicator.setVisibility(View.VISIBLE);
        }
    }
}