package com.app.wallpaper.myfra;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.app.wallpaper.R;
import com.app.wallpaper.databinding.FragmentContentBinding;
import com.app.wallpaper.getdata.GetWalls;
import com.app.wallpaper.getdata.SetData;
import com.app.wallpaper.myOnitem.OnItemHome;
import com.app.wallpaper.myOnitem.OnLoaditem;
import com.app.wallpaper.myact.ViewActivity;
import com.app.wallpaper.myada.LoadDataAdapter;
import com.app.wallpaper.utils.MyItemDecoration;

import java.io.Serializable;
import java.util.List;

public class LoadDataFragment extends Fragment implements OnItemHome, OnLoaditem {
    private FragmentContentBinding binding;
    private static final String ARG_CONTENT = "content";
    private List<GetWalls> urldata;
    private int position;

    public static LoadDataFragment newInstance( int position) {
        LoadDataFragment fragment = new LoadDataFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
//            urldata = (List<GetWalls>) getArguments().getSerializable(ARG_CONTENT);
            position = getArguments().getInt("position");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {// 替换为你的 Fragment 布局文件
        binding = FragmentContentBinding.inflate(getLayoutInflater());
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        binding.contentRe.setLayoutManager(staggeredGridLayoutManager);
        LoadDataAdapter loadDataAdapter = new LoadDataAdapter(SetData.getCataDatas(), getContext(), position, SetData.getDataUrls());
        binding.contentRe.addItemDecoration(new MyItemDecoration(SetData.getContext(), 20, 20, 15));
        loadDataAdapter.setInfoWallPaperListener((OnItemHome) this, (OnLoaditem) this);
        binding.contentRe.setAdapter(loadDataAdapter);
        return binding.getRoot();
    }

    @Override
    public void OnitemClick(int position) {
        Intent intent = new Intent(getContext(), ViewActivity.class);
        intent.putExtra("position", position);
        startActivity(intent);
    }

    @Override
    public void OnitemClick(int cataId, int position) {
        Intent intent = new Intent(getContext(), ViewActivity.class);
        intent.putExtra("cataId", cataId);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}