package com.example.prettywallpapers;

import android.app.Application;
import android.util.Log;

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.example.prettywallpapers.base.Readmyfile;
import com.example.prettywallpapers.list.PrettyCategory;
import com.example.prettywallpapers.list.PrettyPaper;
import com.example.prettywallpapers.myvalue.MyValues;
import com.example.prettywallpapers.room.MyTool;
import com.example.prettywallpapers.room.PrettyPaperDatabase;
import com.example.prettywallpapers.room.PrettyPaperEntity;
import com.example.prettywallpapers.toponad.AdManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class PrettyWallPaper extends Application {
    private static List<PrettyCategory> prettyCategories;
    public static String TAG = "-----AD";
    private String AppId = "h66a246bc81483";
    private int i = 0;
    private String AppKey = "aa66c6b36a70016db7e34aae6bce0f9bd";
    @Override
    public void onCreate() {
        super.onCreate();
        MyValues.prettyWallPaper = this;
        initad();
        try {
            InputStream myfile = getAssets().open("prank_wallpaer.json");
            String mypapers = Readmyfile.getCovertStr(myfile);
            if(mypapers != null){
                Gson gson = new Gson();
                prettyCategories = gson.fromJson(mypapers,new TypeToken<List<PrettyCategory>>(){}.getType());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        setentity();
    }

    public void initad() {
        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

            @Override
            public void onResultCallback(boolean isEU) {
                Log.e(TAG, "onResultCallback:" + isEU);
                if (isEU && ATSDK.getGDPRDataLevel(PrettyWallPaper.this) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(MyValues.prettyWallPaper);
                }

            }

            @Override
            public void onErrorCallback(String errorMsg) {
                Log.e(TAG, "onErrorCallback:" + errorMsg);
            }
        });
        ATSDK.init( this, AppId, AppKey);
        AdManager.loadAllAd();
    }

    public static List<PrettyCategory> getCategorylist(){
        if (prettyCategories != null){
            return prettyCategories;
        }else {
            return prettyCategories;
        }
    }
    private void setentity() {
        while (i<prettyCategories.size()){
            Log.d("dhn","-------------fin"+i);
            PrettyCategory list = prettyCategories.get(i);
            List<PrettyPaper> data = list.getList();
            MyTool.runIO(new Runnable() {
                @Override
                public void run() {
                    for (PrettyPaper datalist : data) {
                        PrettyPaperEntity entity = new PrettyPaperEntity();
                        entity.setImage(datalist.getSourceUrl());
                        entity.setPaperlike(false);
                        PrettyPaperDatabase.getPrettyPaperDatabase().getPrettyPaperDao().insertLikeData(entity);
                        //SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().InsertSecurityEntity(entityApp);
                    }
                }
            });
            i++;
        }
    }

}
