package com.photo.wallpaper.category.activity;

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.photo.wallpaper.category.MyApp;
import com.photo.wallpaper.category.api.ApiItemClick;
import com.photo.wallpaper.category.bean.Data;
import com.photo.wallpaper.category.databinding.FragmentLayoutMainBinding;
import com.photo.wallpaper.category.itemAdapter.AdHomeParent;
import com.photo.wallpaper.category.utils.CommonHelper;
import com.photo.wallpaper.category.utils.ItemSpace;

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


public class FragHome extends Fragment implements ApiItemClick {




    private FragmentLayoutMainBinding vb;


    public FragHome() {

    }

    public static FragHome newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(CommonHelper.key_index, type);
        FragHome fragment = new FragHome();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragmentLayoutMainBinding.inflate(getLayoutInflater());

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



    private void initTabVp() {

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

    }


    private void initList(List<String> data) {
        AdHomeParent adHomeParent = new AdHomeParent();
        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(), ActivityApply.class);
        intent.putExtra(CommonHelper.key_info, info);
        startActivity(intent);
    }

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