package com.key.coolkeyboard.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.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.google.android.material.tabs.TabLayout;
import com.key.coolkeyboard.CoolKeyboardSkin;
import com.key.coolkeyboard.R;
import com.key.coolkeyboard.adapter.PageAdapter;
import com.key.coolkeyboard.databinding.ActivityAllBinding;
import com.key.coolkeyboard.fragment.AllFragment;
import com.key.coolkeyboard.tool.Adcallback;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.topon.AdManager;
import com.key.coolkeyboard.topon.onActionListener;
import com.vungle.ads.Ad;

import java.util.ArrayList;
import java.util.List;

public class AllActivity extends AppCompatActivity {
    ActivityAllBinding binding;
    private String[] listtext = {"ALL", "Aesthetic", "Cool", "Cute", "Festival", "Love", "Neon", "Shining", "Super Theme"};
    private PageAdapter adapter;
    private List<Fragment> list = new ArrayList<>();
    private TextView tabtext;
    private List<ATInterstitial> ads;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAllBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

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

        AdManager.loadAllAd();

        initView();

        initEvent();

    }




    private void initEvent() {
        binding.allBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.allSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AllActivity.this, SerachActivity.class);
                startActivity(intent);

            }
        });
    }

    private void initView() {
        for (String s : listtext) {
            list.add(AllFragment.newInstance(s));
        }

        adapter = new PageAdapter(getSupportFragmentManager(), list);

        binding.allViewpager.setAdapter(adapter);

        for (int i = 0; i < 9; i++) {
            TabLayout.Tab tab = binding.allTabLayout.newTab();
            binding.allTabLayout.addTab(tab);
            View customView = LayoutInflater.from(this).inflate(R.layout.item_tablayout, null);
            tabtext = customView.findViewById(R.id.tab_text);
            tabtext.setText(listtext[i]);
            customView.setBackgroundResource(R.drawable.all_selector);
            tab.setCustomView(customView);
//            tab.setCustomView(LayoutInflater.from(this).inflate(R.layout.item_tablayout, null));
            final int finalI = i;
            tab.view.setOnClickListener(v -> {
                binding.allViewpager.setCurrentItem(finalI);
            });
        }

        binding.allViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTabIcons(position);
                binding.allTabLayout.setScrollPosition(position, 0, true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void setTabIcons(int position) {
        for (int i = 0; i < binding.allTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = binding.allTabLayout.getTabAt(i);
            if (tab != null) {
                tab.getCustomView().findViewById(R.id.tab_text).setSelected(i == position);
                View customView = tab.getCustomView();
                if (i == position) {
                    customView.setBackgroundResource(R.drawable.all_tab_check);
                } else {
                    customView.setBackgroundResource(R.drawable.all_tab_uncheck);
                }
            }
        }

    }


}