package com.live.mylivewallpaper;

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

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.live.mylivewallpaper.data.ResultData;
import com.live.mylivewallpaper.help.Common;
import com.live.mylivewallpaper.help.Db;
import com.live.mylivewallpaper.help.Shared;

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 App extends Application {
    private static Typeface defaultFont;
    private static App mApplication;
    private static String thumbStr = "https://neutrolabgames.com/LiveLoop/CpanelPix/VideoThumb/";


    public static String getThumbStr() {
        return thumbStr;
    }

    public static App getApplication() {
        return mApplication;
    }

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



    }
}
