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.LinearLayoutManager;

import com.wall.dream.wallpapers.DreamWallpaper;
import com.wall.dream.wallpapers.acacctivity.ListViewActivity;
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.ItemCategoryPag;

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


public class ViewMain extends Fragment implements OnItemXianClick {
    private View1MainBinding vb;
    public ViewMain() {
    }
    public static ViewMain newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(ComUtilsGJ.key_index, type);
        ViewMain fragment = new ViewMain();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = View1MainBinding.inflate(getLayoutInflater());

        initTabVp();
        return vb.getRoot();
    }



    private void initTabVp() {

        List<String> data = new ArrayList<>();
        while (data.size() < 8) {
            data = DreamWallpaper.getCategoryNames();
        }
        vb.loadingPb.setVisibility(View.GONE);
        initList(data);

    }


    private void initList(List<String> data) {
        // 使用新的宽卡片样式适配器
        ItemCategoryPag categoryAdapter = new ItemCategoryPag();
        categoryAdapter.setData(data);
        categoryAdapter.setOnItemClickListener(this);
        
        // 设置间距装饰器 - 适合宽卡片布局
        ItemHelperGJ itemHelperGJ = new ItemHelperGJ(0, 0, 8);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(requireContext());
        
        vb.classRecycler.setLayoutManager(linearLayoutManager);
        vb.classRecycler.addItemDecoration(itemHelperGJ);
        vb.classRecycler.setAdapter(categoryAdapter);
    }

    @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) {
        Intent intent = new Intent(requireContext(), ListViewActivity.class);
        intent.putExtra(ListViewActivity.key_index, name);
        startActivity(intent);
    }
}