package com.sound.prankparty.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.sound.prankparty.Adapter.MainViewPager2Adapter;
import com.sound.prankparty.R;
import com.sound.prankparty.databinding.ActivityMainBinding;
import com.sound.prankparty.databinding.MainTabItemCustomBinding;

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);

        new TabLayoutMediator(binding.mainTablayout, binding.mainViewpager, (tab, position) -> {
            MainTabItemCustomBinding tabBinding = MainTabItemCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabBinding.getRoot());

            if (position == 0) {
                tabBinding.iconCustom.setImageResource(R.drawable.main_sound_select);
            } else if (position == 1) {
                tabBinding.iconCustom.setImageResource(R.drawable.main_radio_unselect);
            } else {
                tabBinding.iconCustom.setImageResource(R.drawable.main_favorite_unselect);
            }
        }).attach();

        //设置标签选中事件
        binding.mainTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    MainTabItemCustomBinding tabBinding = MainTabItemCustomBinding.bind(tab.getCustomView());
                    if (tab.getPosition() == 0) {
                        tabBinding.iconCustom.setImageResource(R.drawable.main_sound_select);
                    } else if (tab.getPosition() == 1) {
                        tabBinding.iconCustom.setImageResource(R.drawable.main_radio_select);
                    } else {
                        tabBinding.iconCustom.setImageResource(R.drawable.main_favorite_select);
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    MainTabItemCustomBinding tabBinding = MainTabItemCustomBinding.bind(tab.getCustomView());
                    if (tab.getPosition() == 0) {
                        tabBinding.iconCustom.setImageResource(R.drawable.main_sound_unselect);
                    } else if (tab.getPosition() == 1) {
                        tabBinding.iconCustom.setImageResource(R.drawable.main_radio_unselect);
                    } else {
                        tabBinding.iconCustom.setImageResource(R.drawable.main_favorite_unselect);
                    }
                }
            }

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

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
            // 返回到ViewPager2的当前Fragment
            binding.mainViewpager.setCurrentItem(binding.mainViewpager.getCurrentItem());
        }
    }
}
