package com.ardraw.ardrawingflow.ui.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.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ardraw.ardrawingflow.R;
import com.ardraw.ardrawingflow.databinding.ActivityMainBinding;
import com.ardraw.ardrawingflow.databinding.TabCustomBinding;
import com.ardraw.ardrawingflow.ui.adapter.MainViewPager2Adapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding viewBinding;

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

        prepareData();
        attachEvents();
    }

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

    private void initializeBinding() {
        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 prepareData() {
        MainViewPager2Adapter pagerAdapter = new MainViewPager2Adapter(this);
        viewBinding.viewpager2.setAdapter(pagerAdapter);
    }

    private void attachEvents() {
        TabLayoutMediator mediator = new TabLayoutMediator(
                viewBinding.tabLayout,
                viewBinding.viewpager2,
                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());
        applyTabStyle(tabViewBinding, index);
    }

    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 ? R.color.black : R.color.gray;
                tabBinding.title.setTextColor(fetchColor(colorId));
            }

            private int fetchIconResource(int pos, boolean active) {
                int[] activeIcons = {R.drawable.category, R.drawable.add, R.drawable.favorite};
                int[] inactiveIcons = {R.drawable.discategory, R.drawable.disadd, R.drawable.disfavorite};
                return active ? activeIcons[pos] : inactiveIcons[pos];
            }
        };
    }

    private void applyTabStyle(TabCustomBinding tabBinding, int index) {
        TabConfig config = determineTabConfig(index);
        tabBinding.imageView.setImageResource(config.iconId);
        tabBinding.title.setText(config.label);
        tabBinding.title.setTextColor(fetchColor(config.textColorId));
    }

    private TabConfig determineTabConfig(int index) {
        switch (index) {
            case 0:
                return new TabConfig(R.drawable.category, "Category", R.color.black);
            case 1:
                return new TabConfig(R.drawable.disadd, "Upload", R.color.gray);
            case 2:
                return new TabConfig(R.drawable.disfavorite, "Favorite", R.color.gray);
            default:
                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 getResources().getColor(colorRes, null);
    }

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