package com.fun.funnyprank.activity;

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 android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.ads.MaxInterstitialAd;
import com.fun.funnyprank.FunnyPrankSkin;
import com.fun.funnyprank.R;
import com.fun.funnyprank.adapter.ListAdapter;
import com.fun.funnyprank.data.LikeData;
import com.fun.funnyprank.data.MydataBase;
import com.fun.funnyprank.data.StaticValue;
import com.fun.funnyprank.databinding.ActivityListBinding;
import com.fun.funnyprank.tools.Adcallback;
import com.fun.funnyprank.tools.Mytools;

import java.util.ArrayList;
import java.util.List;

public class ListActivity extends AppCompatActivity {
    ActivityListBinding binding;
    private List<LikeData> dataList = new ArrayList<>();
    private String name;
    private int color;
    private static List<MaxInterstitialAd> adsList;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_list), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        adsList = Mytools.getAllAd();

        Intent intent = getIntent();
        name = intent.getStringExtra(StaticValue.KEY_CategoryName);
        color = intent.getIntExtra(StaticValue.KEY_Color, 0);
        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                dataList = MydataBase.getInstance().likeDao().getlist(name);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        binding.prewListRv.setAdapter(new ListAdapter(ListActivity.this, dataList, color));
                        binding.prewListRv.setLayoutManager(new GridLayoutManager(ListActivity.this, 2));
                    }
                });
            }
        });
        binding.prewListTitle.setText(name);
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAd();
            }
        });
    }


    private void showAd() {
        MaxInterstitialAd cachedAd = Mytools.onCache(adsList);

        if (cachedAd == null) {
            finish();
        } else {
            Mytools.setCallBcak(cachedAd, new Adcallback() {
                @Override
                public void onShowFail(MaxAd ad) {
                    finish();
                }

                @Override
                public void onAdHidden() {
                    finish();
                }
            });
            cachedAd.showAd(this);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        showAd();
    }
}