package public

//
// 消息入口
//

func DealMsgEnter(ip string,msgtype string,unmsg []byte) []byte {
	PrintLog("DEAL ",string(unmsg))
	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()

	// 注册表 消息
	case Msg_reg:
		var pmsg MJreg
		ParseJsonBody(unmsg,&pmsg)
		ret = pmsg.Msg_Deal()

	}
	
	return JsonToByte(&ret)
}


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


// 关于 report 报告 的 消息处理

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

	switch rep.Report{
	case Msg_Report_Offwork:
		return GJbackup()

	case Msg_Report_OverBak:
		SysMsgBox(rep.Computername,"备份结束")
		return GJexecAck(Msg_Exec_CloseWindows)

	case Msg_Report_CloseWindows:
		Reg_Write_key_one(
			Reg_Root_achome,
			Reg_Path_achome_dc  + rep.Computername,
			Reg_Name_achome_dc_lastclose,
			UnixTimeParse(&rep.Timestamp))

	case Msg_Report_DaemonStart:
		Reg_Write_key_one(
			Reg_Root_achome,
			Reg_Path_achome_dc  + rep.Computername,
			Reg_Name_achome_dc_lastlogin,
			UnixTimeParse(&rep.Timestamp))

	case Msg_Report_UserStart:
		if rep.Version != Version{ 
			return GJexecAck(Msg_Exec_UpdateVersion)
		}
	default:
		PrintLog("Default Report Json ",rep.Report)
	
	}
	return nil
}


// 关于 exec 执行 的 消息处理

func (exec *MJexec)Msg_Deal()interface{}{

	exec.IfDelay	= false

	switch exec.Instruction{
	case Msg_Exec_CloseWindows:
		exec.Command 	= Msg_Exec_CloseWindows_cmd
	
	case Msg_Exec_CancelCloseWindows:
		exec.Command 	= Msg_Exec_CancelCloseWindows_cmd

	case Msg_Exec_LogOffUser:
		exec.Command	= Msg_Exec_LogOffUser_cmd

	case Msg_Exec_RebootWindows:
		exec.Command	= Msg_Exec_RebootWindows_cmd
		
	case Msg_Exec_UpdateVersion:
		Exit()

	}

	switch exec.Status{
	case Msg_Exec_State_Request:
		// maybe 当然时我来控制咯
		// 修改 请求状态，client daemon才可以执行
		exec.Status = Msg_Exec_State_Ack
		return exec

	case Msg_Exec_State_Ack:
		// 进行延迟
		if exec.IfDelay {Delay(&exec.Delay)}

		// 区分执行者
		switch exec.Cmder{
		case Msg_Exec_Cmder_ADServer:
			switch exec.Instruction{
			// 软件安装
			case Msg_Exec_InstallSoftware:
				// 对于 msi 格式
				// 从\\adserver安装
				// 共享则需要everyone的读取权限
				if HasSuffix(&exec.Command,".msi"){
					full_cmd := "msiexec /i " + Env_ADSoftlike + `\` + exec.Command + " /qb"
					PsExec_sdi(exec.Computername,full_cmd,exec.SessionID)

				// 对于 exe 格式
				} else {
					// 如果文件存在，则执行C:\windows目录下的文件
					if exec.Extbool {
						PsExec_sdi(exec.Computername,Dir_Windows + exec.Command,exec.SessionID)
					// 如果文件不存在，从 ADMIN$ 复制到C:\windows，再执行
					} else {
						PsExec_sdic(exec.Computername,Env_ADsoftware_local + exec.Command,exec.SessionID)
					}
				}
			}
		default:
			Execcmd_nowait(exec.Command)

		}
		
	}
	return nil
}


// 关于 backup 备份 的 消息处理

func (bak * MJbackup)Msg_Deal() interface{}{

	switch bak.Status{
	case Msg_Backup_Status_Start:

		if !PrimaryUser(bak.Username) { return nil }

		bak.Username		= User_name
		bak.Computername 	= User_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 NotExist(Env_FreeFileSyncExe){
			bak.Errorf("FreeFileSync hasn't been installed")
			PrintLog(bak.Explain)
			SendServer(bak)
			return nil
		}

		//判断是否有最新版本的文件是否存在
		//C:\Users\Public\Documents\AD-Control\0416tengfei.ffs_batch
		if NotExist(bak.BatchFileName){
			bak.BatchFileExist		= false
		}else{
			bak.BatchFileExist		= true
		}
		SendServer(bak)
		return nil

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

		bak.PrintExplain()
		bak.Status 			= Msg_Backup_Status_Continue

		// 交给daemon 处理 Msg_Backup_Status_Continue
		SendMsg(bak.Computername + DaemonListen,bak)
		return nil

	case Msg_Backup_Status_Continue:
		// 生成新FFS所使用的batch文件
		bak.BatchFileName 	= DocuBase + bak.Version + bak.Username + BackupExtFile
		if !bak.BatchFileExist{
			PrintLog("init ",string(bak.BatchFileName))
			if err := WriteBytesFile(bak.BatchFileName,bak.BatchFileData);err !=nil{
				bak.Errorf("Write Batch File Error "+err.Error())
				return bak
			}
		}
		bak.Status 			= Msg_Backup_Status_Run
		bak.Explain		 	= "Backup Ready"
		bak.PrintExplain()
		return bak
	case Msg_Backup_Status_Run:
		SysMsgBox(bak.Computername,bak.Username + " 开始备份")
		PsExec_sd(bak.Computername,Env_FreeFileSyncExe + " "+ bak.BatchFileName)
	case Msg_Backup_Status_Error:
		PrintLog(bak.Explain)
	default:
		PrintLog("Default Backup Json ",bak.Status)
	}
	return nil

}

func (reg * MJreg)Msg_Deal() interface{}{
	switch reg.Action{
	case Msg_Reg_Query:
			reg.DataStr = Reg_query_key_one(reg.Root,reg.Path,reg.Key)
		
	}
	return reg
}