package com.wall.photography.wallpapers.fragment;

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.photography.wallpapers.MyApp;
import com.wall.photography.wallpapers.acctivity.ListActivity;
import com.wall.photography.wallpapers.acctivity.WallpaperActivity;
import com.wall.photography.wallpapers.databinding.FragmentMainBinding;
import com.wall.photography.wallpapers.tool.ComUtils;
import com.wall.photography.wallpapers.tool.ItemHelper;
import com.wall.photography.wallpapers.connector.OnItemClick;
import com.wall.photography.wallpapers.mybean.MyData;
import com.wall.photography.wallpapers.item.ItemMain;

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


public class FragmentMain extends Fragment implements OnItemClick {
    private FragmentMainBinding vb;
    public FragmentMain() {
    }
    public static FragmentMain newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(ComUtils.key_index, type);
        FragmentMain fragment = new FragmentMain();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragmentMainBinding.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) {
        ItemMain adHomeParent = new ItemMain();
        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(MyData info) {
        Intent intent = new Intent(requireContext(), WallpaperActivity.class);
        intent.putExtra(ComUtils.key_info, info);
        startActivity(intent);
    }

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