package com.phone.wall.paper.view;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.tabs.TabLayout;
import com.phone.wall.paper.R;
import com.phone.wall.paper.databinding.ItemTabBinding;
import com.phone.wall.paper.databinding.UiMainBinding;
import com.phone.wall.paper.tools.Utils;

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

public class Main extends AppCompatActivity  {


    private UiMainBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = UiMainBinding.inflate(getLayoutInflater());
        Utils.initFull(this,false);
        setContentView(vb.getRoot());


        List<Fragment> list = new ArrayList<>();
        list.add(FragMain.newInstance(0));
        list.add(FragSearch.newInstance());
        list.add(FragFavorite.newInstance());

        int[] ints = {R.drawable.status_home,R.drawable.selector_search, R.drawable.status__like};
        vb.viewpager2.setAdapter(new FragmentStateAdapter(this) {
            @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.tab.newTab();
            ItemTabBinding tabVb = ItemTabBinding.inflate(getLayoutInflater());
            tabVb.imageIcon.setImageDrawable(ContextCompat.getDrawable(this,ints[i]));
            tab.setCustomView(tabVb.getRoot());
            if(i ==0){
                tabVb.imageIndicator.setVisibility(View.VISIBLE);
            }else {
                tabVb.imageIndicator.setVisibility(View.GONE);
            }
            vb.tab.addTab(tab);
        }
        vb.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                vb.viewpager2.setCurrentItem(position);
                View customView = tab.getCustomView();
                View imageIndicator = customView.findViewById(R.id.image_indicator);
                imageIndicator.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                View imageIndicator = customView.findViewById(R.id.image_indicator);
                imageIndicator.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }



}