package com.wall.photography.wallpapers.acctivity;

import android.content.Intent;
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.photography.wallpapers.R;
import com.wall.photography.wallpapers.databinding.ActivityMainBinding;
import com.wall.photography.wallpapers.databinding.ItemTabBinding;
import com.wall.photography.wallpapers.fragment.FragmentLike;
import com.wall.photography.wallpapers.fragment.FragmentMain;
import com.wall.photography.wallpapers.tool.ComUtils;
import com.wall.photography.wallpapers.up.Http;
import com.wall.photography.wallpapers.up.SaveUtils;

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

public class MainActivity extends AppCompatActivity {


    private ActivityMainBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityMainBinding.inflate(getLayoutInflater());
        ComUtils.initFull(this, false);
        setContentView(vb.getRoot());
        List<Fragment> list = new ArrayList<>();
        list.add(FragmentMain.newInstance(0));
//        list.add(FragUiSearch.newInstance());
        list.add(FragmentLike.newInstance());
//        list.add(FragmentDownload.newInstance());

        boolean post = SaveUtils.INSTANCE.isPost();
        if(!post){
            Http.INSTANCE.makeGetRequest(MainActivity.this);
            SaveUtils.INSTANCE.setPost(true);
        }

        int[] ints = {R.drawable.selector_home, R.drawable.selector_tab_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);
                if (position == 0) {
                    vb.textviewWallpaper.setText(getString(R.string.app_name));
                } else {
                    vb.textviewWallpaper.setText(getString(R.string.collections));
                }
            }

            @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) {

            }
        });

        vb.imSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });
    }


}