package com.wall.exquisite.wallpaper;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class FirstMainActivity extends AppCompatActivity {

    // 关键修改：颜色定义调整
    private final int SELECTED_TEXT_ICON_COLOR = Color.parseColor("#98F5F9"); // 选中：图标+文字#98F5F9
    private final int UNSELECTED_TEXT_ICON_COLOR = Color.DKGRAY; // 未选中：深灰色图标+文字
    private final int TAB_BG_COLOR = Color.TRANSPARENT; // 背景透明（不显示选中背景色）

    // 外层Tab的文字和图标资源
    private final String[] outerTabTexts = {"Home", "Setting"};
    private final int[] outerTabIcons = {R.drawable.home2, R.drawable.setting2};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main2);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            int systemBarsTop = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
            int systemBarsBottom = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
            v.setPadding(0, systemBarsTop, 0, systemBarsBottom);
            return insets;
        });

        TabLayout outerTabLayout = findViewById(R.id.outerTabLayout);
        ViewPager2 outerViewPager = findViewById(R.id.outerViewPager);

        Outer_TabAdapter outerTabAdapter = new Outer_TabAdapter(this);
        outerViewPager.setAdapter(outerTabAdapter);

        // 自定义布局绑定外层Tab（布局不变，只改样式逻辑）
        new TabLayoutMediator(outerTabLayout, outerViewPager, (tab, position) -> {
            View customTabView = LayoutInflater.from(this)
                    .inflate(R.layout.custom_outertab, null);

            LinearLayout tabRoot = customTabView.findViewById(R.id.tab_root);
            ImageView tabIcon = customTabView.findViewById(R.id.tab_icon);
            TextView tabText = customTabView.findViewById(R.id.tab_text);

            // 设置文字和图标
            tabText.setText(outerTabTexts[position]);
            tabIcon.setImageResource(outerTabIcons[position]);

            // 强制Tab均匀分布（保留原逻辑）
            ViewGroup.LayoutParams params = customTabView.getLayoutParams();
            if (params == null) {
                params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            } else {
                params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                params.height = ViewGroup.LayoutParams.MATCH_PARENT;
            }
            customTabView.setLayoutParams(params);

            // 初始化样式：默认选中第一个Tab
            tabRoot.setBackgroundColor(TAB_BG_COLOR); // 所有Tab背景都透明
            if (position == 0) {
                // 选中：图标+文字#98F5F9
                tabText.setTextColor(SELECTED_TEXT_ICON_COLOR);
                tabIcon.setColorFilter(SELECTED_TEXT_ICON_COLOR);
            } else {
                // 未选中：深灰色
                tabText.setTextColor(UNSELECTED_TEXT_ICON_COLOR);
                tabIcon.setColorFilter(UNSELECTED_TEXT_ICON_COLOR);
            }

            tab.setCustomView(customTabView);
        }).attach();

        // 监听Tab切换，更新图标+文字颜色（背景不变）
        outerTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabStyle(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabStyle(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });
    }

    // 封装样式更新逻辑（只改文字和图标颜色，背景固定透明）
    private void updateTabStyle(TabLayout.Tab tab, boolean isSelected) {
        View customView = tab.getCustomView();
        if (customView == null) return;

        ImageView tabIcon = customView.findViewById(R.id.tab_icon);
        TextView tabText = customView.findViewById(R.id.tab_text);

        if (isSelected) {
            // 选中：图标+文字#98F5F9
            tabText.setTextColor(SELECTED_TEXT_ICON_COLOR);
            tabIcon.setColorFilter(SELECTED_TEXT_ICON_COLOR);
        } else {
            // 未选中：深灰色
            tabText.setTextColor(UNSELECTED_TEXT_ICON_COLOR);
            tabIcon.setColorFilter(UNSELECTED_TEXT_ICON_COLOR);
        }
    }
}