package com.prank.happypranksounds.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.prank.happypranksounds.R;
import com.prank.happypranksounds.databinding.ActivityMainBinding;
import com.prank.happypranksounds.databinding.TabItemBinding;
import com.prank.happypranksounds.topon.AdManager;
import com.prank.happypranksounds.ui.adapter.MainAdapter;

import java.util.Objects;

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.viewpager.setAdapter(adapter);
    }

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

        new TabLayoutMediator(binding.tabLayout, binding.viewpager, (tab, position) -> {
            TabItemBinding tabItemBinding = TabItemBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabItemBinding.getRoot());
            setTab(tabItemBinding, position);
        }).attach();

        binding.tabLayout.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) {
                    TabItemBinding tabBinding = TabItemBinding.bind(tab.getCustomView());

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

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

    private void setTab(TabItemBinding tabItemBinding, int position) {
        int iconResId = getIconResource(position, false);
        int textColorResId = R.color.white;

        switch (position) {
            case 0:
                tabItemBinding.text.setText(R.string.category);
                iconResId = R.drawable.category;
                textColorResId = R.color.black;
                break;
            case 1:
                tabItemBinding.text.setText(R.string.sound);
                break;
            case 2:
                tabItemBinding.text.setText(R.string.collection);
                break;
        }

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

    private int getIconResource(int position, boolean isSelected) {
        switch (position) {
            case 1:
                return isSelected ? R.drawable.sound : R.drawable.un_sound;
            case 2:
                return isSelected ? R.drawable.collection : R.drawable.un_collection;
            default:
                return isSelected ? R.drawable.category : R.drawable.un_category;
        }
    }

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