package com.photo.wallpaper.category.activity;

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.photo.wallpaper.category.R;
import com.photo.wallpaper.category.ad.AdManager;
import com.photo.wallpaper.category.databinding.ActivityLayoutMainBinding;
import com.photo.wallpaper.category.databinding.LayoutTabCustomViewBinding;
import com.photo.wallpaper.category.utils.CommonHelper;

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

public class ActivityMain extends AppCompatActivity  {


    private ActivityLayoutMainBinding vb;

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

        AdManager.loadAllAd();
        List<Fragment> list = new ArrayList<>();
        list.add(FragHome.newInstance(0));
        list.add(FragSearch.newInstance());
        list.add(FragLike.newInstance());
//        list.add(FragmentDownload.newInstance());

        int[] ints = {R.drawable.st_home,R.drawable.st_search, R.drawable.st_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();
            LayoutTabCustomViewBinding tabVb = LayoutTabCustomViewBinding.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) {

            }
        });


    }



}