package com.keyborad.theme.trendyborad.trendyuiactivity;

import android.os.Bundle;

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.keyborad.theme.trendyborad.TrendyApp;
import com.keyborad.theme.trendyborad.R;
import com.keyborad.theme.trendyborad.ad.AdsInsUtil;
import com.keyborad.theme.trendyborad.ad.LoadListener;
import com.keyborad.theme.trendyborad.ad.ShowListener;
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 com.tradplus.ads.base.bean.TPAdInfo;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

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

        // 适配系统状态栏
        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();
    }

    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() {
        AdsInsUtil.INSTANCE.showAd(this, AdsInsUtil.Placement.TOP_ON_AD_TOW, new ShowListener() {
            @Override
            public void onAdShown(@Nullable TPAdInfo ad) {
                loadAd();
            }

            @Override
            public void onAdClosed() {
                loadAd();
            }
        });
        if (getBackPressedCallback() != null) {
            getBackPressedCallback().setEnabled(false);
        }
        getOnBackPressedDispatcher().onBackPressed();
    }

    private void loadAd(){
        AdsInsUtil.INSTANCE.loadAd(this, AdsInsUtil.Placement.TOP_ON_AD_TOW, new LoadListener() {
            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
    }
}