package com.wall.various.wallpaper.ui;

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.various.wallpaper.MyApp;
import com.wall.various.wallpaper.databinding.FragUiMainBinding;
import com.wall.various.wallpaper.helper.Common;
import com.wall.various.wallpaper.helper.ItemHelper;
import com.wall.various.wallpaper.inter.ItemClickInterface;
import com.wall.various.wallpaper.myjson.Data;
import com.wall.various.wallpaper.recycler.RecyclerHome;

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


public class FragUiMain extends Fragment implements ItemClickInterface {
    private FragUiMainBinding vb;
    public FragUiMain() {
    }
    public static FragUiMain newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(Common.key_index, type);
        FragUiMain fragment = new FragUiMain();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragUiMainBinding.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) {
        RecyclerHome adHomeParent = new RecyclerHome();
        adHomeParent.setData(data);
        adHomeParent.setInfoWallpaperListener(this);
        ItemHelper itemHelper = new ItemHelper(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(), UiSetWallpaper.class);
        intent.putExtra(Common.key_info, info);
        startActivity(intent);
    }

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