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.R;
import com.app.input.personalised.board.databean.KeyboardTheme;
import com.app.input.personalised.board.itemback.FavoriteRemovedListener;
import com.app.input.personalised.board.basedata.AppDatabase;
import com.app.input.personalised.board.basedata.ThemeRepository;
import com.app.input.personalised.board.databinding.ActivityFavoriteThemeBinding;
import com.app.input.personalised.board.listhelpers.FavoriteListAdapter;
import com.app.input.personalised.board.helpers.GridItemDecoration;

import java.util.List;

public class FavoriteThemeActivity extends BaseActivity implements FavoriteRemovedListener {
    private ActivityFavoriteThemeBinding vb;

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

        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(getString(R.string.likes));

        // 观察收藏数据
        AppDatabase.Companion.getBoartAppDatabase().ThemesDao().queryAllLike().observe(this, favoriteList -> {
            if (favoriteList != null && !favoriteList.isEmpty()) {
                List<KeyboardTheme> data = (List<KeyboardTheme>) favoriteList;
                GridItemDecoration gridItemDecoration = new GridItemDecoration(2, 2, 0);
                FavoriteListAdapter adapterMain = new FavoriteListAdapter(this);
                // 设置取消收藏的回调
                adapterMain.setRemoveLike(this);
                adapterMain.setForYouList(data);
                vb.recycler.setLayoutManager(new GridLayoutManager(FavoriteThemeActivity.this, 2));
                vb.recycler.setAdapter(adapterMain);
                vb.recycler.addItemDecoration(gridItemDecoration);
                // 显示列表，隐藏空状态
                vb.recycler.setVisibility(android.view.View.VISIBLE);
                vb.emptyTitle.setVisibility(android.view.View.GONE);
            } else {
                // 如果没有收藏，显示空状态
                vb.recycler.setAdapter(null);
                vb.recycler.setVisibility(android.view.View.GONE);
                vb.emptyTitle.setVisibility(android.view.View.VISIBLE);
            }
        });
    }

    @Override
    public void OnRemoveLike(KeyboardTheme data) {
        // 使用 Java 友好的方法删除收藏
        ThemeRepository.INSTANCE.removeLikeFromJava(data);
    }

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

