package com.wall.turing.wallpapers.accctivities;

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.turing.wallpapers.ad.AdsInsUtil;
import com.wall.turing.wallpapers.ad.LoadListener;
import com.wall.turing.wallpapers.ad.ShowListener;
import com.wall.turing.wallpapers.databinding.TuringListBinding;
import com.wall.turing.wallpapers.uutils.AppUtils;
import com.wall.turing.wallpapers.uutils.DatabaseManager;
import com.wall.turing.wallpapers.uutils.AdapterHelper;
import com.wall.turing.wallpapers.connect.TuringItemClick;
import com.wall.turing.wallpapers.myselfbean.Wallpaper;
import com.wall.turing.wallpapers.adapteres.ListAdapter;

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

import java.util.List;

public class TuringListActivity extends BaseActivity implements TuringItemClick {
    private TuringListBinding vb;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = TuringListBinding.inflate(getLayoutInflater());
        setContentView(vb.getRoot());
        AppUtils.initFull(this, false);
        categoryName = getIntent().getStringExtra(key_index);
        // 显示时去掉"Turing"后缀
        String displayName = categoryName.replace("Turing", "").replace("Tuirng", "");
        vb.name.setText(displayName);
        initList();
        vb.imageviewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initList() {
        List<Wallpaper> data = DatabaseManager.queryCategoryAll(categoryName);
        AdapterHelper adapterHelper = new AdapterHelper(10, 10, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        vb.list.addItemDecoration(adapterHelper);
        vb.list.setLayoutManager(gridLayoutManager);
        int height = AppUtils.dp2Px(180);
        ListAdapter listAdapter = new ListAdapter(this, height,false);
        listAdapter.setDataInfo(data);
        listAdapter.setInfoWallpaperListener(this);
        vb.list.setAdapter(listAdapter);

    }

    @Override
    public void onItemClickAction(Wallpaper info) {
        Intent intent = new Intent(this, TuringWallpaperActivity.class);
        intent.putExtra(AppUtils.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 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_ONE, new LoadListener() {
            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
    }
}