package com.wall.proshot.wallpapers.myacctivity;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wall.proshot.wallpapers.R;
import com.wall.proshot.wallpapers.databinding.ActivityMyListBinding;
import com.wall.proshot.wallpapers.myconnector.OnItemClick;
import com.wall.proshot.wallpapers.myitem.MyItemList;
import com.wall.proshot.wallpapers.bean.Data;
import com.wall.proshot.wallpapers.mytool.MyComUtils;
import com.wall.proshot.wallpapers.mytool.MyObectBoxManager;

import java.util.List;

public class ListActivity extends AppCompatActivity implements OnItemClick {
    private ActivityMyListBinding vb;
    private String categoryName;
    public static final String key_index = "index";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityMyListBinding.inflate(getLayoutInflater());
        setContentView(vb.getRoot());
        MyComUtils.initFull(this, false);

        // 获取分类名称，容错处理
        categoryName = getIntent().getStringExtra(key_index);
        if (categoryName == null || categoryName.isEmpty()) {
            categoryName = getString(R.string.app_name);
        }
        vb.name.setText(categoryName);

        vb.imageviewBack.setOnClickListener(v -> finish());

        initList();
    }

    private void initList() {
        // 查询分类数据，容错处理
        List<Data> data = MyObectBoxManager.queryCategoryAll(categoryName);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return 1; // 每个item占1列（两列布局）
            }
        });

        vb.list.setLayoutManager(gridLayoutManager);
        int spacing = MyComUtils.dp2Px(10); // 间距：10dp转px
        vb.list.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(android.graphics.Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                int column = position % 2; // 0=左列，1=右列

                // 左右间距：左列左边0，右边spacing/2；右列左边spacing/2，右边0
                outRect.left = column == 0 ? 0 : spacing / 2;
                outRect.right = column == 0 ? spacing / 2 : 0;

                // 上下间距：第一行顶部spacing，其余行上下各spacing/2
                if (position < 2) {
                    outRect.top = spacing;
                } else {
                    outRect.top = spacing / 2;
                }
                outRect.bottom = spacing / 2;
            }
        });

        // 3. 设置Adapter（item高度优化，适配两列）
        int itemHeight = MyComUtils.dp2Px(220); // 调整item高度（220dp，更协调）
        MyItemList myItemList = new MyItemList(this, itemHeight, false);
        myItemList.setDataInfo(data);
        myItemList.setInfoWallpaperListener(this);
        vb.list.setAdapter(myItemList);

        // 4. 优化RecyclerView滚动性能
        vb.list.setHasFixedSize(true); // 固定item大小，提升滚动流畅度
        vb.list.setItemViewCacheSize(20); // 增加缓存item数量
    }

    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(this, WallPaperActivity.class);
        intent.putExtra(MyComUtils.key_info, info);
        startActivity(intent);
    }
}