package com.app.intelligent.board.uiactivity;

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

import com.ad.tradpluslibrary.TPAdManager;
import com.app.intelligent.board.AppContextHelper;
import com.app.intelligent.board.R;
import com.app.intelligent.board.uimodel.KeyboardItemInfo;
import com.app.intelligent.board.uimodel.KeyboardItemDataWrapper;
import com.app.intelligent.board.databinding.ActivityCategoryItemListBinding;
import com.app.intelligent.board.uiadapter.HomeItemSubAdapter;
import com.app.intelligent.board.utils.KeyboardRvItemDecoration;

import java.util.List;

public class CategoryItemListActivity extends AppCompatActivity {
    private ActivityCategoryItemListBinding vb;
    public static final String KEY_NAME ="class_name";
    private String name;
    private List<KeyboardItemInfo> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityCategoryItemListBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(vb.getRoot());
        TPAdManager.INSTANCE.showTPAD(this,()->null);
        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);
        for (KeyboardItemDataWrapper beanWrapper : AppContextHelper.list) {
            if(beanWrapper.getParentName().equals(name)){
                data = beanWrapper.getKeyboardList();
            }
        }
        KeyboardRvItemDecoration listDecoration = new KeyboardRvItemDecoration(15, 15, 0);
        HomeItemSubAdapter adapterMain = new HomeItemSubAdapter(this,data);

        // 核心修改：自定义GridLayoutManager
        vb.recycler.setLayoutManager(new GridLayoutManager(CategoryItemListActivity.this, 1) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                RecyclerView.LayoutParams params = super.generateDefaultLayoutParams();

                int targetHeight = (int) (240 * getResources().getDisplayMetrics().density);
                params.height = targetHeight;
                return params;
            }
        });

        vb.recycler.setAdapter(adapterMain);
        vb.recycler.addItemDecoration(listDecoration);
    }

    private void initClick(){
        vb.back.setOnClickListener(v -> finish());
    }
}