package com.key.simplekeyboard.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.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.key.simplekeyboard.R;

import com.key.simplekeyboard.adapter.PageAdapter;
import com.key.simplekeyboard.data.MydataBase;
import com.key.simplekeyboard.data.Otherdata;
import com.key.simplekeyboard.fragment.AllFragment;
import com.key.simplekeyboard.fragment.LikeFragment;
import com.key.simplekeyboard.tools.Mytools;

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

public class AllActivity extends AppCompatActivity {

    private ImageView back, search;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private TextView tabtext, title;
    private ImageView tabimg;
    private String[] listtext = {"ALL", "Love", "Cool", "Cute", "Neon", "Festival", "Gravity", "Aesthetic", "Super Theme"};
    private List<Fragment> list = new ArrayList<>();
    private TypedArray imageArray;
    private PageAdapter adapter;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all);
        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;
        });
        adapter = new PageAdapter(getSupportFragmentManager(), list);
        initView();

        initEvent();
        imageArray = getResources().obtainTypedArray(R.array.drawable_images);


        viewPager.setAdapter(adapter);
        for (int i = 0; i < 9; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tabLayout.addTab(tab);
            View customView = LayoutInflater.from(this).inflate(R.layout.custom_tablayout, null);
            tabimg = customView.findViewById(R.id.tab_image);
            tabtext = customView.findViewById(R.id.tab_text);
            tabtext.setText(listtext[i]);
            customView.setBackgroundResource(R.drawable.all_selector);
            Drawable drawable = imageArray.getDrawable(i);
            tabimg.setBackground(drawable);
            tab.setCustomView(customView);
            final int finalI = i;
            tab.view.setOnClickListener(v -> {
                viewPager.setCurrentItem(finalI);
            });
        }

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

            }

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

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    private void setTabIcons(int position) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.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);
                }
            }
        }

    }

    private void initEvent() {
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AllActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        search = findViewById(R.id.all_search);
        back = findViewById(R.id.all_back);
        tabLayout = findViewById(R.id.all_tabLayout);
        viewPager = findViewById(R.id.all_viewpager);
        title = findViewById(R.id.all_title);
        title.setTypeface(Typeface.createFromAsset(getAssets(), "DenkOne-Regular.ttf"));
        for (String s : listtext) {
            list.add(AllFragment.newInstance(s));
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (imageArray != null) {
            imageArray.recycle();
        }
    }
}