package com.fun.funnyprank.tools;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;


import androidx.annotation.NonNull;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.ads.MaxInterstitialAd;
import com.fun.funnyprank.FunnyPrankSkin;
import com.fun.funnyprank.data.LikeData;
import com.fun.funnyprank.data.Mydata;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Mytools {
    private static ExecutorService executorService;

    private static ArrayList<MaxInterstitialAd> adArrayList;
    private static final String one_AD = "9a30e00e3adce356";
    private static final String two_Ad = "8f3d0596a7b8f113";
    private static final String three_ad = "64e321a4bc6cc03d";

    public static void setCallBcak(MaxInterstitialAd ad, Adcallback adcallback) {
        ad.setListener(new MaxAdListener() {
            @Override
            public void onAdLoaded(@NonNull MaxAd maxAd) {
                Log.e("zzj", "-----------onAdLoaded------" + maxAd.getAdUnitId());
            }

            @Override
            public void onAdDisplayed(@NonNull MaxAd maxAd) {

            }

            @Override
            public void onAdHidden(@NonNull MaxAd maxAd) {
                adcallback.onAdHidden();

            }

            @Override
            public void onAdClicked(@NonNull MaxAd maxAd) {

            }

            @Override
            public void onAdLoadFailed(@NonNull String s, @NonNull MaxError maxError) {
                Log.e("zzj", "-----------onAdLoadFailed------" + maxError);
            }

            @Override
            public void onAdDisplayFailed(@NonNull MaxAd maxAd, @NonNull MaxError maxError) {
                adcallback.onShowFail(maxAd);
                Log.e("zzj", "-----------onAdDisplayFailed------" + maxError);
            }
        });
    }

    public static MaxInterstitialAd onCache(List<MaxInterstitialAd> list) {
        Collections.shuffle(list);
        for (MaxInterstitialAd ad : list) {
            if (ad.isReady()) {
                return ad;
            }
        }
        return null;
    }

    public static List<MaxInterstitialAd> getAllAd() {
        if (adArrayList == null) {
            adArrayList = new ArrayList<>();
            adArrayList.add(createAd(one_AD, FunnyPrankSkin.app));
            adArrayList.add(createAd(two_Ad, FunnyPrankSkin.app));
            adArrayList.add(createAd(three_ad, FunnyPrankSkin.app));
        }
        for (MaxInterstitialAd ad : adArrayList) {
            if (!ad.isReady()) {
                ad.loadAd();
            }
        }
        return adArrayList;
    }

    private static MaxInterstitialAd createAd(String adUnitId, Context context) {
        MaxInterstitialAd ad = new MaxInterstitialAd(adUnitId, context);
        return ad;
    }


    public static void runIO(Runnable task) {
        getExecutorService().execute(task);
    }

    private static ExecutorService getExecutorService() {
        if (executorService == null) {
            executorService = Executors.newSingleThreadExecutor();
        }
        return executorService;
    }

    private static JSONArray loadJSONFromAsset(String filename) {
        String json;
        try {
            AssetManager assetManager = FunnyPrankSkin.getAppContext().getAssets();
            InputStream inputStream = assetManager.open(filename);
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();
            json = new String(buffer, StandardCharsets.UTF_8);
            return new JSONArray(json);
        } catch (IOException | JSONException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static List<Mydata> parseJsonToList(String filename) {
        List<Mydata> dataList = new ArrayList<>();
        try {
            JSONArray jsonObject = loadJSONFromAsset(filename);
            for (int a = 0; a < jsonObject.length(); a++) {
                JSONObject list = jsonObject.getJSONObject(a);
                Mydata myData = new Mydata();
                List<LikeData> likeDataList = new ArrayList<>();
                myData.setCategoryName(list.getString("categoryName"));
                myData.setCategoryUrl(list.getString("categoryUrl"));
                String categoryName = list.getString("categoryName");
                String categoryUrl = list.getString("categoryUrl");
                JSONArray classArray = list.getJSONArray("list");
                for (int i = 0; i < classArray.length(); i++) {
                    JSONObject item = classArray.getJSONObject(i);
                    LikeData likeData = new LikeData();
                    likeData.setCategoryUrl(categoryUrl);
                    likeData.setIsrank(false);
                    likeData.setLike(false);
                    likeData.setCategoryname(categoryName);
                    likeData.setTitle(item.getString("title"));
                    likeData.setMp3Url(item.getString("mp3Url"));
                    likeData.setPreUrl(item.getString("preUrl"));
                    likeDataList.add(likeData);
                }
                myData.setList(likeDataList);
                dataList.add(myData);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return dataList;
    }
}
