package com.ardrawing.ardrawingspace.activity;

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

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.ardrawing.ardrawingspace.R;
import com.ardrawing.ardrawingspace.adapter.ViewPagerAdapter;
import com.ardrawing.ardrawingspace.databinding.ActivityMainBinding;
import com.ardrawing.ardrawingspace.databinding.TabCustomBinding;
import com.ardrawing.ardrawingspace.topon.AdManager;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding viewBinding;

    private static final int[] ACTIVE_ICONS = {
            R.drawable.home, R.drawable.resource_import, R.drawable.collection
    };

    private static final int[] INACTIVE_ICONS = {
            R.drawable.un_home, R.drawable.un_import, R.drawable.un_collection
    };

    private static final int COLOR_BLACK = R.color.black;
    private static final int COLOR_GRAY = R.color.gray;

    private static final TabConfig[] TAB_CONFIGS = {
            new TabConfig(R.drawable.home, "Category", COLOR_BLACK),
            new TabConfig(R.drawable.un_import, "Import", COLOR_GRAY),
            new TabConfig(R.drawable.un_collection, "Favorite", COLOR_GRAY)
    };

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

        initData();
        initEvent();
    }

    private void configureEdgeToEdge() {
        EdgeToEdge.enable(this);
    }

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

        viewBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
    }

    private void setupWindowInsets() {
        View mainLayout = findViewById(R.id.main);
        ViewCompat.setOnApplyWindowInsetsListener(mainLayout, (view, windowInsets) -> {
            Insets bars = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
            view.setPadding(bars.left, bars.top, bars.right, bars.bottom);
            return windowInsets;
        });
    }

    private void initData() {
        ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(this);
        viewBinding.viewPager.setAdapter(pagerAdapter);
    }

    private void initEvent() {
        TabLayoutMediator mediator = new TabLayoutMediator(
                viewBinding.tabLayout,
                viewBinding.viewPager,
                this::configureTabView
        );
        mediator.attach();

        viewBinding.tabLayout.addOnTabSelectedListener(createTabListener());
    }

    private void configureTabView(TabLayout.Tab tab, int index) {
        TabCustomBinding tabViewBinding = TabCustomBinding.inflate(LayoutInflater.from(this));
        tab.setCustomView(tabViewBinding.getRoot());
        TabConfig config = determineTabConfig(index);
        tabViewBinding.imageView.setImageResource(config.iconId);
        tabViewBinding.title.setText(config.label);
        tabViewBinding.title.setTextColor(fetchColor(config.textColorId));
    }

    private TabLayout.OnTabSelectedListener createTabListener() {
        return new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                refreshTabAppearance(tab, true);
            }

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

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

            private void refreshTabAppearance(TabLayout.Tab tab, boolean active) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TabCustomBinding tabBinding = TabCustomBinding.bind(customView);
                    updateTabIcon(tabBinding, tab.getPosition(), active);
                    updateTabTextColor(tabBinding, active);
                }
            }

            private void updateTabIcon(TabCustomBinding tabBinding, int pos, boolean active) {
                int iconId = fetchIconResource(pos, active);
                tabBinding.imageView.setImageResource(iconId);
            }

            private void updateTabTextColor(TabCustomBinding tabBinding, boolean active) {
                int colorId = active ? COLOR_BLACK : COLOR_GRAY;
                tabBinding.title.setTextColor(fetchColor(colorId));
            }

            private int fetchIconResource(int pos, boolean active) {
                return active ? ACTIVE_ICONS[pos] : INACTIVE_ICONS[pos];
            }
        };
    }

    private TabConfig determineTabConfig(int index) {
        if (index >= 0 && index < TAB_CONFIGS.length) {
            return TAB_CONFIGS[index];
        }
        throw new IllegalStateException("Unexpected tab index: " + index);
    }

    private static class TabConfig {
        final int iconId;
        final String label;
        final int textColorId;

        TabConfig(int iconId, String label, int textColorId) {
            this.iconId = iconId;
            this.label = label;
            this.textColorId = textColorId;
        }
    }

    private int fetchColor(int colorRes) {
        return ContextCompat.getColor(this, colorRes);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (viewBinding != null) {
            viewBinding = null;
        }
    }
}