package com.wall.dream.wallpapers.acacctivity;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import com.tradplus.ads.base.bean.TPAdInfo;
import com.wall.dream.wallpapers.ad.AdsInsUtil;
import com.wall.dream.wallpapers.ad.LoadListener;
import com.wall.dream.wallpapers.ad.ShowListener;
import com.wall.dream.wallpapers.databinding.ViewListBinding;
import com.wall.dream.wallpapers.GJ.ComUtilsGJ;
import com.wall.dream.wallpapers.GJ.ObectBoxManagerGJ;
import com.wall.dream.wallpapers.GJ.ItemHelperGJ;
import com.wall.dream.wallpapers.LianXi.OnItemXianClick;
import com.wall.dream.wallpapers.data.DataBase;
import com.wall.dream.wallpapers.ItemPag.ItemListPag;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class ListViewActivity extends BaseActivity implements OnItemXianClick {
    private ViewListBinding vb;

    private String categoryName;
    public static final String key_index = "index";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ViewListBinding.inflate(getLayoutInflater());
        setContentView(vb.getRoot());
        ComUtilsGJ.initFull(this, false);
        categoryName = getIntent().getStringExtra(key_index);
        // 显示时隐藏后面的Good
        String displayName = removeGoodSuffix(categoryName);
        vb.name.setText(displayName);
        initList();
        vb.imageviewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInterceptBackPressed();
            }
        });
    }

    private void initList() {
        List<DataBase> data = ObectBoxManagerGJ.queryCategoryAll(categoryName);
        // 减少间距，让图片挨在一起
        ItemHelperGJ itemHelperGJ = new ItemHelperGJ(4, 4, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        vb.list.addItemDecoration(itemHelperGJ);
        vb.list.setLayoutManager(gridLayoutManager);
        // 增加图片高度，让图片更大
        int height = ComUtilsGJ.dp2Px(240);
        ItemListPag itemListPag = new ItemListPag(this, height,false);
        itemListPag.setDataInfo(data);
        itemListPag.setInfoWallpaperListener(this);
        vb.list.setAdapter(itemListPag);

    }

    /**
     * 移除分类名称后面的"Good"后缀（不区分大小写）
     */
    private String removeGoodSuffix(String categoryName) {
        if (categoryName == null || categoryName.isEmpty()) {
            return categoryName;
        }
        // 使用正则表达式不区分大小写地移除末尾的"Good"
        return categoryName.replaceAll("(?i)good$", "");
    }

    @Override
    public void onItemClickAction(DataBase info) {
        Intent intent = new Intent(this, WallpaperViewActivity.class);
        intent.putExtra(ComUtilsGJ.key_info, info);
        startActivity(intent);
    }



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

    @Override
    protected void onInterceptBackPressed() {
        AdsInsUtil.INSTANCE.showAd(this, AdsInsUtil.Placement.TOP_ON_AD_TOW, new ShowListener() {
            @Override
            public void onAdShown(@Nullable TPAdInfo ad) {
                loadAd();
            }

            @Override
            public void onAdClosed() {
                loadAd();
            }
        });
        if (getBackPressedCallback() != null) {
            getBackPressedCallback().setEnabled(false);
        }
        getOnBackPressedDispatcher().onBackPressed();
    }

    private void loadAd(){
        AdsInsUtil.INSTANCE.loadAd(this, AdsInsUtil.Placement.TOP_ON_AD_TOW, new LoadListener() {
            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
    }
}