package com.keyborad.theme.trendyborad.trendyuiactivity;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
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.keyborad.theme.trendyborad.R;
import com.keyborad.theme.trendyborad.TrendyApp;
import com.keyborad.theme.trendyborad.bean.BeanDetails;
import com.keyborad.theme.trendyborad.bean.BeanWrapper;
import com.keyborad.theme.trendyborad.databinding.ActivityCaLtTrendyBinding;
import com.keyborad.theme.trendyborad.trendylistutils.TrendyNewCategoryAdapter;
import com.keyborad.theme.trendyborad.trendyutils.TrendyListDecoration;

import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class TrendyCategoryListActivity extends BaseActivity {
    private ActivityCaLtTrendyBinding vb;
    public static final String KEY_NAME = "class_name";
    private String name;
    private List<BeanDetails> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityCaLtTrendyBinding.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;
        });

        // 获取传递的分类名称
        name = getIntent().getStringExtra(KEY_NAME);
        initData();
        initClick();
        TPAdManager.INSTANCE.showTPAD(TrendyCategoryListActivity.this, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                return null;
            }
        });
    }

    private void initData() {
        // 设置标题
        vb.className.setText(name == null ? "" : name);

        // 获取对应分类的数据
        for (BeanWrapper beanWrapper : TrendyApp.list) {
            if (beanWrapper.getParentName().equals(name)) {
                data = beanWrapper.getKeyboardList();
                break; // 找到后退出循环，提升性能
            }
        }

        // 初始化RecyclerView
        TrendyListDecoration trendyListDecoration = new TrendyListDecoration(3, 3, 0);
        vb.recycler.setLayoutManager(new GridLayoutManager(this, 2)); // 2列布局
        vb.recycler.addItemDecoration(trendyListDecoration);

        // 创建并设置新适配器
        TrendyNewCategoryAdapter newAdapter = new TrendyNewCategoryAdapter(this, data);
        vb.recycler.setAdapter(newAdapter);

        // 可选：设置Item点击事件（根据需求实现）
        newAdapter.setOnItemClickListener((position, item) -> {
            // 此处添加Item点击后的逻辑，比如跳转到键盘详情页/使用页
            // 示例：
            // Intent intent = new Intent(this, KeyboardDetailActivity.class);
            // intent.putExtra("bean", item);
            // startActivity(intent);
        });
    }

    private void initClick() {
        // 返回按钮点击事件
        vb.back.setOnClickListener(v -> onInterceptBackPressed());
    }


    @Override
    protected boolean shouldInterceptBackPress() {
        return true;
    }

    @Override
    protected void onInterceptBackPressed() {

        TPAdManager.INSTANCE.showTPAD(TrendyCategoryListActivity.this, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                return null;
            }
        });
        if (getBackPressedCallback() != null) {
            getBackPressedCallback().setEnabled(false);
        }
        getOnBackPressedDispatcher().onBackPressed();
    }


}