package responsecs

import (
	"encoding/binary"
	"unsafe"
)

const (
	RC_SUCCESS = iota
	RC_FAIL
)

const (
	TTS_COMMAND = iota
	REFER_COMMAND
	BYE_COMMAND
	BARGEIN_COMMAND
	DTMF_COMMAND
)

type Response struct {
	agentName  string
	telNo      string
	command    uint32
	resultCode uint32
	payloadLen uint64
}

func NewResponse(agentname string, telno string, command int, rc int, payloadLength int) *Response {
	r := Response{agentName: agentname, telNo: telno, command: uint32(command), resultCode: uint32(rc), payloadLen: uint64(payloadLength)}
	return &r
}

func (r Response) GetData() []byte {
	agentNameLen := 20
	telNoLen := 50
	commandLen := int(unsafe.Sizeof(r.command))
	rcLen := int(unsafe.Sizeof(r.resultCode))
	plLen := int(unsafe.Sizeof(r.payloadLen))

	retBin := make([]byte, commandLen+rcLen+plLen+agentNameLen+telNoLen)

	commandBin := make([]byte, commandLen)
	binary.LittleEndian.PutUint32(commandBin, uint32(r.command))

	rcBin := make([]byte, rcLen)
	binary.LittleEndian.PutUint32(rcBin, uint32(r.resultCode))

	plBin := make([]byte, plLen)
	binary.LittleEndian.PutUint64(plBin, uint64(r.payloadLen))

	agentNameBin := []byte(r.agentName)
	telNoBin := []byte(r.telNo)

	copy(retBin, commandBin)
	copy(retBin[commandLen:], rcBin)
	copy(retBin[commandLen+rcLen:], plBin)
	copy(retBin[commandLen+rcLen+plLen:], agentNameBin)
	copy(retBin[commandLen+rcLen+plLen+agentNameLen:], telNoBin)

	return retBin
}