package com.ar.imagetracear.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.ar.imagetracear.topon.AdManager;
import com.ar.imagetracear.upload.Http;
import com.ar.imagetracear.upload.SaveUtils;
import com.ar.imagetracear.R;
import com.ar.imagetracear.databinding.ActivityMainBinding;
import com.ar.imagetracear.databinding.MainTabItemCustomBinding;
import com.ar.imagetracear.adapter.MainAdapter;
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 state) {
        super.onCreate(state);

        EdgeToEdge.enable(this);

        bindLayout();

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

        AdManager.loadAllAd();

        initPagerAdapter();
        initTabs();

        upload();
    }

    private void bindLayout() {
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    private void initPagerAdapter() {
        MainAdapter adapter = new MainAdapter(this);
        binding.mainViewpager.setAdapter(adapter);
    }

    private void initTabs() {
        bindTabIcons();
        addTabListener();
    }

    private void bindTabIcons() {
        new TabLayoutMediator(binding.mainTablayout, binding.mainViewpager, (tab, idx) -> {
            MainTabItemCustomBinding tabViewBinding = MainTabItemCustomBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabViewBinding.getRoot());
            decorateTab(tabViewBinding, idx);
        }).attach();
    }

    private void decorateTab(MainTabItemCustomBinding tabBinding, int position) {
        int iconId = R.drawable.home;
        boolean dotVisible = false;

        if (position == 0) {
            iconId = R.drawable.home;
            dotVisible = true;
        } else if (position == 1) {
            iconId = R.drawable.import_image;
        } else if (position == 2) {
            iconId = R.drawable.collection;
        }

        tabBinding.iconCustom.setImageResource(iconId);
        tabBinding.dotView.setVisibility(dotVisible ? View.VISIBLE : View.GONE);
    }

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

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

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // Optional: no action
            }

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

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

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