package com.key.vibekeyboard.Activity;

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.key.vibekeyboard.Adapter.MainViewpager2Adapter;
import com.key.vibekeyboard.AppApplication;
import com.key.vibekeyboard.Dialog.PermissionRequestDialog;
import com.key.vibekeyboard.Dialog.RecommendedDialog;
import com.key.vibekeyboard.R;
import com.key.vibekeyboard.Room.Category;
import com.key.vibekeyboard.Room.WallpaperInfo;
import com.key.vibekeyboard.Utils.Mytool;
import com.key.vibekeyboard.databinding.ActivityMainBinding;
import com.key.vibekeyboard.databinding.MainTabCustomBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private List<WallpaperInfo> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 使用 ViewBinding 绑定布局
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 创建并设置 ViewPager 适配器
        MainViewpager2Adapter adapter = new MainViewpager2Adapter(this);
        binding.mainViewpager.setAdapter(adapter);

        // 解析 Json 文件并随机选取一个分类
        List<Category> categories = AppApplication.getCategories();
        Random random = new Random();
        int randomIndex = random.nextInt(categories.size()); // 随机选取分类索引
        list = categories.get(randomIndex).getList(); // 获取随机选中的分类中的壁纸列表

        // 判断是否完成步骤1和步骤2，显示相应的对话框
        if (Mytool.isStep1() && Mytool.isStep2()) {
            showRecommendedDialog();  // 如果两个步骤都完成，显示推荐对话框
        } else {
            showPermissionDialog();  // 否则显示权限请求对话框
        }

//        binding.mainViewpager.setUserInputEnabled(false);//禁止页面左右滑动

        // 初始化 TabLayout（标签栏）
        tabLayout();
    }

    // 显示权限请求对话框
    public void showPermissionDialog() {
        PermissionRequestDialog dialog = new PermissionRequestDialog();
        dialog.show(getSupportFragmentManager(), "PermissionRequestDialog");
    }

    // 显示推荐壁纸对话框
    public void showRecommendedDialog() {
        RecommendedDialog dialog = new RecommendedDialog();
        if (list != null) {
            dialog.setList(list);  // 设置壁纸列表
        }
        dialog.show(getSupportFragmentManager(), "RecommendedDialog");
    }

    // 初始化 TabLayout，设置标签和关联的 ViewPager
    public void tabLayout() {
        // 使用 TabLayoutMediator 连接 TabLayout 和 ViewPager2
        new TabLayoutMediator(binding.mainTablayout, binding.mainViewpager, (tab, position) -> {
            // 创建自定义 Tab 视图
            MainTabCustomBinding tabBinding = MainTabCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabBinding.getRoot());

            // 设置默认的图标，未选中状态的图标
            if (position == 0) {
                tabBinding.iconCustom.setImageResource(R.drawable.home_select);  // 第一个 Tab 图标
            } else if (position == 1) {
                tabBinding.iconCustom.setImageResource(R.drawable.collection);  // 第二个 Tab 图标
            } else {
                tabBinding.iconCustom.setImageResource(R.drawable.setting);  // 第三个 Tab 图标
            }
        }).attach();  // 绑定 TabLayout 和 ViewPager

        // 设置 Tab 选中时和未选中时的图标变化
        binding.mainTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 选中时更改图标
                MainTabCustomBinding tabBinding = MainTabCustomBinding.bind(Objects.requireNonNull(tab.getCustomView()));
                int position = tab.getPosition();
                if (position == 0) {
                    tabBinding.iconCustom.setImageResource(R.drawable.home_select);  // 选中状态的图标
                } else if (position == 1) {
                    tabBinding.iconCustom.setImageResource(R.drawable.collection_select);  // 第二个 Tab 选中图标
                } else {
                    tabBinding.iconCustom.setImageResource(R.drawable.setting_select);  // 第三个 Tab 选中图标
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 未选中时恢复默认图标
                MainTabCustomBinding tabBinding = MainTabCustomBinding.bind(Objects.requireNonNull(tab.getCustomView()));
                int position = tab.getPosition();
                if (position == 0) {
                    tabBinding.iconCustom.setImageResource(R.drawable.home);  // 未选中状态的图标
                } else if (position == 1) {
                    tabBinding.iconCustom.setImageResource(R.drawable.collection);  // 第二个 Tab 未选中图标
                } else {
                    tabBinding.iconCustom.setImageResource(R.drawable.setting);  // 第三个 Tab 未选中图标
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 可以留空，或者添加重复选择时的逻辑
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null; // 释放 ViewBinding 的引用
    }

}
