package com.wall.photography.picture.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.wall.photography.picture.fragment.MainFragment;
import com.wall.photography.picture.R;
import com.wall.photography.picture.databinding.ActivityMainBinding;
import com.wall.photography.picture.databinding.ProjectTabBinding;
import com.wall.photography.picture.fragment.Like;
import com.wall.photography.picture.fragment.Settings;
import com.wall.photography.picture.tool.CommonUtils;

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

public class Main extends AppCompatActivity {


    private ActivityMainBinding vb;

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

        int[] ints = {R.drawable.selector_home, R.drawable.selector_tab_like, R.drawable.selector_tab_settings};
        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();
            ProjectTabBinding tabVb = ProjectTabBinding.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);
                
                // 更新图标为选中状态
                android.widget.ImageView imageIcon = customView.findViewById(R.id.image_icon);
                if (imageIcon != null) {
                    imageIcon.setSelected(true);
                }
                
                if (position == 0) {
                    vb.textviewWallpaper.setText(getString(R.string.app_name));
                } else if (position == 1) {
                    vb.textviewWallpaper.setText(getString(R.string.collections));
                } else if (position == 2) {
                    vb.textviewWallpaper.setText(getString(R.string.settings));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                View imageIndicator = customView.findViewById(R.id.image_indicator);
                imageIndicator.setVisibility(View.INVISIBLE);
                
                // 更新图标为未选中状态
                android.widget.ImageView imageIcon = customView.findViewById(R.id.image_icon);
                if (imageIcon != null) {
                    imageIcon.setSelected(false);
                }
            }

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

            }
        });

    }


}
