package com.ar.arsketch.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.ar.arsketch.R;
import com.ar.arsketch.adapter.MainViewPager2Adapter;
import com.ar.arsketch.databinding.ActivityMainBinding;
import com.ar.arsketch.databinding.MainTabItemCustomBinding;
import com.ar.arsketch.topon.AdManager;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        AdManager.loadAllAd();

        initData();
        initEvent();

    }

    private void initData() {

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

    }

    private void initEvent() {

        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.import_image;
                    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.import_image);
                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;
    }
}