package com.offline.music.playermp3.ui.activity;

import android.view.LayoutInflater;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.offline.music.playermp3.MusicApplication;
import com.offline.music.playermp3.R;
import com.offline.music.playermp3.adapter.HomeViewPagerAdapter;
import com.offline.music.playermp3.databinding.ActivityHomeBinding;
import com.offline.music.playermp3.databinding.HomeTabCustomBinding;
import com.offline.music.playermp3.helper.CommonUtils;

public class HomeActivity extends BaseActivity<ActivityHomeBinding> {

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

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

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

    @Override
    protected void onCreateInit() {
        initView();  // 可以在这里初始化视图
    }

    @Override
    protected void onInitClick() {

    }

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

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

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

    public void initView() {

        int statusBarHeight = CommonUtils.getStatusBarHeight(MusicApplication.myApplication);
        View root = vb.getRoot();
        root.setPadding(0,statusBarHeight,0,0);

        HomeViewPagerAdapter adapter = new HomeViewPagerAdapter(this);
        vb.homeViewPager.setAdapter(adapter);
        vb.homeViewPager.setUserInputEnabled(false);

        // 设置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);  // 更新选中的图标
                int position = tab.getPosition();
                switch (position){
                    case 0:
                        vb.view.setVisibility(View.VISIBLE);
                        vb.homeLayout.setBackgroundColor(ContextCompat.getColor(HomeActivity.this,R.color.black));
                        break;
                    case 1:
                        vb.view.setVisibility(View.GONE);
                        vb.homeLayout.setBackgroundColor(ContextCompat.getColor(HomeActivity.this,R.color.default_play_list_color));
                        break;
                    case 2:
                        vb.view.setVisibility(View.GONE);
                        vb.homeLayout.setBackground(ContextCompat.getDrawable(HomeActivity.this,R.mipmap.bg_library));
                        break;
                }
            }
            @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) {

    }
}

