package icsstat

import (
	"fmt"
	"io"

	"gitlab.com/ics_cinnamon/voiceStatistics/icsconf"
	"gitlab.com/ics_cinnamon/voiceStatistics/icserror"
	"gitlab.com/ics_cinnamon/voiceStatistics/icslog"
	"gitlab.com/ics_cinnamon/voiceStatistics/icsnet"
)

type StatInfos struct {
	//necessary value
	SeqId       string // 25
	LogTime     string // 14
	LogType     string // 3
	Sid         string // 50
	ResultCode  string // 8
	ReqTime     string // 17
	RspTime     string // 17
	ClientIp    string // 40
	DevInfo     string // 5
	OsInfo      string // 50
	NwInfo      string // 5
	SvcName     string // 32
	DevModel    string // 50
	CarrierType string // 1
	HostName    string // 32
	ScnName     string // 32

	//not necessary
	CallId     string // 60
	TrId       string // 100
	ConcCall   string // 5
	SvcType    string // 20
	SvcReqTime string // 17
	SvcRspTime string // 17
	ResultMsg  string // 256
	SttText    string // 8192
	TtsText    string // 1024
}

const (
	//necessary value

	STAT_INFO    = 0
	SEQ_ID       = STAT_INFO + 25
	LOG_TIME     = SEQ_ID + 14
	LOG_TYPE     = LOG_TIME + 3
	SID          = LOG_TYPE + 50
	RESULT_CODE  = SID + 8
	REQ_TIME     = RESULT_CODE + 17
	RES_TIME     = REQ_TIME + 17
	CLIENT_IP    = RES_TIME + 40
	DEV_INFO     = CLIENT_IP + 5
	OS_INFO      = DEV_INFO + 50
	NW_INFO      = OS_INFO + 5
	SVC_NAME     = NW_INFO + 32
	DEV_MODEL    = SVC_NAME + 50
	CARRIER_TYPE = DEV_MODEL + 1
	HOST_NAME    = CARRIER_TYPE + 32
	SCN_NAME     = HOST_NAME + 32

	//not necessary
	CALL_ID      = SCN_NAME + 60
	TR_ID        = CALL_ID + 100
	CONC_CALL    = TR_ID + 5
	SVC_TYPE     = CONC_CALL + 20
	SVC_REQ_TIME = SVC_TYPE + 17
	SVC_RSP_TIME = SVC_REQ_TIME + 17
	RESULT_MSG   = SVC_RSP_TIME + 17
	STT_TEXT     = RESULT_MSG + 256
	TTS_TEXT     = STT_TEXT + 8192 //slice 86..?
)

func ListenStatMNG() (icserr *icserror.IcsError) {
	l := icslog.GetIcsLog()
	conf := icsconf.GetIcsConfig()

	//start bot-command listen
	statPort := conf.Target.TargetPort
	statTransport := conf.Target.Transport
	statLocalAddrStr := fmt.Sprintf("0.0.0.0:%d", statPort)
	statLocalAddr := icsnet.NewNetAddrWithIPAddr(statLocalAddrStr)

	var cmdErr *icserror.IcsError
	statTCP, cmdErr := icsnet.ListenAndServeTCP(&statLocalAddr, nil, "\r\n\r\n", BotCommand)
	if cmdErr != nil {
		l.Printf(icslog.LOG_LEVEL_FATAL, -1, "%s", cmdErr.GetError())
		return nil
	}
	l.Printf(icslog.LOG_LEVEL_INFO, -1, "Listening Stat TCP Command %s port[%d]", statTransport, statPort)
	fmt.Println(statTCP)

	return nil
}

