package com.lc.myapplication.security.mysoundstwo.tool;

import android.content.SharedPreferences;
import android.util.Log;

import com.lc.myapplication.security.mysoundstwo.list.SoundsData;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeDatabase;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyTool {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private static ExecutorService executorService;
    public static String getCovertStr(InputStream stream) {
        String covertStr = "";
        try {
            StringWriter writer = new StringWriter();
            char[] buffer = new char[stream.available()];
            Reader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
            int a = 0;
            while ((a = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, a);
            }
            covertStr = writer.toString();
        } catch (IOException e) {
            return covertStr;
        }
        return covertStr;
    }
    private static ExecutorService getExecutorService(){
        if (executorService == null){
            executorService = Executors.newSingleThreadExecutor();
        }
        return executorService;
    }

    public static void RunIO(Runnable task){
        getExecutorService().execute(task);
    }
    public static void insertEntity(List<SoundsData> list) {
        Log.d("--------size","----size"+list.size());
        MyTool.RunIO(new Runnable() {
            @Override
            public void run() {
                for (SoundsData datalist : list) {
                    ScaryPrankLikeEntity entity = new ScaryPrankLikeEntity();
                    entity.setName(datalist.getTitle());
                    entity.setImage(datalist.getPreUrl());
                    entity.setMp3url(datalist.getMp3Url());
                    entity.setSoundslike(false);
                    ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().insertLikeData(entity);
                    //SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().InsertSecurityEntity(entityApp);
                }
            }
        });
    }


}
