package com.fun.funnyprank.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.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

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

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;

    @SuppressLint("MissingInflatedId")
    @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();
//        setmainTabIcons(binding.mainTabLayout);
        setTabIcons(0);
    }

    private void initView() {
        PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager());
        binding.mainViewpager.setAdapter(pageAdapter);
        binding.mainTabLayout.setupWithViewPager(binding.mainViewpager);
        binding.mainViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

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

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void setTabIcons(final int position) {
        for (int i = 0; i < binding.mainTabLayout.getTabCount(); i++) {
            final TabLayout.Tab tab = binding.mainTabLayout.getTabAt(i);
            if (tab != null) {
                switch (i) {
                    case 0:
                        tab.setIcon(position == 0 ? R.drawable.main_check : R.drawable.main_uncheck);
                        break;
                    case 1:
                        tab.setIcon(position == 1 ? R.drawable.custom_check : R.drawable.custom_uncheck);
                        break;
                    case 2:
                        tab.setIcon(position == 2 ? R.drawable.rank_check : R.drawable.rank_uncheck);
                        break;
                    case 3:
                        tab.setIcon(position == 3 ? R.drawable.like_check : R.drawable.like_uncheck);
                        break;
                    default:
                        break;
                }
            }
        }
    }

    private void setmainTabIcons(TabLayout tabLayout) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                switch (i) {
                    case 0:
                        tab.setIcon(R.drawable.main_check);
                        break;
                    case 1:
                        tab.setIcon(R.drawable.like_uncheck);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}