func BotCommand(t *icsnet.IcsTCPNet, bufend string) {
	l := icslog.GetIcsLog()
	statInfos := new(StatInfos)

	defer t.Close()
	l.Printf(icslog.LOG_LEVEL_INFO, -1, "Connected from - %s", t.RemoteAddr())

	for {
		statReads, rlen, rerr := t.ReadS(10018, bufend) // 여기서 전체 몇 byte씩 읽어서 로그를 찍을건지 알아야함
		if rerr != nil {
			if rerr.GetError() != io.EOF {
				l.Printf(icslog.LOG_LEVEL_ERROR, -1, "[Stat Command] ReadS Error! - %s[%d:%d]",
					rerr.GetError(), rlen, len(statReads))
			}
			break
		} else {
			fmt.Printf("Recved Stat Command(%s) %d \n", statReads, rlen)
			statInfos.ChangeByteToString(statReads)

			insLog := ""
			insLog += fmt.Sprintf("%s\n", statInfos.CallId)
			insLog += fmt.Sprintf("%s\n", statInfos.SeqId)
			insLog += fmt.Sprintf("%s\n", statInfos.LogTime)
			insLog += fmt.Sprintf("%s\n", statInfos.LogType)
			insLog += fmt.Sprintf("%s\n", statInfos.Sid)
			insLog += fmt.Sprintf("%s\n", statInfos.ResultCode)
			insLog += fmt.Sprintf("%s\n", statInfos.ReqTime)
			insLog += fmt.Sprintf("%s\n", statInfos.RspTime)
			insLog += fmt.Sprintf("%s\n", statInfos.ClientIp)
			insLog += fmt.Sprintf("%s\n", statInfos.DevInfo)
			insLog += fmt.Sprintf("%s\n", statInfos.OsInfo)
			insLog += fmt.Sprintf("%s\n", statInfos.NwInfo)
			insLog += fmt.Sprintf("%s\n", statInfos.SvcName)
			insLog += fmt.Sprintf("%s\n", statInfos.DevModel)
			insLog += fmt.Sprintf("%s\n", statInfos.CarrierType)
			insLog += fmt.Sprintf("%s\n", statInfos.HostName)
			insLog += fmt.Sprintf("%s\n", statInfos.ScnName)
			insLog += fmt.Sprintf("%s\n", statInfos.CallId)
			insLog += fmt.Sprintf("%s\n", statInfos.TrId)
			insLog += fmt.Sprintf("%s\n", statInfos.ConcCall)
			insLog += fmt.Sprintf("%s\n", statInfos.SvcType)
			insLog += fmt.Sprintf("%s\n", statInfos.SvcReqTime)
			insLog += fmt.Sprintf("%s\n", statInfos.SvcRspTime)
			insLog += fmt.Sprintf("%s\n", statInfos.ResultMsg)
			insLog += fmt.Sprintf("%s\n", statInfos.SttText)
			insLog += fmt.Sprintf("%s\n", statInfos.TtsText)

			l.Printf(icslog.LOG_LEVEL_INFO, -1, "%s", insLog)
			fmt.Printf("stats- %s \n", statInfos.SeqId)
			fmt.Printf("stats- %s \n", statInfos.LogTime)
			fmt.Printf("stats- %s \n", statInfos.LogType)
			fmt.Printf("stats- %s \n", statInfos.Sid)
			fmt.Printf("stats- %s \n", statInfos.ResultCode)
			fmt.Printf("stats- %s \n", statInfos.ReqTime)
			fmt.Printf("stats- %s \n", statInfos.RspTime)
			fmt.Printf("stats- %s \n", statInfos.ClientIp)
			fmt.Printf("stats- %s \n", statInfos.DevInfo)
			fmt.Printf("stats- %s \n", statInfos.OsInfo)
			fmt.Printf("stats- %s \n", statInfos.NwInfo)
			fmt.Printf("stats- %s \n", statInfos.SvcName)
			fmt.Printf("stats- %s \n", statInfos.DevModel)
			fmt.Printf("stats- %s \n", statInfos.CarrierType)
			fmt.Printf("stats- %s \n", statInfos.HostName)
			fmt.Printf("stats- %s \n", statInfos.ScnName)
			fmt.Printf("stats- %s \n", statInfos.CallId)
			fmt.Printf("stats- %s \n", statInfos.TrId)
			fmt.Printf("stats- %s \n", statInfos.ConcCall)
			fmt.Printf("stats- %s \n", statInfos.SvcType)
			fmt.Printf("stats- %s \n", statInfos.SvcReqTime)
			fmt.Printf("stats- %s \n", statInfos.SvcRspTime)
			fmt.Printf("stats- %s \n", statInfos.ResultMsg)
			fmt.Printf("stats- %s \n", statInfos.SttText)
			fmt.Printf("stats- %s \n", statInfos.TtsText)
			// l.Printf(icslog.LOG_LEVEL_INFO, -1, "Recved Stat Command(%s) %d", ttsHeader, rlen)
		}
	}
}

