package alstatus

import (
	"fmt"
	"net"
	"pbx-api-gin/internal/app/stc/active"
	msgdata "pbx-api-gin/internal/app/stc/data"
	"pbx-api-gin/internal/app/stc/socket"
	"pbx-api-gin/pkg/utils"
)

func SendToStc(conn net.Conn, data []byte) {

	_, err := conn.Write(data)
	if err != nil {
		utils.LoggerDebug.Printf("Send To STC msg err:%+v", err)
		conn.Close()
	}
	//lfshook.NewLogger().Logger.Infof("SendToStc data:%x", data)
}

// report alarm status to STC
func AlarmStatus(exten string, status string) {

	//Not Master role , ignore
	if !active.Master {
		return
	}

	//check exten if it is a alarm exten
	if !utils.IsPAIU(exten) { // if not alarm device , return
		return
	}

	protocol := msgdata.NewProtocol()
	protocol.MessageID = 0x26
	protocol.DataLength = 0x04
	protocol.Data = make([]byte, 4)

	protocol.Data[0] = exten[2] - '0' //车厢号
	protocol.Data[1] = exten[3] - '0' //位置号

	//报警器工作状态
	switch status {
	case "unavailable", "Unavailable": //offline
		protocol.Data[2] = 0x00

	case "idle", "Idle": //idle
		protocol.Data[2] = 0x01

	case "dial": //dial
		protocol.Data[2] = 0x02
		return
	case "queue": //PAD alarm
		protocol.Data[2] = 0x03

	case "connect": //connect
		protocol.Data[2] = 0x04

	case "allreset": //allreset

		protocol.MessageID = 0x29
		protocol.Data[0] = 0x02 //all reset
		protocol.Data[1] = 0
		protocol.Data[2] = 0
	case "allhold": //allhold

		protocol.MessageID = 0x29
		protocol.Data[0] = 0x01 //all hold
		protocol.Data[1] = 0
		protocol.Data[2] = 0
	}

	encoded, errEn := protocol.Encode()
	if errEn != nil {
		fmt.Println("Encode error:", errEn)
		return
	}
	//check if actived
	utils.LoggerDebug.Printf("PAD number:%s		CarNum:%x 	 Pos:%x 	Status:%x", exten, protocol.Data[0], protocol.Data[1], protocol.Data[2])

	if socket.Conn != nil {
		SendToStc(socket.Conn, encoded)
	}

	if socket.Conn8 != nil {
		SendToStc(socket.Conn8, encoded)
	}

}

// report broadcast status to STC
func PaStatus(src string, patype string, operation string) {

	//Not Master role , ignore
	if !active.Master {
		return
	}
	utils.LoggerDebug.Printf("PA Status Src:%s 		Type:%s		Status:%s", src, patype, operation)
	protocol := msgdata.NewProtocol()
	protocol.MessageID = 0x22
	protocol.DataLength = 0x04
	protocol.Data = make([]byte, 4)

	//广播发起方
	switch src {
	case "2311": //mc1
		protocol.Data[0] = 0x01
	case "2381": //mc8
		protocol.Data[0] = 0x08
	/*case "1411": //mc8
		protocol.Data[0] = 0x08
	case "1481": //mc8
		protocol.Data[0] = 0x08*/
	default: //
		protocol.Data[0] = 0x00
	}

	//广播类型
	switch patype {
	case "C2C": //司机对讲---ami
		protocol.Data[1] = 0x01
	case "PA": //人工广播---ami
		protocol.Data[1] = 0x02
	case "DCS": //开关门提示音
		protocol.Data[1] = 0x03
	case "EMG": //紧急广播
		protocol.Data[1] = 0x04
	case "PAD": //报警
		protocol.Data[1] = 0x05
	case "CPA": //地面广播---ami
		protocol.Data[1] = 0x06
	case "SPC": //特殊
		protocol.Data[1] = 0x07
	case "STN": //报站
		protocol.Data[1] = 0x08
	case "CHK": //自检
		protocol.Data[1] = 0x09
	case "VOL": //音调条件
		protocol.Data[1] = 0x0a
	}

	//操作类型
	switch operation {
	case "start": //
		protocol.Data[2] = 0x01
	case "end": //
		protocol.Data[2] = 0x02
	case "refuse": //
		protocol.Data[2] = 0x03
	case "fail": //
		protocol.Data[2] = 0x04
	case "continue": //
		protocol.Data[2] = 0x05
	}

	encoded, errEn := protocol.Encode()
	if errEn != nil {
		utils.LoggerDebug.Printf("Encode error:%+v", errEn)
		return
	}

	if socket.Conn != nil {
		SendToStc(socket.Conn, encoded)
	}

	if socket.Conn8 != nil {
		SendToStc(socket.Conn8, encoded)
	}

}

// report broadcast status to STC
func OccPad(operation string) {

	//Not Master role , ignore
	if !active.Master {
		return
	}

	utils.LoggerDebug.Printf("OCC-PAD status:%s", operation)
	protocol := msgdata.NewProtocol()
	protocol.MessageID = 0x2A
	protocol.DataLength = 0x04
	protocol.Data = make([]byte, 4)

	//广播发起方
	switch operation {
	case "start": //mc1
		protocol.Data[0] = 0x01
	case "end": //mc8
		protocol.Data[0] = 0x02
	case "": //
		protocol.Data[0] = 0x00
	}

	encoded, errEn := protocol.Encode()
	if errEn != nil {
		utils.LoggerDebug.Printf("Encode error:%+v", errEn)
		return
	}

	if socket.Conn != nil {
		SendToStc(socket.Conn, encoded)
	}

	if socket.Conn8 != nil {
		SendToStc(socket.Conn8, encoded)
	}

}

// report broadcast status to STC
func SendRecordFile(filename, rcdtype string) {

	//time.Sleep(5 * time.Second)
	/*
		if !utils.FileExists(filename) {
			lfshook.NewLogger().Logger.Infof("===Recording filename not exist:%+v=", filename)
			return
		}
	*/
	//Not Master role , ignore
	if !active.Master {
		return
	}

	protocol := msgdata.NewProtocol()
	protocol.MessageID = 0x31

	filenameHex := []byte(filename)
	dataLen := len(filenameHex) + 1

	protocol.DataLength = uint16(dataLen)
	protocol.Data = make([]byte, dataLen)
	copy(protocol.Data[1:], filenameHex)

	switch rcdtype {
	case "C2C": //
		protocol.Data[0] = 0x01
	case "PA": //
		protocol.Data[0] = 0x02
	case "PAD": //
		protocol.Data[0] = 0x05
	case "CPA": //
		protocol.Data[0] = 0x06
	case "OTR": //
		protocol.Data[0] = 0x03
		//lfshook.NewLogger().Logger.Infof("===Recording filename:%+v=", protocol.Data)
	}
	encoded, errEn := protocol.Encode()
	if errEn != nil {
		utils.LoggerDebug.Printf("Encode error:%+v", errEn)
		return
	}

	if socket.Conn != nil {
		SendToStc(socket.Conn, encoded)
	}

	if socket.Conn8 != nil {
		SendToStc(socket.Conn8, encoded)
	}
}
