package com.setqq.script.sdk;

import android.os.Environment;

import com.saki.http.Http;
import com.saki.log.SQLog;
import com.saki.qq.global.Global;
import com.saki.service.NewService;
import com.saki.tools.FileUtil;
import com.setqq.script.Msg;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

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

public class ApiAdapter implements ApiInterface {

    protected NewService context;
    private IScript mis;
    public Msg msg;

    public ApiAdapter(NewService context,IScript is,Msg msg){
        this.context = context;
        this.msg = msg;
        this.mis = is;
    }
    @Override
    public void runTimeout(final String function,final String param,final long time){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(time);
                    if(function != null && !function.equals("")) {
                        mis.runFunction(function,param);
                    }
                } catch (Exception e) {
                    SQLog.e("RunTimeoutException:"+e.toString());
                }
            }
        }).start();
    }
    @Override
    public String getTextMsg(){
        return msg.getTextMsg();
    }
    @Override
    public String getMsg(String key){
        ArrayList<String> list = msg.getMsg(key);
        StringBuilder sb = new StringBuilder("");
        if(list != null){
            for(String s:list)
                sb.append(s);
        }
        return sb.toString();
    }
    @Override
    public void clearMsg(){
       msg.clearMsg();
    }
    @Override
    public void add(String type, String msg) {
        this.msg.addMsg(type, msg);
    }
    @Override
    public String getScriptPath(){return FileUtil.SCRIPT_PATH;}
    @Override
    public String at(int index) {
        ArrayList<String> arr = msg.getMsg("at");
        if (arr == null || index < 0 || index >= arr.size())
            return null;
        return arr.get(index);
    }

    @Override
    public int atCnt() {
        return msg.getMsg("at") == null ? 0 : msg.getMsg("at").size();
    }

    @Override
    public String httpCall(String method,String url,String data,String head){
        byte[] param = null;
        if(data != null && !data.equals(""))
            param = data.getBytes();
        return new String(new Http().call(method,url,param,head));
    }
    @Override
    public String load(String path, String key, String defaultValue) {
        File file = new File(path);
        if (file.exists()) {
            Properties p = new Properties();
            try {
                p.load(new FileInputStream(file));
                return p.getProperty(key);
            } catch (IOException e) {
                SQLog.e("读取错误:" + e.toString());
            }
        }
        return defaultValue;
    }

    @Override
    public void logE(String info) {
        SQLog.e(info);
    }

    @Override
    public void logI(String info) {
        SQLog.i(info);
    }

    @Override
    public void logW(String info) { SQLog.w(info); }

    @Override
    public double robot() {
        return Global.qq;
    }

    @Override
    public String getRootPath() {
        return Environment.getExternalStorageDirectory() + "/";
    }
    @Override
    public String getLibPath(){return FileUtil.SCRIPT_LIB_PATH;}
    @Override
    public String read(String path){
        byte[] bin = FileUtil.readFile(new File(path));
        return new String(bin);
    }
    @Override
    public void write(String path,String c){
        FileUtil.writeFile(new File(path),c.getBytes());
    }
    @Override
    public void save(String path, String key, String value) {
        File file = new File(path);
        try {
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            Properties p = new Properties();
            p.load(new FileInputStream(file));
            p.setProperty(key, value);
            p.store(new FileOutputStream(file), null);
        } catch (IOException e) {
            SQLog.e("保存错误:" + e.toString());
        }
    }
    @Override
    public ApiInterface send(){
        Msg mmsg = new ScriptSend(context).send(msg);
        if (mmsg != null) {
            this.msg = mmsg;
            return this;
        }
        return null;
    }

    @Override
    public int getType() {
        return msg.type;
    }

    @Override
    public double getGroupId() {
        return msg.groupid;
    }

    @Override
    public double getCode() {
        return msg.code;
    }

    @Override
    public double getUin() {
        return msg.uin;
    }

    @Override
    public double getTime() {
        return msg.time;
    }

    @Override
    public int getValue() {
        return msg.value;
    }

    @Override
    public String getGroupName() {
        return msg.groupName;
    }

    @Override
    public String getUinName() {
        return msg.uinName;
    }

    @Override
    public String getTitle() {
        return msg.title;
    }

    @Override
    public void setType(int type) {
        msg.type = type;
    }

    @Override
    public void setGroupId(double id) {
        msg.groupid = Double.valueOf(id).longValue();
    }

    @Override
    public void setCode(double code) {
        msg.code = Double.valueOf(code).longValue();
    }

    @Override
    public void setUin(double uin) {
        msg.uin = Double.valueOf(uin).longValue();
    }

    @Override
    public void setTime(double time) {
        msg.time = Double.valueOf(time).longValue();
    }

    @Override
    public void setValue(int value) {
        msg.value = value;
    }

    @Override
    public void setGroupName(String name) {
        msg.groupName = name;
    }

    @Override
    public void setUinName(String name) {
        msg.uinName = name;
    }

    @Override
    public void setTitle(String name) {
        msg.title = name;
    }
}
