package com.app.input.inputaura.lmyfvfs;

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

import com.ad.toponlibrary.AdManager;
import com.app.input.inputaura.InputAura;
import com.app.input.inputaura.R;
import com.app.input.inputaura.dasda.Shikamaru;
import com.app.input.inputaura.dasda.Inhj;
import com.app.input.inputaura.databinding.AdiasdnadBinding;
import com.app.input.inputaura.onjfsgvfsf.DubTbf;

import java.util.ArrayList;
import java.util.List;

public class YhkFgv extends BaseActivity {
    private AdiasdnadBinding vb;
    public static final String KEY_NAME = "class_name";
    private String name;
    private List<Shikamaru> data = new ArrayList<>();

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

        // 查找对应分类的数据
        for (Inhj inhj : InputAura.list) {
            if (inhj.getParentName().equals(name)) {
                data = inhj.getKeyboardList();
                break;
            }
        }

        // 使用新的适配器
        DubTbf adapterMain = new DubTbf(this, data);

        // 设置两列网格布局
        GridLayoutManager layoutManager = new GridLayoutManager(YhkFgv.this, 2);
        vb.recycler.setLayoutManager(layoutManager);
        vb.recycler.setAdapter(adapterMain);

        // 添加间距装饰器 - 修正：设置为false，因为我们已经在适配器中考虑了间距
        int spacing = getResources().getDimensionPixelSize(R.dimen.grid_spacing);
        vb.recycler.addItemDecoration(new GridSpacingItemDecoration(2, spacing, false));
    }

    private void initClick() {
        vb.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onInterceptBackPressed();
            }
        });
    }

    // 网格间距装饰器
    public static class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(android.graphics.Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view);
            int column = position % spanCount;

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount;
                outRect.right = (column + 1) * spacing / spanCount;

                if (position < spanCount) {
                    outRect.top = spacing;
                }
                outRect.bottom = spacing;
            } else {
                // 修正：只添加列间距，不添加边缘间距
                outRect.left = column * spacing / spanCount;
                outRect.right = spacing - (column + 1) * spacing / spanCount;

                // 添加顶部间距（除第一行外）
                if (position >= spanCount) {
                    outRect.top = spacing;
                }
            }
        }
    }


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

    @Override
    protected void onInterceptBackPressed() {
        AdManager.showTopOn(this, () -> {});
        if (getBackPressedCallback() != null) {
            getBackPressedCallback().setEnabled(false);
        }
        getOnBackPressedDispatcher().onBackPressed();
    }
}