package com.app.wave.keyboard.ui;

import android.os.Bundle;
import android.view.View;

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.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

import com.ad.tradpluslibrary.TPAdManager;
import com.app.wave.keyboard.App;
import com.app.wave.keyboard.R;
import com.app.wave.keyboard.bean.DetailsBean;
import com.app.wave.keyboard.bean.WrapperBean;
import com.app.wave.keyboard.databinding.ActivityMoreBinding;
import com.app.wave.keyboard.adapter.CategoryItemAdapter;
import com.app.wave.keyboard.utils.ItemDeco;
import android.graphics.Typeface;
import java.util.List;

public class MoreActivity extends BaseActivity {

    private ActivityMoreBinding vb;
    public static final String KEY_NAME = "class_name";
    private String name;
    private List<DetailsBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityMoreBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(vb.getRoot());
        TPAdManager.INSTANCE.loadAllAd(this);
        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;
        });

        name = getIntent().getStringExtra(KEY_NAME);
        initData();
        initClick();
        setupScrollListener(); // 添加滚动监听
    }

    private void initData() {
        vb.className.setText(name);
        vb.className.setTypeface(vb.className.getTypeface(), Typeface.BOLD);
        for (WrapperBean wrapperBean : App.list) {
            if (wrapperBean.getParentName().equals(name)) {
                data = wrapperBean.getKeyboardList();
            }
        }

        ItemDeco itemDeco = new ItemDeco(3, 3, 0);
        CategoryItemAdapter adapterMain = new CategoryItemAdapter(this, data);
        adapterMain.setAdaptiveWidthMode(3);

        vb.recycler.setLayoutManager(new GridLayoutManager(MoreActivity.this, 3));
        vb.recycler.setAdapter(adapterMain);
        vb.recycler.addItemDecoration(itemDeco);
    }

    private void initClick() {
        // 返回按钮点击
        vb.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInterceptBackPressed();
            }
        });

        // 回到顶部按钮点击
        vb.btnScrollToTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToTop();
            }
        });
    }

    // 设置滚动监听器
    private void setupScrollListener() {
        vb.recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                // 检查是否可以向上滚动
                boolean canScrollUp = recyclerView.canScrollVertically(-1);
                vb.btnScrollToTop.setVisibility(canScrollUp ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                // 滚动停止时检查一次，确保状态正确
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    boolean canScrollUp = recyclerView.canScrollVertically(-1);
                    vb.btnScrollToTop.setVisibility(canScrollUp ? View.VISIBLE : View.GONE);
                }
            }
        });

        // 初始检查一次，如果一开始就可以向上滚动，就显示按钮
        vb.recycler.post(new Runnable() {
            @Override
            public void run() {
                boolean canScrollUp = vb.recycler.canScrollVertically(-1);
                vb.btnScrollToTop.setVisibility(canScrollUp ? View.VISIBLE : View.GONE);
            }
        });
    }

    // 回到顶部方法
    private void scrollToTop() {
        if (vb.recycler.getLayoutManager() != null) {
            // 平滑滚动到顶部
            vb.recycler.smoothScrollToPosition(0);
        }
    }


    @Override
    protected boolean shouldInterceptBackPress() {
        return true;
    }

    @Override
    protected void onInterceptBackPressed() {

        TPAdManager.INSTANCE.showTPAD(this, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                return null;
            }
        });
        if (getBackPressedCallback() != null) {
            getBackPressedCallback().setEnabled(false);
        }
        getOnBackPressedDispatcher().onBackPressed();
    }
}