package public
// 消息入口
func DealMsgEnter(ip string,msgtype string,unmsg []byte) []byte {
	var ret interface{}
	switch msgtype{
	// 报告 消息
	case Msg_Report:
		var pmsg MJreport
		ParseJsonBody(unmsg,&pmsg)
		ret = pmsg.Msg_Deal()
		
	// 执行 消息
	case Msg_Exec:
		var pmsg MJexec
		ParseJsonBody(unmsg,&pmsg)
		ret = pmsg.Msg_Deal()
	
	// 备份 消息
	case Msg_Backup:
		var pmsg MJbackup
		ParseJsonBody(unmsg,&pmsg)
		ret = pmsg.Msg_Deal()
	}
	
	return JsonToByte(&ret)
}


// 
// 具体消息实现
//

func (rep *MJreport)Msg_Deal() interface{} {

	PrintLog(rep.Explain)

	switch rep.Report{
	case Msg_Report_Offwork:
		if IfPrimaryUser(rep.Username){
			return GJbackup(Msg_Backup_Status_Start)
		}
		return nil
	case Msg_Report_OverBak:
		return GJexecAck(Msg_Exec_CloseWindows)
	default:
		PrintLog("Default Report Json ",rep.Report)
	
	}
	
	return nil
}
func (exec *MJexec)Msg_Deal()interface{}{
	switch exec.Status{
	case Msg_Exec_State_Request:
		// maybe 当然时我来控制咯
		// 修改 请求状态，client daemon才可以执行
		exec.Status = Msg_Exec_State_Ack

		// 根据指令类型
		// 确定具体命令
		// 以及是否延迟 
		switch exec.Instruction{
		case Msg_Exec_CloseWindows:
			exec.Command 	= Msg_Exec_CloseWindows_cmd
			exec.IfDelay	= false
		
		case Msg_Exec_CancelCloseWindows:
			exec.Command 	= Msg_Exec_CancelCloseWindows_cmd
			exec.IfDelay	= false
		}
		return exec
	case Msg_Exec_State_Ack:
		if exec.IfDelay {Delay(&exec.Delay)}
		Execcmd_nowait(exec.Command)
		
	}
	return nil
}
func (bak * MJbackup)Msg_Deal () interface{}{
	switch bak.Status{
	case Msg_Backup_Status_Start:
		bak.Username		= Env_username
		bak.Computername 	= Env_computername
		bak.Explain 		= "Backup Start"
		bak.Status 			= Msg_Backup_Status_If
		bak.Version			= BackupVersion
		bak.BatchFileName 	= DocuBase + bak.Version + bak.Username + BackupExtFile

		//PrintLog("收集D盘剩余空间,查询主用户")
		PrintLog(bak.Explain)

		//如果Start可以，并且得到了主用户，判断是否有最新版本的文件和文件是否存在
		if !Exists(Env_FreeFileSyncExe){
			bak.Errorf("FreeFileSync hasn't been installed")
			DebugLog(bak.Explain)
			SendServer(bak)
			return nil
		}
		//C:\Users\Public\Documents\AD-Control\0416tengfei.ffs_batch
		if !Exists(bak.BatchFileName){
			bak.BatchFileExist		= false
		}else{
			bak.BatchFileExist		= true
		}

		SendServer(bak)
		return nil

	case Msg_Backup_Status_RFile:
		bak.Status 			= Msg_Backup_Status_If
		bak.Explain 		= "Backup Read Batch File"
		PrintLog(bak.Explain)

		return bak

	// 判断是否能进行备份
	case Msg_Backup_Status_If:
		// 如果ffs的配置文件不存在 或者 是旧版本
		if !bak.BatchFileExist || bak.Version != BackupVersion{
			bak.BatchFileData 	= CreateExtTempate(&bak.Username)
			bak.Version 	= BackupVersion
		}
		
		bak.Status 			= Msg_Backup_Status_RunBak
		bak.Explain 		= "Backup Runing"
		PrintLog(bak.Explain)

		SendMsg(bak.Computername + DeamonListen,bak)

	case Msg_Backup_Status_RunBak:
		// 有新版本的话，下行赋值才有意义
		bak.BatchFileName 	= DocuBase + bak.Version + bak.Username + BackupExtFile
		if !bak.BatchFileExist{
			if err := WriteBytesFile(bak.BatchFileName,bak.BatchFileData);err !=nil{

				bak.Errorf("Write Batch File Error "+err.Error())
				return nil
			}
		}
		bak.Explain		 	= "Backup Runing"
		PrintLog(bak.Explain)
		Execcmd_nowait(Env_FreeFileSyncExe + " "+ bak.BatchFileName)
		return nil

	case Msg_Backup_Status_Error:
		PrintLog(bak.Explain)
		return nil
	default:
		PrintLog("Default Backup Json ",bak.Status)
	}
	return nil

}

//
// 快速 Json 获取
//

func GJreport(s string) interface{}{
	var rep MJreport
	rep.init(s)
	return rep
}

func (rep * MJreport)init(Report string)  {
	rep.Username 		= Env_username
	rep.Computername 	= Env_computername
	rep.MsgType 		= Msg_Report
	rep.Report			= Report
}


func GJexec(s string) interface{}{
	var rep MJexec
	rep.init(s)
	return rep
}
func GJexecAck(s string) interface{}{
	// var rep  := GJexec(s)
	// 这样不行，GJexec返回的是interface{}
	var rep MJexec
	rep.init(s)
	rep.Status			= Msg_Exec_State_Ack
	return rep
}
func (exec * MJexec)init(s string){
	exec.Username 		= Env_username
	exec.Computername 	= Env_computername
	exec.MsgType 		= Msg_Exec
	exec.Instruction	= s
	//exec.Command		= cmd
	exec.Status			= Msg_Exec_State_Request
	exec.IfDelay		= false
	exec.Delay			= Rand(RandMax)
}


func GJbackup(s string)interface{}{
	var rep MJbackup
	rep.init(s)
	return rep
}
func (bak * MJbackup)init(Status string)  {
	bak.Username 		= Env_username
	bak.Computername 	= Env_computername
	bak.MsgType 		= Msg_Backup
	bak.Version 		= BackupVersion
	bak.Status			= Status
}

func (bak * MJbackup)Errorf(s string){
	bak.Status 			= Msg_Backup_Status_Error
	bak.Explain 		= "Backup Error: " + bak.Computername + " -> " + s
}