package com.newapp.paperwall.uiview;

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

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

import com.newapp.paperwall.BaseApp;
import com.newapp.paperwall.adapter.HomeAdapter;
import com.newapp.paperwall.databinding.FragHomeBinding;
import com.newapp.paperwall.javabean.Data;
import com.newapp.paperwall.listener.OnItemClickListener;
import com.newapp.paperwall.tools.UiUtils;
import com.newapp.paperwall.tools.ItemSpace;

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


public class HomeFragment extends Fragment implements OnItemClickListener {
    private FragHomeBinding vb;
    public HomeFragment() {
    }
    public static HomeFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(UiUtils.key_index, type);
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragHomeBinding.inflate(getLayoutInflater());
        initTabVp();
        return vb.getRoot();
    }



    private void initTabVp() {

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


    private void initList(List<String> data) {
        HomeAdapter homeAdapter = new HomeAdapter();
        homeAdapter.setData(data);
        homeAdapter.setInfoWallpaperListener(this);
        ItemSpace itemSpace = new ItemSpace(10, 10, 0);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
//        vb.rvClass.addItemDecoration(itemSpace);
        vb.rvClass.setLayoutManager(gridLayoutManager);
        vb.rvClass.setAdapter(homeAdapter);

    }

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

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