package com.hd.live.wall.wallpapers.act;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.paging.PagingData;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hd.live.wall.wallpapers.adapter.AdaMain;
import com.hd.live.wall.wallpapers.base.BasisFragment;
import com.hd.live.wall.wallpapers.databinding.FragmentHomeBinding;
import com.hd.live.wall.wallpapers.dbData.WallData;
import com.hd.live.wall.wallpapers.utils.ListDecoration;
import com.hd.live.wall.wallpapers.viewmode.VmMain;

public class FragmentLive extends BasisFragment<FragmentHomeBinding> {


    private static final String ARG_PARAM1 = "param1";


    private int wallpaperType;

    private VmMain vmMain;

    public FragmentLive() {

    }
    public static FragmentLive newInstance(int type) {
        FragmentLive fragment = new FragmentLive();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            wallpaperType = getArguments().getInt(ARG_PARAM1);
        }
    }


    @Override
    protected FragmentHomeBinding getFragmentVb() {
        return FragmentHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        AdaMain adaMain = new AdaMain(requireActivity());
        Vb.recycler.setAdapter(adaMain);
        Vb.recycler.setLayoutManager(new GridLayoutManager(requireContext(),2, RecyclerView.VERTICAL, false));
        Vb.recycler.addItemDecoration(new ListDecoration(requireContext(),3,3,0));
        vmMain = getFragmentScopeViewModel(VmMain.class);
        vmMain.getPagingData(wallpaperType).observe(getViewLifecycleOwner(), new Observer<PagingData<WallData>>() {
            @Override
            public void onChanged(PagingData<WallData> resultDataPagingData) {
                adaMain.submitData(requireActivity().getLifecycle(),resultDataPagingData);
            }
        });

    }
}