package com.sound.prankparty.Main;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.sound.prankparty.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {

    private ViewPager2 viewPager2;
    private TabLayout tabLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager2 = findViewById(R.id.main_viewpager);
        tabLayout = findViewById(R.id.main_tablayout);

        MainViewPager2Adapter adapter = new MainViewPager2Adapter(this);
        viewPager2.setAdapter(adapter);

//        viewPager2.setUserInputEnabled(false); //true:滑动，false：禁止滑动

        new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
            View customView = LayoutInflater.from(this).inflate(R.layout.main_tab_item_custom, null);
            tab.setCustomView(customView);

            ImageView tabIcon = customView.findViewById(R.id.icon_custom);
            if (tabIcon != null) {
                if (position == 0) {
                    tabIcon.setImageResource(R.drawable.main_sound_select);
                } else if (position == 1) {
                    tabIcon.setImageResource(R.drawable.main_radio_unselect);
                } else {
                    tabIcon.setImageResource(R.drawable.main_favorite_unselect);
                }
            }

        }).attach();


        //设置标签选中事件
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    ImageView tabIcon = customView.findViewById(R.id.icon_custom);
                    if (tabIcon != null) {
                        if (tab.getPosition() == 0) {
                            tabIcon.setImageResource(R.drawable.main_sound_select);
                        } else if (tab.getPosition() == 1) {
                            tabIcon.setImageResource(R.drawable.main_radio_select);
                        } else {
                            tabIcon.setImageResource(R.drawable.main_favorite_select);
                        }
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    ImageView tabIcon = customView.findViewById(R.id.icon_custom);
                    if (tabIcon != null) {
                        if (tab.getPosition() == 0) {
                            tabIcon.setImageResource(R.drawable.main_sound_unselect);
                        } else if (tab.getPosition() == 1) {
                            tabIcon.setImageResource(R.drawable.main_radio_unselect);
                        } else {
                            tabIcon.setImageResource(R.drawable.main_favorite_unselect);
                        }
                    }
                }
            }

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

    }

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