package com.free.life.wallpaper;

import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.free.life.wallpaper.bean.Category;
import com.free.life.wallpaper.topon.AdManager;
import com.free.life.wallpaper.utils.CommonUtils;
import com.free.life.wallpaper.utils.DealUtils;
import com.free.life.wallpaper.ox.DbManager;
import com.free.life.wallpaper.bean.Data;
import com.google.firebase.FirebaseApp;

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

public class MyWallpaper extends Application {
    public static Context mAppContext;

    private static List<Data> natureList;
    private static List<Data> animalsList;

    private static List<Data> experimentalList;
    private static List<Data> filmList;
    private static List<Data> patternsList;
    private static List<Data> streetList;

    private static List<Data> travelList;
    public static String TAG = "-------MyApp---------";
    public static Typeface defaultFont;

    private String APPId="h67b85139418a0";
    private String AppKey="a9864901d01b4c7dfbc4c5f066bf20d33";
//    private String debug_Key="";

    private String animasName = "Animals.json";
    private String exName = "experimental.json";
    private String filmName = "film.json";
    private String natureName = "Nature.json";
    private String pattName = "patterns.json";
    private String streetName = "street.json";
    private String travelName = "travel.json";
    private static List<Category>  data = new ArrayList<>();
    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = this;
        defaultFont = Typeface.createFromAsset(getAssets(), "custfont.ttf");
        DbManager.init(this);

        initTop();

//                        // 确保Firebase初始化
//        FirebaseApp.initializeApp(this);
//
//        // 输出日志以确认Firebase初始化
//        if (!FirebaseApp.getApps(this).isEmpty()) {
//            Log.d("TAG", "Firebase1 初始化成功1");
//        } else {
//            Log.e("TAG", "Firebase0 初始化失败0");
//        }

        String[] names = {animasName,exName,filmName,natureName,pattName,streetName,travelName};
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for(String name:names){
                        InputStream open = getAssets().open(name);
                        String covertStr = CommonUtils.getCovertStr(open);
                        if (!covertStr.isEmpty()) {
                            List<Data> data1 = DealUtils.INSTANCE.getData(covertStr);
                            Category category = new Category();
                             String realName = name.substring(0, name.indexOf("."));
                            category.setClassName(realName);
                            List<Data> data2 = data1.subList(0, 200);
                            Collections.shuffle(data2);
                            category.setDataList(data2);
                            data.add(category);

                        }
                    }
                    Log.d(MyWallpaper.TAG,"------------data complete");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    public static List<Category> getData() {
        return data;
    }



    private void initTop(){
        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

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

            }

            @Override
            public void onErrorCallback(String errorMsg) {
                Log.e(TAG, "onErrorCallback:" + errorMsg);
            }
        });

        ATSDK.init( this, APPId, AppKey);
        //测试工具
//        ATDebuggerUITest.showDebuggerUI(this,debug_Key);
        AdManager.loadAllAd();

    }

    public static Context getContext() {
        return mAppContext.getApplicationContext();
    }

}