func (statInfos *StatInfos) ChangeByteToString(statInfo []byte) {
	// n := bytes.Index(statInfo[STAT_INFO:SEQ_ID], []byte{0})
	statInfos.SeqId = string(statInfo[STAT_INFO:SEQ_ID])
	// fmt.Println("seqid ", statInfo[STAT_INFO : SEQ_ID])

	// n := bytes.Index(statInfo[SEQ_ID:LOG_TIME], []byte{0})
	statInfos.LogTime = string(statInfo[SEQ_ID:LOG_TIME])
	// fmt.Println("seqid ", statInfo[SEQ_ID : SEQ_ID+n])

	// n = bytes.Index(statInfo[LOG_TIME:LOG_TYPE], []byte{0})
	statInfos.LogType = string(statInfo[LOG_TIME:LOG_TYPE])
	// fmt.Println("seqid ", statInfo[SEQ_ID : SEQ_ID+n])

	// n = bytes.Index(statInfo[LOG_TYPE:SID], []byte{0})
	statInfos.Sid = string(statInfo[LOG_TYPE:SID])

	// n = bytes.Index(statInfo[SID:RESULT_CODE], []byte{0})
	statInfos.ResultCode = string(statInfo[SID:RESULT_CODE])

	// n = bytes.Index(statInfo[RESULT_CODE:REQ_TIME], []byte{0})
	statInfos.ReqTime = string(statInfo[RESULT_CODE:REQ_TIME])

	// n = bytes.Index(statInfo[REQ_TIME:RES_TIME], []byte{0})
	statInfos.RspTime = string(statInfo[REQ_TIME:RES_TIME])

	// n = bytes.Index(statInfo[RES_TIME:CLIENT_IP], []byte{0})
	statInfos.ClientIp = string(statInfo[RES_TIME:CLIENT_IP])

	// n = bytes.Index(statInfo[CLIENT_IP:DEV_INFO], []byte{0})
	statInfos.DevInfo = string(statInfo[CLIENT_IP:DEV_INFO])

	// n = bytes.Index(statInfo[DEV_INFO:OS_INFO], []byte{0})
	statInfos.OsInfo = string(statInfo[DEV_INFO:OS_INFO])

	// n = bytes.Index(statInfo[OS_INFO:NW_INFO], []byte{0})
	statInfos.NwInfo = string(statInfo[OS_INFO:NW_INFO])

	// n = bytes.Index(statInfo[NW_INFO:SVC_NAME], []byte{0})
	statInfos.SvcName = string(statInfo[NW_INFO:SVC_NAME])

	// n = bytes.Index(statInfo[SVC_NAME:DEV_MODEL], []byte{0})
	statInfos.DevModel = string(statInfo[SVC_NAME:DEV_MODEL])

	// n = bytes.Index(statInfo[DEV_MODEL:CARRIER_TYPE], []byte{0})
	statInfos.CarrierType = string(statInfo[DEV_MODEL:CARRIER_TYPE])

	// n = bytes.Index(statInfo[CARRIER_TYPE:HOST_NAME], []byte{0})
	statInfos.HostName = string(statInfo[CARRIER_TYPE:HOST_NAME])

	// n = bytes.Index(statInfo[HOST_NAME:SCN_NAME], []byte{0})
	statInfos.ScnName = string(statInfo[HOST_NAME:SCN_NAME])

	// n = bytes.Index(statInfo[SCN_NAME:CALL_ID], []byte{0})
	statInfos.CallId = string(statInfo[SCN_NAME:CALL_ID])

	// n = bytes.Index(statInfo[CALL_ID:TR_ID], []byte{0})
	statInfos.TrId = string(statInfo[CALL_ID:TR_ID])

	// n = bytes.Index(statInfo[TR_ID:CONC_CALL], []byte{0})
	statInfos.ConcCall = string(statInfo[TR_ID:CONC_CALL])

	// n = bytes.Index(statInfo[CONC_CALL:SVC_TYPE], []byte{0})
	statInfos.SvcType = string(statInfo[CONC_CALL:SVC_TYPE])

	// n = bytes.Index(statInfo[SVC_TYPE:SVC_REQ_TIME], []byte{0})
	statInfos.SvcReqTime = string(statInfo[SVC_TYPE:SVC_REQ_TIME])

	// n = bytes.Index(statInfo[SVC_REQ_TIME:SVC_RSP_TIME], []byte{0})
	statInfos.SvcRspTime = string(statInfo[SVC_REQ_TIME:SVC_RSP_TIME])

	// n = bytes.Index(statInfo[SVC_RSP_TIME:RESULT_MSG], []byte{0})
	statInfos.ResultMsg = string(statInfo[SVC_RSP_TIME:RESULT_MSG])

	// n = bytes.Index(statInfo[RESULT_MSG:STT_TEXT], []byte{0})
	statInfos.SttText = string(statInfo[RESULT_MSG:STT_TEXT])

	// n = bytes.Index(statInfo[STT_TEXT:TTS_TEXT], []byte{0})
	statInfos.TtsText = string(statInfo[STT_TEXT:])

}
