package com.free.video.wallpaper.baseapp;

import android.app.Application;
import android.graphics.Typeface;

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.free.video.wallpaper.ad.AdManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.free.video.wallpaper.content.ResultData;
import com.free.video.wallpaper.helper.Commontool;
import com.free.video.wallpaper.helper.ObjectBoxManager;
import com.free.video.wallpaper.helper.Store;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyWallpaperApp extends Application {
    private static Typeface defaultFont;
    private static MyWallpaperApp mApplication;
    private static String thumbStr = "https://neutrolabgames.com/LiveLoop/CpanelPix/VideoThumb/";

    public static String TAG = "--------------------LIVE----------";
    public static String getThumbStr() {
        return thumbStr;
    }

    public static MyWallpaperApp getApplication() {
        return mApplication;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        defaultFont = Typeface.createFromAsset(getAssets(), "font.ttf");
        initSDk();
        ObjectBoxManager.init(this);
        String dbinit = Store.INSTANCE.getDbinit();
        if(dbinit.equals("0")){
            loadData();
        }

    }

    public static Typeface getDefaultFont() {
        return defaultFont;
    }

    private void loadData() {
          String name[] = {"trending.json","Explore.json","Shift.json"};
            ExecutorService cachedThreadPool = Executors.newFixedThreadPool(3);
              for (int i = 0;i<3;i++){
                  int task = i;
                  cachedThreadPool.execute(new Runnable() {
                      @Override
                      public void run() {
                          Gson gson = new Gson();
                          InputStream open = null;
                          try {
                              open = getAssets().open(name[task]);
                          }catch (Exception e){
                          }
                          if(open == null){
                              return;
                          }
                          Type type = new TypeToken<List<ResultData>>() {
                          }.getType();
                          String covertStr = Commontool.getCovertStr(open);
                          List<ResultData> data = gson.fromJson(covertStr, type);
                          for (ResultData datum : data) {
                              ObjectBoxManager.insertDb(datum);
                          }
                          Store.INSTANCE.setDbinit("1");
                      }
                  });
              }
    }



    private void initSDk(){
         String APPId ="h68382a065cfce";

         String AppKey ="a9b6ec097d76678a97c7cfcb0fb399ce3";
         String debugKey = "fe06c76b794f95216f98e5982ae8f335fc558956";
        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

            @Override
            public void onResultCallback(boolean isEU) {
                if (isEU && ATSDK.getGDPRDataLevel(MyWallpaperApp.this) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(MyWallpaperApp.this);
                }

            }

            @Override
            public void onErrorCallback(String errorMsg) {

            }
        });
        ATSDK.init( this, APPId, AppKey);
//        ATDebuggerUITest.showDebuggerUI(this,debugKey);
        AdManager.loadAllAd();
    }
}
