package com.hd.live.wall.wallpapers;

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

import com.ad.toponlibrary.AdManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hd.live.wall.wallpapers.dbData.WallData;
import com.hd.live.wall.wallpapers.utils.helper;
import com.hd.live.wall.wallpapers.utils.dbManager;
import com.hd.live.wall.wallpapers.utils.Store;
import com.up.uploadlibrary.UpLoadManager;

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 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");
        AdManager.INSTANCE.init(TAG,this,"h689af270407f0","a4346235307c4088aa524b4de82d6d5d9",
                "9788519403e27aa6420a9b9ad49bc0fc76166bea",
                "n689af2ae7b6d2",
                "n689af2ae2ee3b",
                "n689af2ade4f28",false);

        UpLoadManager.INSTANCE.init(this,TAG);
        dbManager.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<WallData>>() {
                          }.getType();
                          String covertStr = helper.getCovertStr(open);
                          List<WallData> data = gson.fromJson(covertStr, type);
                          for (WallData datum : data) {
                              dbManager.insertDb(datum);
                          }
                          Store.INSTANCE.setDbinit("1");
                      }
                  });
              }
    }




}
