package com.hi.music.player.ui.activity;

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

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.hi.music.player.R;
import com.hi.music.player.adapter.A_HomeViewPagerAdapter;
import com.hi.music.player.databinding.ActivityAhomeBinding;
import com.hi.music.player.databinding.HomeTabCustomBinding;

public class A_HomeActivity extends BaseActivity<ActivityAhomeBinding> {

    // 图标数组定义为类成员，避免重复
    private final int[] defaultIcons = {
            R.drawable.home_unselect,
            R.drawable.import_unselect,
    };

    private final int[] selectedIcons = {
            R.drawable.home_select,
            R.drawable.import_select,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected ActivityAhomeBinding getViewBinding() {
        return ActivityAhomeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onCreateInit() {

        initData();

    }

    @Override
    protected void onInitClick() {

    }

    @Override
    public boolean isFullScreen() {
        return true;
    }

    @Override
    public boolean statusBarLight() {
        return false;
    }

    public void initData(){

        A_HomeViewPagerAdapter adapter = new A_HomeViewPagerAdapter(this);
        vb.homeViewpager.setAdapter(adapter);

        // 设置TabLayout的图标
        new TabLayoutMediator(vb.homeTabLayout, vb.homeViewpager, (tab, position) -> {
            HomeTabCustomBinding tabBinding = HomeTabCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabBinding.getRoot());
            tabBinding.homeIcon.setImageResource(defaultIcons[position]);  // 默认图标
        }).attach();

        // 添加Tab选中与未选中事件监听器
        vb.homeTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabIcon(tab, true);  // 更新选中的图标
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabIcon(tab, false);  // 恢复未选中图标
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 可选：重复选择Tab时的操作
            }
        });

        // 设置默认选中第一个
        TabLayout.Tab firstTab = vb.homeTabLayout.getTabAt(0);
        if (firstTab != null) {
            firstTab.select();
            updateTabIcon(firstTab, true);  // 设置选中的图标
        }
    }

    private void updateTabIcon(TabLayout.Tab tab, boolean isSelected) {
        HomeTabCustomBinding tabBinding = HomeTabCustomBinding.bind(tab.getCustomView());
        int position = tab.getPosition();
        tabBinding.homeIcon.setImageResource(isSelected ? selectedIcons[position] : defaultIcons[position]);
    }

    @Override
    public void onClick(View v) {

    }
}

