package com.wallpaper.perfectwallpaper.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 androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.wallpaper.perfectwallpaper.R;
import com.wallpaper.perfectwallpaper.adapter.MainViewPager2Adapter;
import com.wallpaper.perfectwallpaper.databinding.ActivityMainBinding;
import com.wallpaper.perfectwallpaper.databinding.MainTabItemCustomBinding;
import com.wallpaper.perfectwallpaper.fragment.CategoryFragment;
import com.wallpaper.perfectwallpaper.fragment.FavoriteFragment;
import com.wallpaper.perfectwallpaper.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

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() {


        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new CategoryFragment());
        fragmentList.add(new FavoriteFragment());

        MainViewPager2Adapter adapter = new MainViewPager2Adapter(this,fragmentList);
        binding.mainViewpager.setAdapter(adapter);

    }

    private void initEvent() {

        binding.mainViewpager.setUserInputEnabled(false);

        new TabLayoutMediator(binding.mainTablayout, binding.mainViewpager, (tab, position) -> {
            MainTabItemCustomBinding tabBinding = MainTabItemCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabBinding.getRoot());
            setTabIconAndDotVisibility(tabBinding, position);
        }).attach();

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

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

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

            private void updateTabIcon(TabLayout.Tab tab, boolean isSelected) {
                if (tab.getCustomView() != null) {
                    MainTabItemCustomBinding tabBinding = MainTabItemCustomBinding.bind(tab.getCustomView());
                    int iconResId = getIconResource(tab.getPosition(), isSelected);
                    tabBinding.iconCustom.setImageResource(iconResId);
                }
            }

            private int getIconResource(int position, boolean isSelected) {
                switch (position) {
                    case 1:
                        return R.drawable.category;
                    case 2:
                        return R.drawable.favorite;
                    default:
                        return R.drawable.home;
                }
            }

        });

    }

    private void setTabIconAndDotVisibility(MainTabItemCustomBinding tabBinding, int position) {
        switch (position) {
            case 0:
                tabBinding.iconCustom.setImageResource(R.drawable.home);
                tabBinding.dotView.setVisibility(View.VISIBLE);
                break;
            case 1:
                tabBinding.iconCustom.setImageResource(R.drawable.category);
                break;
            case 2:
                tabBinding.iconCustom.setImageResource(R.drawable.favorite);
                break;
            default:
                tabBinding.iconCustom.setImageResource(R.drawable.home);
                break;
        }
    }

    private void showDot(TabLayout.Tab tab) {
        View dotView = tab.view.findViewById(R.id.dot_view);
        if (dotView != null) {
            dotView.setVisibility(View.VISIBLE);
        }
    }

    private void hideDot(TabLayout.Tab tab) {
        View dotView = tab.view.findViewById(R.id.dot_view);
        if (dotView != null) {
            dotView.setVisibility(View.GONE);
        }
    }

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