package com.hd.live.wallpapers;

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

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hd.live.wallpapers.ad.AdManager;
import com.hd.live.wallpapers.bean.ResultData;
import com.hd.live.wallpapers.others.ComUtils;
import com.hd.live.wallpapers.others.DbUtils;
import com.hd.live.wallpapers.others.SaveUtils;
import com.hd.live.wallpapers.post.Http;

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

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

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

    public static LiveApp getApplication() {
        return mApplication;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        defaultFont = Typeface.createFromAsset(getAssets(), "font.ttf");
        initSDk();
        DbUtils.init(this);
        String dbinit = SaveUtils.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() {
//                          Common.logMsg("执行任务 "+task+"---开始--"+Thread.currentThread().getName());
                          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 = ComUtils.getCovertStr(open);
                          List<ResultData> data = gson.fromJson(covertStr, type);
                          for (ResultData datum : data) {
                              DbUtils.insertDb(datum);
                          }
                          SaveUtils.INSTANCE.setDbinit("1");
//                          Common.logMsg("执行任务 "+task+"---完成--"+Thread.currentThread().getName());
                      }
                  });
              }
    }



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

         String AppKey ="aab8950d76a24d7df4fcd16e67a6117bf";
         String debugKey = "4994d5dce29b75386e6909a8bbad6e0a933cde1f";
        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

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

            }

            @Override
            public void onErrorCallback(String errorMsg) {

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