package com.setqq.script.sdk;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import com.saki.service.NewService;
import com.saki.sqv9.R;
import com.saki.tools.BitmapCallback;
import com.saki.tools.BitmapUtil;
import com.setqq.script.Script;

import java.io.File;

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

public abstract class ScriptAdapter implements IScript {
    public String name;
    public String version;
    public String info;
    public String author;
    public String id;
    public String icon;
    public String ui;
    public String path;
    public void initScript(final NewService service,final String update){
        BitmapUtil.loadBitmap(getIcon(), new BitmapCallback() {
            @Override
            public void callBack(Bitmap map) {
                new Script(service,id, name, author, version, info, hasUI(), new BitmapDrawable(service.getResources(), map), ScriptAdapter.this).setUpdate(update);
            }
        }, BitmapFactory.decodeResource(service.getResources(),R.drawable.ic_plugin_face_default));
    }
    public void initScript(final NewService service){
        BitmapUtil.loadBitmap(getIcon(), new BitmapCallback() {
            @Override
            public void callBack(Bitmap map) {
                new Script(service,id, name, author, version, info, hasUI(), new BitmapDrawable(service.getResources(), map), ScriptAdapter.this);
            }
        }, BitmapFactory.decodeResource(service.getResources(),R.drawable.ic_plugin_face_default));
    }

    @Override
    public void delete() {
        destroy();
        File f = new File(path);
        f.delete();
    }
    public abstract String getIcon();
    public abstract boolean hasUI();
    @Override
    public String getViewUrl() {
        return ui.indexOf("http") == 0?ui:"file://" + new File(path).getParentFile() + "/" + ui;
    }
}
