package com.lock.arcanvasart.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.lock.arcanvasart.R;
import com.lock.arcanvasart.adapter.MainViewPager2Adapter;
import com.lock.arcanvasart.databinding.ActivityMainBinding;
import com.lock.arcanvasart.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());

        initData();
        initEvent();
    }

    // 初始化数据
    private void initData() {
        MainViewPager2Adapter adapter = new MainViewPager2Adapter(this);
        binding.mainViewPager.setAdapter(adapter);
    }

    // 初始化事件
    private void initEvent() {
        setupTabLayoutMediator();
        setupTabSelectedListener();

        binding.middleButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CameraActivity.class);
                startActivity(intent);
            }
        });

    }

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

            // 设置不同的左右间距
            View tabView = tab.getCustomView();
            if (tabView != null) {
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) tabView.getLayoutParams();
                switch (position) {
                    case 1:
                        layoutParams.setMargins(0, 0, 40, 0);
                        break;
                    case 2:
                        layoutParams.setMargins(40, 0, 0, 0);
                        break;
                    default:
                        layoutParams.setMargins(0, 0, 0, 0);
                        break;
                }
                tabView.setLayoutParams(layoutParams); // 更新 Tab 的布局参数
            }
        }).attach();
    }


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

    // 设置标签选中事件
    private void setupTabSelectedListener() {
        binding.mainTab.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.category_fragment;
                    case 2:
                        return R.drawable.favorite_fragment;
                    case 3:
                        return R.drawable.profile_fragment;
                    default:
                        return R.drawable.home_fragment;
                }
            }

            // 显示小圆点
            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);
                }
            }
        });
    }
}
