package com.setqq.script;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;

import com.saki.log.SQLog;
import com.saki.qq.global.ScriptManager;
import com.saki.service.NewService;
import com.saki.sqv9.R;
import com.saki.tools.BitmapCallback;
import com.saki.tools.BitmapUtil;
import com.saki.tools.FileUtil;
import com.saki.tools.HttpUtil;
import com.setqq.script.javascript.JavaScript;
import com.setqq.script.lua.Lua;
import com.setqq.script.sdk.ScriptAdapter;
import com.setqq.script.sdk.ServerScript;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;

/**
 * Created by Saki on 2018/6/4.
 */

public class ScriptLoader {
    private static NewService service;
    public static void initLoader(NewService service){
        ScriptLoader.service = service;
    }
    private static void loadFromServer(){
        String url = "http://setqqv8.top:8080/SQv8/get_script.jsp";
        String json = new String(HttpUtil.getUrlContent(url));
        try {
            JSONArray list = new JSONArray(json);
            for(int i=0;i<list.length();i++){
                JSONObject script = list.getJSONObject(i);
                new ServerScript(service,script); //加载服务器脚本
            }
            ScriptManager.getInstance().flushScript();//刷新脚本页面
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private static void findScriptFromDirectiry(File script){
        //加载所有已加密的脚本文件
        File[] ecs = script.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(Lua.ENCODE_SUFFIX)||name.endsWith(JavaScript.ENCODE_SUFFIX);
            }
        });
        if(ecs != null){
            for(File ec : ecs){
                loadEncode(ec);
            }
        }
    }
    public static void load(final String path,final String ns){
        new Thread(new Runnable() {
            @Override
            public void run() {
                File scriptRoot = new File(path);
                File[] scripts = scriptRoot.listFiles();
                if(scripts != null){
                    for(File script : scripts){
                        //脚本都应该在自己所属的目录中
                        if(script.isDirectory()){
                            //仅加载指定的id的脚本
                            if(ns != null){
                                if(ns.equals(script.getName())){
                                    findScriptFromDirectiry(script);
                                }
                                return;
                            }
                            findScriptFromDirectiry(script);
                        }
                    }
                }
                loadFromServer();
            }
        }).start();
    }
    private static void loadEncode(final File ec){
        try {
            if(ec.getName().endsWith(Lua.ENCODE_SUFFIX))
                new Lua(service, ec.getAbsolutePath());
            else if(ec.getName().endsWith(JavaScript.ENCODE_SUFFIX))
                new JavaScript(service,ec.getAbsolutePath());
        }catch (Exception e){
            e.printStackTrace();
            SQLog.e("脚本加载异常:" + e.getMessage());
        }
    }
}
