package com.setqq.script.lua;

import org.keplerproject.luajava.LuaState;
import org.keplerproject.luajava.LuaStateFactory;
import android.webkit.JavascriptInterface;
import com.setqq.script.Msg;
import com.saki.log.SQLog;
import com.saki.service.NewService;
import com.saki.tools.FileUtil;
import com.setqq.script.NullIDException;
import com.setqq.script.sdk.ApiAdapter;
import com.setqq.script.sdk.ScriptAdapter;

public class Lua extends ScriptAdapter {
    public static final String SUFFIX = ".sl";
    public static final String ENCODE_SUFFIX = ".ecl";
    private LuaState l;
    private NewService context;

    public Lua(NewService context, String fileName) throws Exception {
        l = LuaStateFactory.newLuaState();
        l.openLibs();
        l.LdoString(FileUtil.loadEncodeFile(fileName));
        this.path = fileName;
        this.context = context;
        this.id = l.getLuaObject("id").toString();
        if (id.equals("nil")) {
            throw new NullIDException(path);
        }
        this.name = l.getLuaObject("name").toString();
        this.author = l.getLuaObject("author").toString();
        this.version = l.getLuaObject("version").toString();
        this.info = l.getLuaObject("info").toString();
        this.icon = l.getLuaObject("icon").toString();
        this.ui = l.getLuaObject("ui").toString();

        initScript(context);
    }
    @Override
    public void destroy() {
//        l.close();
    }

    @Override
    public void onLoad(NewService context) {
        Msg msg = new Msg(19);
        onMessageHandler(msg);
    }

    @Override
    public void onMessageHandler(Msg msg) {
        execLua("handleMessage", new ApiAdapter(context,this,msg));
        if (msg.type == Msg.TYPE_STOP)
            destroy();
    }

    @Override
    @JavascriptInterface
    public String onAction(String value) {
        Msg msg = new Msg();
        msg.addMsg("msg",value);
        return execLuaFormResault("onAction", new ApiAdapter(context,this,msg));
    }
    private String execLuaFormResault(String method,Object... param){
        l.getField(LuaState.LUA_GLOBALSINDEX, method);
        for (Object p : param) {
            l.pushJavaObject(p);
        }
        int ec = 0;
        if ((ec = l.pcall(param.length, 1, 0)) != 0) {
            SQLog.e("lua脚本[" + name + "]发生异常,错误代码:(" + ec + ")\n" + l.getLuaObject(1));
            return null;
        }
        l.setField(LuaState.LUA_GLOBALSINDEX,"result");
        return l.getLuaObject("result").toString();
    }
    private void execLua(String method, Object... param) {
        l.getField(LuaState.LUA_GLOBALSINDEX, method);
        for (Object p : param) {
            l.pushJavaObject(p);
        }
        int ec = 0;
        if ((ec = l.pcall(param.length, 0, 0)) != 0) {
            SQLog.e("lua脚本[" + name + "]发生异常,错误代码:(" + ec + ")\n" + l.getLuaObject(1));
        }
    }
    @Override
    public void runFunction(String funName,String param){
        execLua(funName,param);
    }

    @Override
    public boolean hasUI() {
        return ui!=null&&!ui.equals("nil");
    }

    @Override
    public String getIcon() {
        if (!icon.equals("nil"))
            return icon;
        return null;
    }
}
