package icsws

import (
	"fmt"
	"time"
)

type IAP struct {
	message string
}

func NewIAP() IAP {
	i := IAP{}

	return i
}

func (i *IAP) String() string {
	return i.message
}

func (i *IAP) Connect() string {
	i.message = "[\"CONNECT\\naccept-version:1.1,1.0\\nheart-beat:10000,10000\\n\\n\\u0000\"]"
	//fmt.Println(i.message)
	return i.message
}

func (i *IAP) Disconnect() string {
	i.message = "[\"DISCONNECT\\n\\n\\u0000\"]"
	//fmt.Println(i.message)
	return i.message
}

func (i *IAP) Subscribe(agentID string) string {
	i.message = fmt.Sprintf("[\"SUBSCRIBE\\nid:sub-0\\ndestination:/voice-agent/command/%s\\n\\n\\u0000\"]", agentID)
	//fmt.Println(i.message)
	return i.message
}

func (i *IAP) RoomIn(roomID, agentID string) string {
	rmsg := fmt.Sprintf("{\"room_id\":\"%s\",\"room_status\":\"IN\",\"counsellor\":\"%s\"}", roomID, agentID)
	msg := fmt.Sprintf("{\\\"room_id\\\":\\\"%s\\\",\\\"room_status\\\":\\\"IN\\\",\\\"counsellor\\\":\\\"%s\\\"}", roomID, agentID)
	//msg := fmt.Sprintf("{\\\"room_id\\\":\\\"%s\\\",\\\"room_status\\\":\\\"IN\\\",\\\"counsellor\\\":\\\"%s\\\"}", roomID, agentID)
	bmsg := []byte(rmsg)
	i.message = fmt.Sprintf("[\"SEND\\ndestination:/set/room\\ncontent-length:%d\\n\\n%s\\u0000\"]", len(bmsg), msg)
	//i.message = fmt.Sprintf("SEND\ndestination:/set/room\ncontent-length:61\n\n{\"room_id\":\":\"%s\",\"room_status\":\"IN\",\"counsellor\":\"user-001\"}\u0000", roomID)

	//fmt.Println(i.message)
	return i.message
}

func (i *IAP) RoomOut(roomID, agentID string) string {
	rmsg := fmt.Sprintf("{\"room_id\":\"%s\",\"room_status\":\"OUT\",\"counsellor\":\"%s\"}", roomID, agentID)
	msg := fmt.Sprintf("{\\\"room_id\\\":\\\"%s\\\",\\\"room_status\\\":\\\"OUT\\\",\\\"counsellor\\\":\\\"%s\\\"}", roomID, agentID)
	bmsg := []byte(rmsg)
	i.message = fmt.Sprintf("[\"SEND\\ndestination:/set/room\\ncontent-length:%d\\n\\n%s\\u0000\"]", len(bmsg), msg)

	//fmt.Println(i.message)
	return i.message
}

func (i *IAP) CallStart(roomID string, custTel string) string {
	rmsg := fmt.Sprintf("{\"timestamp\":%d,\"room_id\":\"%s\",\"call_status\":\"START\"}", time.Now().UnixNano()/1e6, roomID)
	msg := fmt.Sprintf("{\\\"timestamp\\\":%d,\\\"room_id\\\":\\\"%s\\\",\\\"call_status\\\":\\\"START\\\"}", time.Now().UnixNano()/1e6, roomID)
	//msg := fmt.Sprintf("{\\\"timestamp\\\":%d,\\\"room_id\\\":\\\"%s\\\",\\\"call_status\\\":\\\"START\\\",\\\"customer_phone_number\\\":\\\"%s\\\"}", time.Now().UnixNano()/1e6, roomID, custTel)
	bmsg := []byte(rmsg)
	i.message = fmt.Sprintf("[\"SEND\\ndestination:/set/call\\ncontent-length:%d\\n\\n%s\\u0000\"]", len(bmsg), msg)

	//fmt.Println(i.message)
	return i.message
}

func (i *IAP) CallEnd(roomID string, custTel string) string {
	rmsg := fmt.Sprintf("{\"timestamp\":%d,\"room_id\":\"%s\",\"call_status\":\"END\"}", time.Now().UnixNano()/1e6, roomID)
	msg := fmt.Sprintf("{\\\"timestamp\\\":%d,\\\"room_id\\\":\\\"%s\\\",\\\"call_status\\\":\\\"END\\\"}", time.Now().UnixNano()/1e6, roomID)
	//msg := fmt.Sprintf("{\\\"timestamp\\\":%d,\\\"room_id\\\":\\\"%s\\\",\\\"call_status\\\":\\\"END\\\",\\\"customer_phone_number\\\":\\\"%s\\\"}", time.Now().UnixNano()/1e6, roomID, custTel)
	bmsg := []byte(rmsg)
	i.message = fmt.Sprintf("[\"SEND\\ndestination:/set/call\\ncontent-length:%d\\n\\n%s\\u0000\"]", len(bmsg), msg)

	//fmt.Println(i.message)
	return i.message
}

func (i *IAP) SendMessage(txrx, roomID, message string, seq int) string {
	var msg string
	var rmsg string
	if len(message) == 0 {
		rmsg = fmt.Sprintf("{\"timestamp\":%d,\"room_id\":\"%s\",\"seq\":%d,\"txrx\":\"%s\"}",
			time.Now().UnixNano()/1e6, roomID, seq, txrx)
		msg = fmt.Sprintf("{\\\"timestamp\\\":%d,\\\"room_id\\\":\\\"%s\\\",\\\"seq\\\":%d,\\\"txrx\\\":\\\"%s\\\"}",
			time.Now().UnixNano()/1e6, roomID, seq, txrx)
	} else {
		rmsg = fmt.Sprintf("{\"timestamp\":%d,\"room_id\":\"%s\",\"seq\":%d,\"txrx\":\"%s\",\"message\":\"%s\"}",
			time.Now().UnixNano()/1e6, roomID, seq, txrx, message)
		msg = fmt.Sprintf("{\\\"timestamp\\\":%d,\\\"room_id\\\":\\\"%s\\\",\\\"seq\\\":%d,\\\"txrx\\\":\\\"%s\\\",\\\"message\\\":\\\"%s\\\"}",
			time.Now().UnixNano()/1e6, roomID, seq, txrx, message)
	}
	bmsg := []byte(rmsg)
	i.message = fmt.Sprintf("[\"SEND\\ndestination:/set/counseling/message\\ncontent-length:%d\\n\\n%s\\u0000\"]",
		len(bmsg), msg)

	//fmt.Println(i.message)
	return i.message
}
