package com.prank.funnypranksounds.ui.activity;

import android.content.Intent;
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.prank.funnypranksounds.R;
import com.prank.funnypranksounds.databinding.ActivityMainBinding;
import com.prank.funnypranksounds.databinding.TabCustomBinding;
import com.prank.funnypranksounds.ui.adapter.MainAdapter;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

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

        initData();
        initEvent();

    }

    private void initData() {

        MainAdapter adapter = new MainAdapter(this);
        binding.viewpager.setAdapter(adapter);

    }

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

        binding.setting.setOnClickListener(v -> {
            Intent intent = new Intent(this, SettingActivity.class);
            this.startActivity(intent);
        });

        new TabLayoutMediator(binding.tabLayout, binding.viewpager, (tab, position) -> {
            TabCustomBinding tabCustomBinding = TabCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabCustomBinding.getRoot());
            setTab(tabCustomBinding, 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) {
                    TabCustomBinding tabBinding = TabCustomBinding.bind(tab.getCustomView());

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

                    int textColor = isSelected ? getResources().getColor(R.color.yellow, null) : getResources().getColor(R.color.gray, null);
                    tabBinding.text.setTextColor(textColor);
                }
            }

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

        });

    }

    private void setTab(TabCustomBinding tabCustomBinding, int position) {
        switch (position) {
            case 0:
                tabCustomBinding.imageView.setImageResource(R.drawable.category);
                tabCustomBinding.text.setText(R.string.category);
                tabCustomBinding.text.setTextColor(getResources().getColor(R.color.yellow, null));
                break;
            case 1:
                tabCustomBinding.imageView.setImageResource(R.drawable.un_audio);
                tabCustomBinding.text.setText(R.string.audio);
                tabCustomBinding.text.setTextColor(getResources().getColor(R.color.gray, null));
                break;
            case 2:
                tabCustomBinding.imageView.setImageResource(R.drawable.un_collection);
                tabCustomBinding.text.setText(R.string.collection);
                tabCustomBinding.text.setTextColor(getResources().getColor(R.color.gray, null));
                break;
        }
    }


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