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_Shutdown:
		var pmsg MJexec
		ParseJsonBody(unmsg,&pmsg)
		//return Msg_exec_shutdown(&pmsg)
	
	// 备份 消息
	case Msg_Backup:
		var pmsg MJbackup
		ParseJsonBody(unmsg,&pmsg)
		ret = pmsg.Msg_Deal()
	}
	
	return JsonToByte(&ret)
}


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

func (rep *MJreport)Msg_Deal() interface{} {
	switch rep.Report{
	case Msg_Report_Offwork:
		if IfPrimaryUser(rep.Username){
			return GJbackup(Msg_Backup_Status_Start)
		}
		return nil
	default:
		PrintLog("Default Report Json ",rep.Report)
	
	}
	
	return nil
}
// func Msg_exec_shutdown(j *MJexec)interface{}{
// 	switch j.MsgStatus{
// 	case Msg_Exec_State_Request:
// 		return j
// 	case Msg_Exec_State_ACK:
// 		;
// 	}
// 	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_Over:
		PrintLog(" 查询文件以判断是否结束")
	case Msg_Backup_Status_Error:
		PrintLog(bak.Explain)
		return nil
	default:
		PrintLog("Default Backup Json ",bak.Status)
	}
	return nil

}



func GJreport(s string) interface{}{
	var rep MJreport
	rep.init(s)
	return rep
}
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 (rep * MJreport)init(Report string)  {
	rep.Username 		= Env_username
	rep.Computername 	= Env_computername
	rep.MsgType 		= Msg_Report
	rep.Report			= Report
}

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