package com.newapp.paperwall.uiview;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
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 com.google.android.material.tabs.TabLayout;
import com.newapp.paperwall.R;
import com.newapp.paperwall.databinding.ActivityMainBinding;
import com.newapp.paperwall.databinding.ItemTabBinding;
import com.newapp.paperwall.tools.UiUtils;

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());
        UiUtils.initFull(this, true);
        setContentView(vb.getRoot());


        List<Fragment> list = new ArrayList<>();
        list.add(HomeFragment.newInstance(0));
//        list.add(FindFragment.newInstance());
        list.add(CollectFragment.newInstance());

        int[] ints = {R.drawable.selector_home,  R.drawable.selector_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.imgIcon.setImageDrawable(ContextCompat.getDrawable(this, ints[i]));
            tab.setCustomView(tabVb.getRoot());
            if (i == 0) {
                tabVb.tvBottom.setText(R.string.category);
                tabVb.tvBottom.setTextColor(Color.parseColor("#ff617b"));
            } else {
                tabVb.tvBottom.setText(R.string.collect);
                tabVb.tvBottom.setTextColor(Color.parseColor("#808080"));
            }
            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();
                assert customView != null;
                TextView imageIndicator = customView.findViewById(R.id.tvBottom);
                imageIndicator.setTextColor(Color.parseColor("#ff617b"));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                assert customView != null;
                TextView imageIndicator = customView.findViewById(R.id.tvBottom);
                imageIndicator.setTextColor(Color.parseColor("#808080"));
            }

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

            }
        });

    initSearchClick();
    }
private void initSearchClick(){
        vb.toSearch.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, SearchActivity.class);
            startActivity(intent);
        });
}

}