package writecallsignal

import (
	"encoding/binary"
	"fmt"

	"gitlab.com/ics_cinnamon/voicegateway/recorddata"
)

type CallSignalData struct {
	stream []byte
}

const (
	//CALLSIGNAL_DATA_LEN = 61

	CHANNELID_POS  = 0 //13
	CHANNELID_LEN  = 13
	SERVERID_POS   = CHANNELID_POS + 13 //2
	SERVERID_LEN   = SERVERID_POS + 2
	AGENTID_POS    = SERVERID_POS + 2 //16
	AGENTID_LEN    = AGENTID_POS + 16
	STATION_POS    = AGENTID_POS + 16 //4
	STATION_LEN    = STATION_POS + 4
	CREATETIME_POS = STATION_POS + 4 //8
	CREATETIME_LEN = CREATETIME_POS + 8
	EVENTTYPE_POS  = CREATETIME_POS + 8 //1
	EVENTTYPE_LEN  = EVENTTYPE_POS + 1
	STARTTIME_POS  = EVENTTYPE_POS + 1 //8
	STARTTIME_LEN  = STARTTIME_POS + 8
	ENDTIME_POS    = STARTTIME_POS + 8 //8
	ENDTIME_LEN    = ENDTIME_POS + 8
	INOUT_POS      = ENDTIME_POS + 8 //1
	INOUT_LEN      = INOUT_POS + 1
	CUSTID_POS     = INOUT_POS + 1 //1
	CUSTID_LEN     = CUSTID_POS + 20
)

func NewCallSignalData() *CallSignalData {
	c := &CallSignalData{
		stream: make([]byte, recorddata.MAX_CALLSIGNAL_PACKET_LEN),
	}

	return c
}

func (c *CallSignalData) SetData(chanID string, serverID int, station string, custID string, startTS int64, endTS int64, event, inout string) {
	c.setChannelID(chanID)
	c.setServerID(serverID)
	agentid := fmt.Sprintf("agent%s", station)
	c.setAgentID(agentid)
	c.setStation(station)
	c.setCreateTime(startTS)
	c.setEventType(event)
	c.setStartTime(startTS)
	c.setEndTime(endTS)
	c.setInOut(inout)
	c.setCustID(custID)
}

func (c CallSignalData) GetData() []byte {
	return c.stream
}

func (c *CallSignalData) setChannelID(channelID string) {
	tmp := []byte(channelID)
	copy(c.stream[CHANNELID_POS:CHANNELID_LEN], tmp)
}

func (c CallSignalData) GetChannelID() []byte {
	return c.stream[CHANNELID_POS:CHANNELID_LEN]
}

func (c *CallSignalData) setServerID(serverID int) {
	id := fmt.Sprintf("%d", serverID)
	tmp := []byte(id)
	copy(c.stream[SERVERID_POS:SERVERID_LEN], tmp)
}

func (c CallSignalData) GetServerID() []byte {
	return c.stream[SERVERID_POS:SERVERID_LEN]
}

func (c *CallSignalData) setAgentID(agentID string) {
	tmp := []byte(agentID)
	copy(c.stream[AGENTID_POS:AGENTID_LEN], tmp)
}

func (c CallSignalData) GetAgentID() []byte {
	return c.stream[AGENTID_POS:AGENTID_LEN]
}

func (c *CallSignalData) setStation(station string) {
	//func (c *CallSignalData) setStation(station string) {
	//tmp := make([]byte, 4)
	//binary.LittleEndian.PutUint32(tmp, uint32(station))
	copy(c.stream[STATION_POS:STATION_LEN], []byte(station))
}

func (c CallSignalData) GetStation() []byte {
	return c.stream[STATION_POS:STATION_LEN]
}

func (c *CallSignalData) setCreateTime(createtime int64) {
	tmp := make([]byte, 8)
	binary.LittleEndian.PutUint64(tmp, uint64(createtime))
	copy(c.stream[CREATETIME_POS:CREATETIME_LEN], tmp)
}

func (c CallSignalData) GetCreateTime() []byte {
	return c.stream[CREATETIME_POS:CREATETIME_LEN]
}

func (c *CallSignalData) setEventType(eventtype string) {
	tmp := []byte(eventtype)
	copy(c.stream[EVENTTYPE_POS:EVENTTYPE_LEN], tmp)
}

func (c CallSignalData) GetEventType() []byte {
	return c.stream[EVENTTYPE_POS:EVENTTYPE_LEN]
}

func (c *CallSignalData) setStartTime(starttime int64) {
	tmp := make([]byte, 8)
	binary.LittleEndian.PutUint64(tmp, uint64(starttime))
	copy(c.stream[STARTTIME_POS:STARTTIME_LEN], tmp)
}

func (c CallSignalData) GetStartTime() []byte {
	return c.stream[STARTTIME_POS:STARTTIME_LEN]
}

func (c *CallSignalData) setEndTime(endtime int64) {
	tmp := make([]byte, 8)
	binary.LittleEndian.PutUint64(tmp, uint64(endtime))
	copy(c.stream[ENDTIME_POS:ENDTIME_LEN], tmp)
}

func (c CallSignalData) GetEndTime() []byte {
	return c.stream[ENDTIME_POS:ENDTIME_POS]
}

func (c *CallSignalData) setInOut(inout string) {
	var tmp []byte = make([]byte, 1)
	switch inout {
	case "10":
		//tmp = []byte("-1")
		u := -1
		tmp[0] = byte(u)
	case "11":
		//tmp = []byte("-2")
		u := -2
		tmp[0] = byte(u)
	case "12":
		//tmp = []byte("-3")
		u := -3
		tmp[0] = byte(u)
	case "13":
		//tmp = []byte("-4")
		u := -4
		tmp[0] = byte(u)
	case "14":
		//tmp = []byte("-5")
		u := -5
		tmp[0] = byte(u)
	case "15":
		//tmp = []byte("-6")
		u := -6
		tmp[0] = byte(u)
	default:
		tmp = []byte(inout)
	}
	copy(c.stream[INOUT_POS:INOUT_LEN], tmp)
	tmp = nil
}

func (c CallSignalData) GetInOut() []byte {
	return c.stream[INOUT_POS:INOUT_LEN]
}

func (c *CallSignalData) setCustID(custID string) {
	//fmt.Println(custID)
	tmp := []byte(custID)
	//fmt.Println(custID, tmp)
	copy(c.stream[CUSTID_POS:CUSTID_LEN], tmp)
}

func (c CallSignalData) GetCustID() []byte {
	return c.stream[CUSTID_POS:CUSTID_LEN]
}
