package com.wall.exquisite.wallpaper;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

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

public class HomePageFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        TabLayout innerTabLayout = view.findViewById(R.id.innerTabLayout);
        ViewPager2 innerViewPager = view.findViewById(R.id.innerViewPager);

        Inner_TabAdapter innerTabAdapter = new Inner_TabAdapter(requireActivity());
        innerViewPager.setAdapter(innerTabAdapter);

        String[] innerTabTexts = {"Animals", "Nature", "Patterns"};
        // 定义颜色变量，方便统一管理
        int selectedColor = getResources().getColor(android.R.color.white);
        int unselectedColor = android.graphics.Color.parseColor("#98F5F9");

        new TabLayoutMediator(innerTabLayout, innerViewPager, (tab, position) -> {
            View customTabView = LayoutInflater.from(getContext())
                    .inflate(R.layout.custom_innertab, null);

            TextView tabText = customTabView.findViewById(R.id.tab_text);
            View tabDivider = customTabView.findViewById(R.id.tab_divider);

            tabText.setText(innerTabTexts[position]);
            // 初始化时设置颜色：第一个Tab默认选中（白色），其他未选中（#98F5F9）
            tabText.setTextColor(position == 0 ? selectedColor : unselectedColor);

            if (position == innerTabTexts.length - 1) {
                tabDivider.setVisibility(View.GONE);
            }

            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.setCustomView(customTabView);
        }).attach();

        // 选中状态监听（切换时更新颜色）
        innerTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tabText = tab.getCustomView().findViewById(R.id.tab_text);
                tabText.setTextColor(selectedColor); // 选中：白色
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tabText = tab.getCustomView().findViewById(R.id.tab_text);
                tabText.setTextColor(unselectedColor); // 未选中：#98F5F9
            }

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

        // 不需要额外调用 selectTab，因为在 TabLayoutMediator 中已经初始化了第一个Tab的颜色
        return view;
    }
}