package com.exquisite.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.exquisite.R;
import com.google.android.material.tabs.TabLayout;

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

public class AllFragment extends Fragment {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private MyPagerAdapter adapter;
    private TextView tabtext;
    private List<Fragment> list = new ArrayList<>();

    private String[] listtext = {"For You", "Love", "Cool", "Cute", "Neon", "Aesthetic"};

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all, container, false);

        tabLayout = view.findViewById(R.id.all_tabLayout);
        viewPager = view.findViewById(R.id.all_viewpager);


        list.add(new FragmentForyou());
        list.add(new FragmentLove());
        list.add(new FragmentCool());
        list.add(new FragmentCute());
        list.add(new FragmentNeon());
        list.add(new FragmentAesthetic());

        adapter = new MyPagerAdapter(getChildFragmentManager(), list);
        viewPager.setAdapter(adapter);

        for (int i = 0; i < 6; i++) {
            TabLayout.Tab tab4k = tabLayout.newTab();
            tabLayout.addTab(tab4k);
            View customView = LayoutInflater.from(requireContext()).inflate(R.layout.custom_tablayout, null);
            tabtext = customView.findViewById(R.id.tab_text);
            tabtext.setText(listtext[i]);
            tabtext.setBackgroundResource(android.R.color.transparent);
            tab4k.setCustomView(customView);
            final int finalI = i;
            tab4k.view.setOnClickListener(v -> {
                viewPager.setCurrentItem(finalI);
            });
        }


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTabIcons(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        return view;

    }

    private void setTabIcons(int position) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.getCustomView().findViewById(R.id.tab_text).setSelected(i == position);
            }
        }

    }

    private static class MyPagerAdapter extends FragmentStatePagerAdapter {

        private final List<Fragment> fragmentList;

        public MyPagerAdapter(@NonNull FragmentManager fm, List<Fragment> list) {
            super(fm);
            fragmentList = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }
}