package com.lc.myapplication.security.mysoundstwo;

import android.app.Application;
import android.content.Context;
import android.util.ArraySet;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lc.myapplication.security.mysoundstwo.list.SoundsData;
import com.lc.myapplication.security.mysoundstwo.list.SoundsList;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeDatabase;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeEntity;
import com.lc.myapplication.security.mysoundstwo.tool.MyTool;
import com.lc.myapplication.security.mysoundstwo.value.MyValue;

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

public class ScaryPrank extends Application {
    private static List<SoundsList> soundslist;
    private int i = 0;
    @Override
    public void onCreate() {
        super.onCreate();
        MyValue.scaryPrank = this;
        try {
            InputStream openlist = getAssets().open("prank.json");
            String str = MyTool.getCovertStr(openlist);
            if (!str.isEmpty()){
                Gson gson = new Gson();
                soundslist = gson.fromJson(str,new TypeToken<List<SoundsList>>(){
                }.getType());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        setentity();
    }

    private void setentity() {
        while (i<soundslist.size()){
            Log.d("dhn","-------------fin"+i);
            SoundsList list = soundslist.get(i);
            List<SoundsData> data = list.getList();
                MyTool.RunIO(new Runnable() {
                    @Override
                    public void run() {
                        for (SoundsData datalist : data) {
                            ScaryPrankLikeEntity entity = new ScaryPrankLikeEntity();
                            entity.setName(datalist.getTitle());
                            entity.setImage(datalist.getPreUrl());
                            entity.setMp3url(datalist.getMp3Url());
                            entity.setSoundslike(false);
                            entity.setResource(0);
                            ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().insertLikeData(entity);
                            //SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().InsertSecurityEntity(entityApp);
                        }
                    }
                });
            i++;
        }
    }

    public static List<SoundsList> getList(){
        if (soundslist != null){
            return soundslist;
        }
        return soundslist;
    }
}
