package com.app.input.personalised.board.boartactivity;

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 kotlin.Unit;
import kotlin.jvm.functions.Function0;

import com.ad.tradpluslibrary.TPAdManager;
import com.app.input.personalised.board.BoartApp;
import com.app.input.personalised.board.R;
import com.app.input.personalised.board.databean.KeyboardTheme;
import com.app.input.personalised.board.databean.ThemeCategory;
import com.app.input.personalised.board.databinding.ActivityThemeListBinding;
import com.app.input.personalised.board.listhelpers.RecommendedGridAdapter;
import com.app.input.personalised.board.helpers.GridItemDecoration;

import java.util.List;

public class ThemeListActivity extends BaseActivity {
    private ActivityThemeListBinding vb;
    public static final String KEY_NAME = "class_name";
    private String name;
    private List<KeyboardTheme> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityThemeListBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(vb.getRoot());
        TPAdManager.INSTANCE.loadAllAd(this);
        com.app.input.personalised.board.helpers.StatusBarHelper.INSTANCE.setTransparentStatusBar(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();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 确保状态栏始终为透明
        com.app.input.personalised.board.helpers.StatusBarHelper.INSTANCE.setTransparentStatusBar(this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            // 窗口获得焦点时，确保状态栏为透明
            com.app.input.personalised.board.helpers.StatusBarHelper.INSTANCE.setTransparentStatusBar(this);
        }
    }

    private void initData() {
        vb.className.setText(name);
        for (ThemeCategory beanWrapper : BoartApp.list) {
            if (beanWrapper.getParentName().equals(name)) {
                data = beanWrapper.getKeyboardList();
            }
        }
        GridItemDecoration gridItemDecoration = new GridItemDecoration(2, 2, 0);
        RecommendedGridAdapter adapterMain = new RecommendedGridAdapter(this);
        adapterMain.setForYouList(data);
        vb.recycler.setLayoutManager(new GridLayoutManager(ThemeListActivity.this, 2));
        vb.recycler.setAdapter(adapterMain);
        vb.recycler.addItemDecoration(gridItemDecoration);

    }

    private void initClick() {
        vb.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInterceptBackPressed();
            }
        });
    }

    @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();
    }
}