package com.wallpaper.wallpaperbox.ui.activity;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
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.wallpaper.wallpaperbox.R;
import com.wallpaper.wallpaperbox.databinding.ActivityMainBinding;
import com.wallpaper.wallpaperbox.databinding.MainCustomBinding;
import com.wallpaper.wallpaperbox.topon.AdManager;
import com.wallpaper.wallpaperbox.ui.adapter.MainAdapter;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

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

        initData();
        initEvent();
    }

    private void initData() {
        AdManager.loadAllAd();

        MainAdapter adapter = new MainAdapter(this);
        binding.mainViewpager2.setAdapter(adapter);
    }

    private void initEvent() {
        binding.mainViewpager2.setUserInputEnabled(false);

        new TabLayoutMediator(binding.mainTabLayout, binding.mainViewpager2, (tab, position) -> {
            MainCustomBinding mainCustomBinding = MainCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(mainCustomBinding.getRoot());
            setTab(mainCustomBinding, position);
        }).attach();

        binding.mainTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTab(tab, true);
            }

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

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

            private void updateTab(TabLayout.Tab tab, boolean isSelected) {
                if (tab.getCustomView() != null) {
                    MainCustomBinding mainCustomBinding = MainCustomBinding.bind(tab.getCustomView());

                    int iconResId = getIconResource(tab.getPosition(), isSelected);
                    mainCustomBinding.image.setImageResource(iconResId);

                    int textColor = isSelected ? R.color.blue : R.color.black;
                    mainCustomBinding.text.setTextColor(ContextCompat.getColor(MainActivity.this, textColor));
                }
            }
        });
    }

    private void setTab(MainCustomBinding mainCustomBinding, int position) {
        int iconResId = getIconResource(position, false);
        int textColorResId = R.color.black;

        switch (position) {
            case 0:
                mainCustomBinding.text.setText("Home");
                iconResId = R.drawable.home;
                textColorResId = R.color.blue;
                break;
            case 1:
                mainCustomBinding.text.setText("Collection");
                break;
        }

        mainCustomBinding.image.setImageResource(iconResId);
        mainCustomBinding.text.setTextColor(ContextCompat.getColor(this, textColorResId));
    }

    private int getIconResource(int position, boolean isSelected) {
        if (position == 1) {
            return isSelected ? R.drawable.collection : R.drawable.un_collection;
        }
        return isSelected ? R.drawable.home : R.drawable.un_home;
    }

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