package com.wall.various.wallpaper.ui;

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.wall.various.wallpaper.R;
import com.wall.various.wallpaper.databinding.ItemTabBinding;
import com.wall.various.wallpaper.databinding.UiMainBinding;
import com.wall.various.wallpaper.helper.Common;
import com.wall.various.wallpaper.upload.Http;
import com.wall.various.wallpaper.upload.SaveUtils;

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

public class UiMain extends AppCompatActivity  {


    private UiMainBinding vb;

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


        List<Fragment> list = new ArrayList<>();
        list.add(FragUiMain.newInstance(0));
        list.add(FragUiSearch.newInstance());
        list.add(FragUiLike.newInstance());
//        list.add(FragmentDownload.newInstance());

        int[] ints = {R.drawable.selector_tab1,R.drawable.selector_tab2, R.drawable.selector_tab3};
        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) {

            }
        });
        upload();

    }


    private void  upload(){
        boolean post = SaveUtils.INSTANCE.isPost();
        if(!post){
            Http.INSTANCE.makeGetRequest(UiMain.this);
            SaveUtils.INSTANCE.setPost(true);
        }
    }


}