package com.free.life.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.free.life.wallpaper.MyWallpaper;
import com.free.life.wallpaper.databinding.FragmentMainBinding;
import com.free.life.wallpaper.recycler.HomeParentAdapter;
import com.free.life.wallpaper.bean.Category;
import com.free.life.wallpaper.bean.Data;
import com.free.life.wallpaper.mylistener.ItemClickListener;
import com.free.life.wallpaper.utils.CommonUtils;
import com.free.life.wallpaper.utils.RecyclerSpace;

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


public class FragmentHome extends Fragment implements ItemClickListener {


    private int mWallpaperType;


    private List<Data> userList;

    private FragmentMainBinding vb;


    public FragmentHome() {

    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mWallpaperType = getArguments().getInt(CommonUtils.key_index);
        }

    }

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

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



    private void initTabVp() {
        List<Fragment> list = new ArrayList<>();
        List<Category> data = new ArrayList<>();
        while (data.size() < 7) {
            data = MyWallpaper.getData();
        }
        vb.loadingPb.setVisibility(View.GONE);
//        Log.d(MyWallpaper.TAG, "------------data=" + data);
//        for (int i = 0; i < data.size(); i++) {
//            list.add(FragmentChild.newInstance(i));
//        }

        initList(data);

//        vb.viewpager2.setOffscreenPageLimit(1);
//        vb.viewpager2.setAdapter(new FragmentStateAdapter(getChildFragmentManager(),getLifecycle()) {
//            @NonNull
//            @Override
//            public Fragment createFragment(int position) {
//                return list.get(position);
//            }
//
//            @Override
//            public int getItemCount() {
//                return list.size();
//            }
//        });
//        vb.viewpager2.setUserInputEnabled(false);
//
//        for (int i = 0; i < list.size(); i++) {
//            TabLayout.Tab tab = vb.tabClass.newTab();
//            ClassTabViewBinding tabVb = ClassTabViewBinding.inflate(getLayoutInflater());
//            tabVb.name.setText(data.get(i).getClassName());
//            tab.setCustomView(tabVb.getRoot());
//            tabVb.name.setSelected(i == 0);
//            vb.tabClass.addTab(tab);
//        }
//        vb.tabClass.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                int position = tab.getPosition();
//                vb.viewpager2.setCurrentItem(position);
//
//                View customView = tab.getCustomView();
//                if (customView != null) {
//                    CustomerTv tv = customView.findViewById(R.id.name);
//                    tv.setSelected(true);
//                }
//
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//                View customView = tab.getCustomView();
//                if (customView != null) {
//                    CustomerTv tv = customView.findViewById(R.id.name);
//                    tv.setSelected(false);
//                }
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
    }


    private void initList(List<Category> data) {
        HomeParentAdapter homeParentAdapter = new HomeParentAdapter();
        homeParentAdapter.setData(data);
        homeParentAdapter.setInfoWallpaperListener(this);
        RecyclerSpace recyclerSpace = new RecyclerSpace(10, 10, 0);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(requireContext());
//        vb.classRecycler.addItemDecoration(itemSpace);
        vb.classRecycler.setLayoutManager(linearLayoutManager);
        vb.classRecycler.setAdapter(homeParentAdapter);

    }

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

    @Override
    public void onClickMore(int index) {
        Intent intent = new Intent(requireContext(), ClassActivity.class);
        intent.putExtra(ClassActivity.key_index, index);
        startActivity(intent);
    }
}