package com.auto.clicker.autoclicker.ui.activity.setting;

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.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.databinding.ActivitySettingBinding;
import com.auto.clicker.autoclicker.databinding.HowTabBinding;
import com.auto.clicker.autoclicker.ui.adapter.setting.SettingAdapter;
import com.google.android.material.tabs.TabLayoutMediator;

public class SettingActivity extends AppCompatActivity {
    private ActivitySettingBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivitySettingBinding.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;
        });

        initPagerAdapter();
        initTabs();

        binding.settingViewpager.setCurrentItem(0, false);
    }

    private void initPagerAdapter() {
        SettingAdapter adapter = new SettingAdapter(this);
        binding.settingViewpager.setAdapter(adapter);
    }

    private void initTabs() {
        new TabLayoutMediator(binding.settingTablayout, binding.settingViewpager, (tab, idx) -> {
            com.auto.clicker.autoclicker.databinding.HowTabBinding tabViewBinding = HowTabBinding.inflate(LayoutInflater.from(this));
            tab.setCustomView(tabViewBinding.getRoot());
            decorateTab(tabViewBinding, idx);
        }).attach();
    }

    private void decorateTab(HowTabBinding tabBinding, int position) {
        String text = "";
        if (position == 0) {
            text = "Actions";
        } else if (position == 1) {
            text = "UI Size";
        }
        tabBinding.textCustom.setText(text);
    }

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