package siptemplate

import (
	"fmt"
	"sync"
)

type SIPTmpl struct {
	Template []string
}

const (
	INVITETMPL_ID = iota
	ACKTMPL_ID
	BYETMPL_ID
	OK200INVITETMPL_ID
	OK200BYETMPL_ID
	REGI_ID
	MAX_ID
)

var (
	inviteTmpl      string
	ackTmpl         string
	byeTmpl         string
	ok200InviteTmpl string
	ok200ByeTmpl    string
	regiTmpl        string
)

func init() {
	inviteTmpl = fmt.Sprint("INVITE sip:%s@192.168.0.21:3000;transport=udp SIP/2.0\r\n" +
		"Via: SIP/2.0/UDP 192.168.0.21:5090;branch=z9hG4bKac596453013\r\n" +
		"Max-Forwards: 69\r\n" +
		// "From: <sip:%s@192.168.0.21>;tag=1c867251551\r\n" +
		"From: <sip:%s@192.168.0.21>;tag=1c867251551\r\n" +
		"To: <sip:%s@192.168.0.21;user=phone>\r\n" +
		"Call-ID: %s\r\n" +
		"CSeq: 1 INVITE\r\n" +
		"Contact: <sip:%s@192.168.0.21:3000>\r\n" +
		"Supported: em,100rel,timer,replaces,path,resource-priority,sdp-anat\r\n" +
		"Allow: REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE\r\n" +
		"User-Agent: M800B/v.7.20A.204.759\r\n" +
		"Content-Type: application/sdp\r\n" +
		"Content-Length: %d\r\n" +
		"\r\n" +
		"v=0\r\n" +
		"o=AudiocodesGW 1879481090 705321541 IN IP4 192.168.0.21\r\n" +
		"s=Phone-Call\r\n" +
		// "c=IN IP4 192.168.0.222\r\n" + // IP4는 로컬이 아니라서 고정을 해도될듯
		"c=IN IP4 %s\r\n" +
		"t=0 0\r\n" +
		"m=audio %d RTP/AVP 8 0 18 101\r\n" +
		// "m=audio %d RTP/AVP 8 0 18 101\r\n" +
		"a=ptime:20\r\n" +
		"a=sendrecv\r\n" +
		"a=rtpmap:8 PCMA/8000\r\n" +
		"a=rtpmap:0 PCMU/8000\r\n" +
		"a=rtpmap:18 G729/8000\r\n" +
		"a=fmtp:18 annexb=no\r\n" +
		"a=rtpmap:101 telephone-event/8000\r\n" +
		"a=fmtp:101 0-15")

	ackTmpl = fmt.Sprint("ACK sip:%s@192.168.0.21:5090;transport=udp SIP/2.0\r\n" +
		"Via: SIP/2.0/UDP 192.168.0.21:5090;branch=z9hG4bKac390123597\r\n" +
		"Max-Forwards: 69\r\n" +
		"From: <sip:%s@192.168.0.221>;tag=1c867251551\r\n" +
		"To: <sip:%s@192.168.0.21;user=phone>;tag=MTY0OTExODIxODQxMzQ5NTY1Mw--\r\n" +
		"Call-ID: %s\r\n" +
		"CSeq: 1 ACK\r\n" +
		"Contact: <sip:%s@192.168.0.21:5090>\r\n" +
		"Supported: em,timer,replaces,path,resource-priority\r\n" +
		"Allow: REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE\r\n" +
		"User-Agent: M800B/v.7.20A.204.759\r\n" +
		"Content-Length: 0")

	byeTmpl = fmt.Sprint("BYE sip:%s@192.168.0.21:5090;transport=udp SIP/2.0\r\n" +
		"Via: SIP/2.0/UDP 192.168.0.222:5090;branch=z9hG4bKac1246799505\r\n" +
		"Max-Forwards: 69\r\n" +
		"From: <sip:%s@192.168.0.221>;tag=1c867251551\r\n" +
		"To: <sip:%s@192.168.0.21;user=phone>;tag=MTY0OTExODIxODQxMzQ5NTY1Mw--\r\n" +
		"Call-ID: %s\r\n" +
		"CSeq: 2 BYE\r\n" +
		"Supported: em,timer,replaces,path,resource-priority\r\n" +
		"Allow: REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE\r\n" +
		"User-Agent: M800B/v.7.20A.204.759\r\n" +
		"Reason: Q.850 ;cause=16\r\n" +
		"Content-Length: 0")

	ok200InviteTmpl = fmt.Sprint("SIP/2.0 200 OK\r\n" +
		"Via: SIP/2.0/UDP 192.168.0.21:5090;branch=z9hG4bKac596453013\r\n" +
		"From: <sip:%s@192.168.0.221>;tag=1c867251551\r\n" +
		"To: <sip:%s@192.168.0.21;user=phone>;tag=MTY0OTExODIxODQxMzQ5NTY1Mw--\r\n" +
		"Call-ID: %s\r\n" +
		"CSeq: 1 INVITE\r\n" +
		"Contact: <sip:%s@%d:%d;transport=udp>\r\n" +
		// "Contact: <sip:%s@192.168.0.21:5090;transport=udp>\r\n" +
		"Allow: REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE\r\n" +
		"Supported: timer, path, replaces, sdp-anat\r\n" +
		"User-Agent: iCOMSYS_CALLAGENT\r\n" +
		"Content-Type: application/sdp\r\n" +
		"Content-Length: 226\r\n" +
		"\r\n" +
		"v=0\r\n" +
		"o=iCOMSYS-AGENT 1649118218 1649118219 IN IP4 192.168.0.21\r\n" +
		"s=\r\n" +
		// "c=IN IP4 192.168.0.21\r\n" +
		"c=IN IP4 %s\r\n" +
		"t=0 0\r\n" +
		"m=audio 21000 RTP/AVP 8 101 \r\n" +
		"a=rtpmap:8 PCMA/8000\r\n" +
		"a=ptime:20\r\n" +
		"a=sendrecv\r\n" +
		"a=rtpmap:101 telephone-event/8000\r\n" +
		"a=fmtp:101 0-15")

	ok200ByeTmpl = fmt.Sprint("SIP/2.0 200 OK\r\n" +
		"Via: SIP/2.0/UDP 192.168.0.21:5090;branch=z9hG4bKac1520651703\r\n" +
		"From: <sip:%s@192.168.0.21>;tag=1c1838414031\r\n" +
		"To: <sip:%s@192.168.0.21;user=phone>;tag=MTY0ODQyNzQ2NTU4MzMzNDYyMg--\r\n" +
		"Call-ID: %s\r\n" +
		"CSeq: 2 BYE\r\n" +
		"Contact: <sip:%s@192.168.0.21:5090;transport=udp>\r\n" +
		"Allow: REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE\r\n" +
		"User-Agent: iCOMSYS_CALLAGENT\r\n")

	regiTmpl = fmt.Sprint("SIP/2.0 200 OK\r\n" +
		"Via: SIP/2.0/UDP 192.168.0.21;received=192.168.0.21;rport=5090;branch=ZTg1OTUyNWQtMTc3NC00YjJmLWIyZWUtZjg5OTFkOGU0OTU5\r\n" +
		"From: <sip:%s@192.168.0.21>;tag=MTY0ODcwMDU1MzQ5OTM3NzQ2Mw--\r\n" +
		"To: <sip:%s@192.168.0.21>;tag=1c2023025228\r\n" +
		"Call-ID: %s@192.168.0.21\r\n" +
		"CSeq: 0 REGISTER\r\n" +
		"Contact: <sip:%s@192.168.0.21:5090;transport=udp>;expires=3600\r\n" +
		"Expires: 3600\r\n" +
		"Content-Length: 0\r\n\r\n")
}

var onceTemplate sync.Once
var gTemplate *SIPTmpl

func GetTemplate() *SIPTmpl {
	return gTemplate
}

func Init() {
	onceTemplate.Do(func() {
		gTemplate = &SIPTmpl{}

		gTemplate.Template = make([]string, MAX_ID)
		gTemplate.Template[INVITETMPL_ID] = inviteTmpl
		gTemplate.Template[ACKTMPL_ID] = ackTmpl
		gTemplate.Template[BYETMPL_ID] = byeTmpl
		gTemplate.Template[OK200INVITETMPL_ID] = ok200InviteTmpl
		gTemplate.Template[OK200BYETMPL_ID] = ok200ByeTmpl
		gTemplate.Template[REGI_ID] = regiTmpl
	})
}

func (s *SIPTmpl) String(id int) string {
	return s.Template[id]
}
