package com.ar.drawing.art.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

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 androidx.viewpager2.widget.ViewPager2;

import com.ar.drawing.art.R;
import com.ar.drawing.art.databinding.UiHomeBinding;
import com.ar.drawing.art.fragmen.FragFavorite;
import com.ar.drawing.art.fragmen.FragGallery;
import com.ar.drawing.art.fragmen.FragHome;
import com.ar.drawing.art.upload.Http;
import com.ar.drawing.art.upload.SaveUtils;
import com.ar.drawing.art.utils.ArtApp;
import com.ar.drawing.art.utils.Common;
import com.google.android.material.tabs.TabLayout;

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

public class UIMain extends AppCompatActivity {

    private UiHomeBinding binding;

    private List<Fragment> listFragment = new ArrayList<>();




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Common.setStatusBarTextColor(this, true);
        binding = UiHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        listFragment.add(FragHome.newInstance());
        listFragment.add(FragGallery.newInstance());
        listFragment.add(FragFavorite.newInstance());
        boolean post = SaveUtils.INSTANCE.isPost();
        if(!post){
            Http.INSTANCE.makeGetRequest(UIMain.this);
            SaveUtils.INSTANCE.setPost(true);
        }
        onTabInit();
        binding.viewPager2.setUserInputEnabled(false);
        binding.viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getItemCount() {
                return listFragment.size();
            }
        });
        bingTabVp();
    }
    private void bingTabVp() {
        binding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ImageView icon = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.im_icon);
                icon.setSelected(true);
                binding.viewPager2.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ImageView icon = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.im_icon);
                icon.setSelected(false);
            }

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

            }
        });
        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Objects.requireNonNull(binding.tab.getTabAt(position)).select();
            }
        });
    }


    private void onTabInit() {

        for (int i = 0; i < listFragment.size(); i++) {
            TabLayout.Tab tab = binding.tab.newTab();
            View inflate = LayoutInflater.from(this).inflate(R.layout.item_tab, null, false);
            ImageView icon = inflate.findViewById(R.id.im_icon);
            TextView tabText = inflate.findViewById(R.id.tab_text);
            if (i == 0) {
                tabText.setText(ContextCompat.getString(this,R.string.tab_home));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_tab_home));
            } else  if (i == 1){
                tabText.setText(ContextCompat.getString(this,R.string.tab_import));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_selector_import));
            }else {
                tabText.setText(ContextCompat.getString(this,R.string.tab_favorite));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_tab_like));
            }
            tab.setCustomView(inflate);
            binding.tab.addTab(tab);
        }
    }
}