package com.wall.snap.wallpaper.acctivitysnap;

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.snap.wallpaper.ad.AdsInsUtil;
import com.wall.snap.wallpaper.ad.LoadListener;
import com.wall.snap.wallpaper.ad.ShowListener;
import com.wall.snap.wallpaper.databinding.ActivityListsnapBinding;
import com.wall.snap.wallpaper.mysnapbean.MysnapData;
import com.wall.snap.wallpaper.toolsnap.ComUtilssnap;
import com.wall.snap.wallpaper.toolsnap.ObectBoxManagersnap;
import com.wall.snap.wallpaper.toolsnap.ItemHelpersnap;
import com.wall.snap.wallpaper.itemsnap.ItemsnapList;

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

import java.util.List;

public class ListsnapActivity extends BaseActivity implements com.wall.snap.wallpaper.connectorsnap.OnItemsnapClick {
    private ActivityListsnapBinding vb;

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

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

    private void initList() {
        List<MysnapData> data = ObectBoxManagersnap.queryCategoryAll(categoryName);
        ItemHelpersnap itemHelpertwo = new ItemHelpersnap(10, 10, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        vb.list.addItemDecoration(itemHelpertwo);
        vb.list.setLayoutManager(gridLayoutManager);
        int height = ComUtilssnap.dp2Px(200);
        ItemsnapList itemList = new ItemsnapList(this, height,false);
        itemList.setDataInfo(data);
        itemList.setInfoWallpaperListener(this);
        vb.list.setAdapter(itemList);

    }

    @Override
    public void onItemClickAction(MysnapData info) {
        Intent intent = new Intent(this, WallpapersnapActivity.class);
        intent.putExtra(ComUtilssnap.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) {

            }
        });
    }
}