package com.wall.wallpaper.hd.ui_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.wallpaper.hd.WallApp;
import com.wall.wallpaper.hd.adapter.AdapterHome;
import com.wall.wallpaper.hd.databinding.FragMainBinding;
import com.wall.wallpaper.hd.javabean.Data;
import com.wall.wallpaper.hd.listener.ItemClickListener;
import com.wall.wallpaper.hd.tools.CommonUtils;
import com.wall.wallpaper.hd.tools.ItemSpace;

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


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

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



    private void initTabVp() {

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

    }


    private void initList(List<String> data) {
        AdapterHome adHomeParent = new AdapterHome();
        adHomeParent.setData(data);
        adHomeParent.setInfoWallpaperListener(this);
        ItemSpace itemSpace = new ItemSpace(10, 10, 0);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(requireContext());
//        vb.classRecycler.addItemDecoration(itemSpace);
        vb.classRecycler.setLayoutManager(linearLayoutManager);
        vb.classRecycler.setAdapter(adHomeParent);

    }

    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(requireContext(), ActivitySetWallpaper.class);
        intent.putExtra(CommonUtils.key_info, info);
        startActivity(intent);
    }

    @Override
    public void onClickMore(String name) {
        Intent intent = new Intent(requireContext(), ActivityWallList.class);
        intent.putExtra(ActivityWallList.key_index, name);
        startActivity(intent);
    }
}