package public

//
//
// msg env ////////////////////////////////////////////////////////////////
//

const EndSign							byte    = 125
const msgerror							string = "error"


// msg type ////////////////////////////////////////////////////////////////

const Msg_Report						string = "report"
const Msg_Exec							string = "exec"
const Msg_Backup						string = "backup"
const Msg_reg							string = "reg"

// msg report type ///////////////////////////////////////////////////////////

const Msg_Report_DaemonStart			string = "Daemon Start"
const Msg_Report_UserStart				string = "User Start"
const Msg_Report_Offwork				string = "offwork"
const Msg_Report_OverBak				string = "overbak"
const Msg_Report_CloseWindows			string = "closewindows"
const Msg_Report_OverCopyToWeDrive		string = "overcopytowedrive"
const Msg_Report_OverCopyToDeptShare	string = "OverCopyToDeptShare"
// msg backup status //////////////////////////////////////////////////////////////

const Msg_Backup_Status_Start			string = "start"
const Msg_Backup_Status_If				string = "if"
const Msg_Backup_Status_Continue		string = "Continue"
const Msg_Backup_Status_Run				string = "run"
const Msg_Backup_Status_Error			string = msgerror		


// msg exec status ////////////////////////////////////////////////////////////////

const (
	Msg_Exec_Cmder_ADServer				= iota
	Msg_Exec_Cmder_LocalDaemon
	Msg_Exec_Cmder_LocalUser
)
const Msg_Exec_State_Request			string = "request"
const Msg_Exec_State_Ack				string = "ack"
const Msg_Exec_CloseWindows				string = "closewindow"
const Msg_Exec_CloseWindows_cmd			string = "shutdown -s -t 15"
const Msg_Exec_CancelCloseWindows		string = "cancelclosewindows"
const Msg_Exec_CancelCloseWindows_cmd	string = "shutdown -a"
const Msg_Exec_LogOffUser				string = "logoffuser"
const Msg_Exec_LogOffUser_cmd			string = "shutdown -l"
const Msg_Exec_UserExec					string = "userexec"
const Msg_Exec_RebootWindows			string = "rebootWindows"
const Msg_Exec_RebootWindows_cmd		string = "shutdown -r -t 15"
const Msg_Exec_UpdateVersion			string = "updateverion"
const Msg_Exec_UpdateVersion_cmd		string = "Env_ADSoftTool_Update"
const Msg_Exec_InstallSoftware			string = "installsoftware"
const Msg_Exec_CopyWeDrive				string = "copyWeDrive"
const Msg_Exec_CopyDeptShare			string = "copydeptshare"

const (
	Msg_Reg_Query						= iota
	Msg_Reg_Read
)

//
//
// group Json ////
//

// Basic Json Fromat ///////////////////////////////////////////////////////
type MJbase struct{
	MsgType								string `json:"msgtype"`
	Status								string `json:"status"`
	Explain								string `json:"explain"`
	Username 							string `json:"username"`
	Computername 						string `json:"computername"`
	Dept								string `json:"dept"`
	Timestamp							int64  `json:"timestamp"`

	DataBool							bool   `json:"databool"`
	DataStr								string `json:"datastr"`
}
// report Json Format //////////////////////////////////////////////////////
type MJreport struct {
	MJbase
	Report								string `json:"report"`
	Version								string `json:"version"`

}

// Exec Json Fromat ///////////////////////////////////////////////////////
type MJexec struct {
	MJbase
	Instruction							string `json:"instruction"`
	Command								string `json:"command"`
	Cmder								int    `josn:"cmder"`
	SessionID							string `json:"sessionid"`		
	IfDelay								bool   `josn:"ifDelay"`
	Delay								string `json:"delay"`
}
// backup Json Format /////////////////////////////////////////////////////
type MJbackup struct{
	MJbase
	Version								string `json:"version"`
	DiskFree							string `json:"diskfree"`
	//Error								bool   `json:"error"`
	BatchFileExist						bool   `json:"batchfileexist"`
	BatchFileData						[]byte `json:"batchfiledata"`
	BatchFileName						string `json:"batchfilename"`
}
type MJreg	struct{
	MJbase
	Action								int    `json:"action"`
	Root								int	   `json:"root"`
	Path								string `json:"path"`
	Key									string `json:"key"`
	DataType							int	   `json:"datatype"`

}


//
//
// quickly Get & Set Json 
//


// Report
 
func GJreport(s string,who string) interface{}{
	var rep MJreport
	rep.init(s)
	if who != "" { rep.Username = who }

	return rep
}
func (rep * MJreport)init(Report string)  {

	rep.Username 		= User_name
	rep.Dept		= User_dept

	rep.Timestamp		= NowTimestamp()
	rep.Computername 	= User_computername
	rep.MsgType 		= Msg_Report
	rep.Report			= Report
	rep.Version			= Version
}
func (rep * MJreport)PrintExplain(){
	PrintLog(rep.Computername,rep.Username,rep.Explain)
}


// Exec

func GJexec(s string) MJexec{
	var exec MJexec
	exec.init(s)
	return exec
}
func GJexecAck(s string) interface{}{
	var exec MJexec
	exec.init(s)
	exec.Status			= Msg_Exec_State_Ack
	return exec
}
func GJexecCopyWeDrive(s string) MJexec{
	var exec MJexec
	exec.init(s)
	exec.DataStr		= User_dept
	return exec
}
func GJexecInstallSoftware(b bool,s string) interface{}{
	var exec MJexec
	exec.init(s)
	exec.Status			= Msg_Exec_State_Ack
	exec.DataBool		= b
	exec.Cmder			= Msg_Exec_Cmder_ADServer
	exec.Instruction	= Msg_Exec_InstallSoftware
	exec.Command		= s
	return exec
}

func GJexecUserCmd(s string) interface{}{
	var exec MJexec
	exec.init("")
	exec.Command			= s
	exec.Status			= Msg_Exec_State_Ack
	return exec
}
func (exec * MJexec)init(s string){
	exec.Dept		= User_dept
	exec.Username 		= User_name
	exec.Computername 	= User_computername
	exec.Timestamp		= NowTimestamp()
	exec.MsgType 		= Msg_Exec
	exec.Instruction	= s
	exec.SessionID		= User_sessionID
	exec.Cmder			= Msg_Exec_Cmder_LocalDaemon
	exec.Status			= Msg_Exec_State_Request
	// exec.IfDelay		= false
	// exec.Delay			= Rand(RandMax)
	exec.Timestamp		= NowTimestamp()
}


// Backup

func GJbackup()interface{}{
	var rep MJbackup
	rep.init()
	return rep
}
func (bak * MJbackup)init()  {
	bak.Dept			= User_dept
	bak.Timestamp		= NowTimestamp()
	bak.MsgType 		= Msg_Backup
	bak.Version 		= BackupVersion
	bak.Status			= Msg_Backup_Status_Start
	bak.Version			= Version
}
func (bak * MJbackup)Errorf(s string){
	bak.Status 			= Msg_Backup_Status_Error
	bak.Explain 		= "Backup Error: " + bak.Computername + " -> " + s
}
func (bak * MJbackup)PrintExplain(){
	PrintLog(bak.Username,bak.Status,bak.Explain)
}