package com.key.vibekeyboard.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.key.vibekeyboard.ui.adapter.MainViewpager2Adapter;
import com.key.vibekeyboard.MyApplication;
import com.key.vibekeyboard.ui.dialog.PermissionRequestDialog;
import com.key.vibekeyboard.ui.dialog.RecommendedDialog;
import com.key.vibekeyboard.R;
import com.key.vibekeyboard.data.bean.Category;
import com.key.vibekeyboard.data.database.entity.WallpaperInfo;
import com.key.vibekeyboard.databinding.ActivityMainBinding;
import com.key.vibekeyboard.databinding.MainTabCustomBinding;
import com.key.vibekeyboard.utils.InputMethodUtils;

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);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initViewPager();

        loadRandomCategory();

        checkPermissionsAndShowDialog();

        binding.mainViewpager.setUserInputEnabled(false);

        initTabLayout();
    }

    private void initViewPager() {
        MainViewpager2Adapter adapter = new MainViewpager2Adapter(this);
        binding.mainViewpager.setAdapter(adapter);
    }

    private void loadRandomCategory() {
        List<Category> categories = MyApplication.getCategories();
        Random random = new Random();
        int randomIndex = random.nextInt(categories.size());
        list = categories.get(randomIndex).getList();
    }

    private void checkPermissionsAndShowDialog() {
        if (InputMethodUtils.isInputMethodEnabled() && InputMethodUtils.isCurrentInputMethodActive()) {
            showRecommendedDialog();
        } else {
            showPermissionDialog();
        }
    }

    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");
    }

    private void initTabLayout() {
        new TabLayoutMediator(binding.mainTablayout, binding.mainViewpager, (tab, position) -> {
            MainTabCustomBinding tabBinding = MainTabCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabBinding.getRoot());

            setTabIcon(tabBinding, position);
        }).attach();

        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();
                setTabIcon(tabBinding, position, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                MainTabCustomBinding tabBinding = MainTabCustomBinding.bind(Objects.requireNonNull(tab.getCustomView()));
                int position = tab.getPosition();
                setTabIcon(tabBinding, position, false);
            }

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

    private void setTabIcon(MainTabCustomBinding tabBinding, int position) {
        setTabIcon(tabBinding, position, false);
    }

    private void setTabIcon(MainTabCustomBinding tabBinding, int position, boolean isSelected) {
        if (position == 0) {
            tabBinding.iconCustom.setImageResource(isSelected ? R.drawable.home_select : R.drawable.home);
        } else if (position == 1) {
            tabBinding.iconCustom.setImageResource(isSelected ? R.drawable.collection_select : R.drawable.collection);
        } else {
            tabBinding.iconCustom.setImageResource(isSelected ? R.drawable.setting_select : R.drawable.setting);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}

