package com.ar.artracestudio.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.ar.artracestudio.R;
import com.ar.artracestudio.adapter.MainViewPager2Adapter;
import com.ar.artracestudio.databinding.ActivityMainBinding;
import com.ar.artracestudio.databinding.MainTabItemCustomBinding;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        MainViewPager2Adapter adapter = new MainViewPager2Adapter(this);
        binding.mainViewpager.setAdapter(adapter);

        setupTabLayoutMediator();
        setupTabSelectedListener();
    }

    // 设置TabLayout和ViewPager的关联
    private void setupTabLayoutMediator() {
        new TabLayoutMediator(binding.mainTablayout, binding.mainViewpager, (tab, position) -> {
            MainTabItemCustomBinding tabBinding = MainTabItemCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabBinding.getRoot());
            setTabIconAndDotVisibility(tabBinding, position);
        }).attach();
    }

    // 设置标签图标和小圆点的可见性
    private void setTabIconAndDotVisibility(MainTabItemCustomBinding tabBinding, int position) {
        switch (position) {
            case 0:
                tabBinding.iconCustom.setImageResource(R.drawable.home);
                tabBinding.dotView.setVisibility(View.VISIBLE);
                break;
            case 1:
                tabBinding.iconCustom.setImageResource(R.drawable.import_image);
                break;
            case 2:
                tabBinding.iconCustom.setImageResource(R.drawable.collection);
                break;
            default:
                tabBinding.iconCustom.setImageResource(R.drawable.home);
                break;
        }
    }

    // 设置标签选中事件
    private void setupTabSelectedListener() {
        binding.mainTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabIcon(tab, true);
                showDot(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabIcon(tab, false);
                hideDot(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 处理Tab重新选中事件
            }

            // 更新Tab图标
            private void updateTabIcon(TabLayout.Tab tab, boolean isSelected) {
                if (tab.getCustomView() != null) {
                    MainTabItemCustomBinding tabBinding = MainTabItemCustomBinding.bind(tab.getCustomView());
                    int iconResId = getIconResource(tab.getPosition(), isSelected);
                    tabBinding.iconCustom.setImageResource(iconResId);
                }
            }

            // 获取图标资源
            private int getIconResource(int position, boolean isSelected) {
                // 选中状态图标
                switch (position) {
                    case 1:
                        return R.drawable.import_image;
                    case 2:
                        return R.drawable.collection;
                    default:
                        return R.drawable.home;
                }
            }

            // 显示小圆点
            private void showDot(TabLayout.Tab tab) {
                View dotView = tab.view.findViewById(R.id.dot_view);
                if (dotView != null) {
                    dotView.setVisibility(View.VISIBLE);
                }
            }

            // 隐藏小圆点
            private void hideDot(TabLayout.Tab tab) {
                View dotView = tab.view.findViewById(R.id.dot_view);
                if (dotView != null) {
                    dotView.setVisibility(View.GONE);
                }
            }
        });
    }

}
