package com.example.wallisyn.Activity;

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

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

import com.example.wallisyn.ad.AdsInsUtil;
import com.example.wallisyn.ad.LoadListener;
import com.example.wallisyn.ad.ShowListener;
import com.example.wallisyn.databinding.ActivityWallpaperListBinding;
import com.example.wallisyn.databinding.ActivityWallpaperListBinding;
import com.example.wallisyn.tool.ComUtils;
import com.example.wallisyn.tool.ObectBoxManager;
import com.example.wallisyn.tool.ItemHelper;
import com.example.wallisyn.connector.OnItemClick;
import com.example.wallisyn.mybean.MyData;
import com.example.wallisyn.item.ListWallpaperItem;
import com.thinkup.core.api.TUAdInfo;

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

import java.util.List;
import java.util.Objects;

public class WallpaperListActivity extends BaseActivity implements OnItemClick {
    private ActivityWallpaperListBinding vb;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityWallpaperListBinding.inflate(getLayoutInflater());
        setContentView(vb.getRoot());
        ComUtils.initFull(this, false);
        categoryName = getIntent().getStringExtra(key_index);
        vb.name.setText(categoryName);
        initList();
        vb.imageviewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initList() {
        List<MyData> data = ObectBoxManager.queryCategoryAll(categoryName);
        ItemHelper itemHelper = new ItemHelper(10, 10, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        vb.list.addItemDecoration(itemHelper);
        vb.list.setLayoutManager(gridLayoutManager);
        int height = ComUtils.dp2Px(180);
        ListWallpaperItem itemList = new ListWallpaperItem(this, height, false);
        itemList.setDataInfo(data);
        itemList.setInfoWallpaperListener(this);
        vb.list.setAdapter(itemList);

    }

    @Override
    public void onItemClickAction(MyData info) {
        Intent intent = new Intent(this, WallpaperActivity.class);
        intent.putExtra(ComUtils.key_info, info);
        startActivity(intent);
    }

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

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

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

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

            }
        });
    }
}