package com.wall.dream.wallpapers.View;

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

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.wall.dream.wallpapers.acacctivity.WallpaperViewActivity;
import com.wall.dream.wallpapers.databinding.View1MainBinding;
import com.wall.dream.wallpapers.GJ.ComUtilsGJ;
import com.wall.dream.wallpapers.GJ.ItemHelperGJ;
import com.wall.dream.wallpapers.LianXi.OnItemXianClick;
import com.wall.dream.wallpapers.data.DataBase;
import com.wall.dream.wallpapers.ItemPag.ItemListPag;

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

public class ViewWallpaperGrid extends Fragment implements OnItemXianClick {
    private View1MainBinding vb;

    public ViewWallpaperGrid() {
    }

    public static ViewWallpaperGrid newInstance() {
        return new ViewWallpaperGrid();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = View1MainBinding.inflate(getLayoutInflater());
        initWallpaperGrid();
        return vb.getRoot();
    }

    private void initWallpaperGrid() {
        // 获取壁纸数据
        List<DataBase> data = new ArrayList<>();
        // 这里应该从数据源获取壁纸列表
        // data = MyApp.getWallpapers();
        
        vb.loadingPb.setVisibility(View.GONE);
        initGridList(data);
    }

    private void initGridList(List<DataBase> data) {
        // ItemList需要Context, itemHeight, showSpace三个参数
        // 180dp高度适合网格布局
        int itemHeight = (int) (180 * getResources().getDisplayMetrics().density);
        ItemListPag itemListPag = new ItemListPag(requireContext(), itemHeight, false);
        itemListPag.setDataInfo(data);
        itemListPag.setInfoWallpaperListener(this);
        
        // 使用3列网格布局
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 3);
        ItemHelperGJ itemHelperGJ = new ItemHelperGJ(6, 6, 0);
        vb.classRecycler.addItemDecoration(itemHelperGJ);
        vb.classRecycler.setLayoutManager(gridLayoutManager);
        vb.classRecycler.setAdapter(itemListPag);
    }

    @Override
    public void onItemClickAction(DataBase info) {
        Intent intent = new Intent(requireContext(), WallpaperViewActivity.class);
        intent.putExtra(ComUtilsGJ.key_info, info);
        startActivity(intent);
    }

    @Override
    public void onClickMore(String name) {
        // 网格模式下不需要此功能
    }
}

