package innerTask

import (
	"encoding/json"
	"netease_control_center/interfaces/log"
	"time"
)

var (
	ICmdFunc ICmd_Exec_Func
)

func ICmdPack(cmd ICmd, args ...interface{}) string {
	var tmp [1]ICmdStruct
	tmp[0] = ICmdStruct{
		Cmd:      cmd,
		Args:     args,
		PackTime: time.Now(),
	}

	if data, err := json.Marshal(tmp); err == nil {
		return string(data)
	} else {
		log.Println(log.DebugLog, "json code err!")
		return ""
	}
}

func ICmdsPack(Icmds []ICmdStruct) string {
	for k, _ := range Icmds {
		Icmds[k].PackTime = time.Now()
	}

	if data, err := json.Marshal(Icmds); err == nil {
		return string(data)
	} else {
		return ""
	}
}

func ICmdExecString(Icmdstring string) {
	log.Println(log.DebugLog, "Begin exec cmd:", Icmdstring)
	if len(Icmdstring) == 0 {
		return
	}

	var tmp []ICmdStruct
	if err := json.Unmarshal([]byte(Icmdstring), &tmp); err == nil {
		for _, v := range tmp {
			if ICmdFunc != nil {
				ICmdFunc(v.Cmd, v.Args...)
			}
		}
	} else {
		log.Println(log.DebugLog, "Json decode cmd string err:", err)
	}
}
