package com.fun.airhornprank.activity;

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.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.fun.airhornprank.R;
import com.fun.airhornprank.adapter.PageAdapter;
import com.fun.airhornprank.databinding.ActivityMainBinding;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;

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

        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();

        setTab(0);
    }

    private void initView() {

        binding.mainPager.setAdapter(new PageAdapter(getSupportFragmentManager()));
        binding.mainTab.setupWithViewPager(binding.mainPager);
        binding.mainPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void setTab(int position) {
        for (int i = 0; i < binding.mainTab.getTabCount(); i++) {
            final TabLayout.Tab tab = binding.mainTab.getTabAt(i);
            if (tab != null) {
                switch (i) {
                    case 0 ->
                            tab.setCustomView(position == 0 ? R.layout.main_check_cons : R.layout.main_uncheck_cons);
                    case 1 ->
                            tab.setCustomView(position == 1 ? R.layout.custom_check_cons : R.layout.custom_uncheck_cons);
                    case 2 ->
                            tab.setCustomView(position == 2 ? R.layout.rank_check_cons : R.layout.rank_uncheck_cons);
                    case 3 ->
                            tab.setCustomView(position == 3 ? R.layout.like_check_cons : R.layout.like_uncheck_cons);
                    default -> {
                    }
                }
            }
        }
    }

}