package com.setqq.script;

import com.setqq.script.sdk.IScript;
import com.saki.log.SQLog;
import com.saki.qq.global.ScriptManager;
import com.saki.service.NewService;
import com.saki.tools.FileUtil;
import com.saki.ui.SwitchButton;
import com.saki.ui.SwitchButton.OnCheckedChangeListener;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;

import java.io.File;

public class Script implements OnCheckedChangeListener {
    public static SharedPreferences getAppConfig(Context context) {
        return context.getSharedPreferences("app_config", Context.MODE_PRIVATE);
    }

    public String name;
    public String packageName;
    public String author;
    public String version;
    public String info;
    public Drawable icon;
    public IScript instance;
    public boolean opration = false;
    public boolean jump = false;
    public int type;
    public String update;
    private NewService context;

    public Script(NewService context, String packName, String name, String author,
                  String version, String info, boolean jump, Drawable icon,
                  IScript instance) {
        this.context = context;
        this.packageName = packName;
        this.name = name;
        this.author = author;
        this.version = version;
        this.info = info;
        this.icon = icon;
        this.instance = instance;
        this.jump = jump;
        this.opration = getAppConfig(context).getBoolean(packageName, false);
        ScriptManager.getInstance().addScript(this);
        onLoad(context);
    }
    public void setUpdate(String url){
        this.type = 1;
        this.update = url;
    }
    public void delete(Context context) {
        if(packageName != null){
            FileUtil.deleteFile(new File(FileUtil.BASE_PATH+"Script/"+packageName));
        }
    }

    public boolean handlerMsg(Msg msg) {
        try {
            if (instance != null) {
                instance.onMessageHandler(msg);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            SQLog.e("[" + name + "]脚本发生异常:" + e.getMessage());
        }
        return false;
    }

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        opration = isChecked;
        Editor e = getAppConfig(context).edit();
        e.putBoolean(packageName, opration);
        e.commit();
    }

    public String getView()
    {
        if(instance != null){
            return instance.getViewUrl();
        }
        return null;
    }
    public void onLoad(NewService context) {
        if (instance != null) {
            try {
                instance.onLoad(context);
            } catch (Exception e) {
                e.printStackTrace();
                SQLog.w("脚本初始化异常:" + e.toString());
            }
        }
    }
    public void stop() {
        try {
            instance.destroy();
        } catch (Exception e) {
            e.printStackTrace();
            SQLog.e("脚本停止时异常:" + e.toString());
        }
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Script){
            return this.packageName.equals(((Script) obj).packageName);
        }
        return super.equals(obj);
    }
}
