package com.ar.ardrawingboard.ui.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.ar.ardrawingboard.R;
import com.ar.ardrawingboard.topon.AdManager;
import com.ar.ardrawingboard.ui.adapter.MainAdapter;
import com.ar.ardrawingboard.databinding.ActivityMainBinding;
import com.ar.ardrawingboard.databinding.MainTabItemCustomBinding;
import com.ar.ardrawingboard.upload.Http;
import com.ar.ardrawingboard.upload.SaveUtils;
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);
        initializeViewBinding();
        configureViewPager();
        setupTabNavigation();

        upload();
    }

    private void initializeViewBinding() {
        viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());
    }

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

        MainAdapter viewPagerAdapter = new MainAdapter(this);
        viewBinding.mainViewpager.setAdapter(viewPagerAdapter);
    }

    private void setupTabNavigation() {
        configureTabMediator();
        attachTabSelectionListener();
    }

    private void configureTabMediator() {
        new TabLayoutMediator(viewBinding.mainTablayout, viewBinding.mainViewpager, (tab, position) -> {
            MainTabItemCustomBinding customTabBinding = MainTabItemCustomBinding.inflate(
                    LayoutInflater.from(MainActivity.this));
            tab.setCustomView(customTabBinding.getRoot());
            applyTabStyle(customTabBinding, position);
        }).attach();
    }

    private void applyTabStyle(MainTabItemCustomBinding tabBinding, int index) {
        int iconResource;
        boolean showDot = false;

        switch (index) {
            case 0:
                iconResource = R.drawable.home;
                showDot = true;
                break;
            case 1:
                iconResource = R.drawable.import_image;
                break;
            case 2:
                iconResource = R.drawable.collection;
                break;
            default:
                iconResource = R.drawable.home;
                break;
        }

        tabBinding.iconCustom.setImageResource(iconResource);
        tabBinding.dotView.setVisibility(showDot ? View.VISIBLE : View.GONE);
    }

    private void attachTabSelectionListener() {
        viewBinding.mainTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab selectedTab) {
                updateDotVisibility(selectedTab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab unselectedTab) {
                updateDotVisibility(unselectedTab, false);
            }

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

            private void updateDotVisibility(TabLayout.Tab tab, boolean isVisible) {
                View dot = tab.view.findViewById(R.id.dot_view);
                if (dot != null) {
                    dot.setVisibility(isVisible ? View.VISIBLE : View.GONE);
                }
            }
        });
    }

    private void  upload(){
        boolean post = SaveUtils.INSTANCE.isPost();
        if(!post){
            Http.INSTANCE.makeGetRequest(MainActivity.this);
            SaveUtils.INSTANCE.setPost(true);
        }
    }
}