package model

import "strings"

type PresenceStateChange struct {
	Event      string `json:"event"`
	Status     string `json:"status"`
	Message    string `json:"message"`
	Presentity string `json:"presentity"`
	Extension  string `json:"extension"`
	DndStatus  string `json:"dndStatus"`
}

type Hangup struct {
	AccountCode       string `json:"accountCode"`
	CallerIDName      string `json:"callerIDName"`
	CallerIDNum       string `json:"callerIDNumber"`
	Cause             string `json:"cause"`
	Causetxt          string `json:"causetxt"`
	Channel           string `json:"channel"`
	ChannelState      string `json:"channelState"`
	ChannelStateDesc  string `json:"channelStateDesc"`
	ConnectedLineName string `json:"connectedLineName"`
	ConnectedLineNum  string `json:"connectedLineNumber"`
	Context           string `json:"context"`
	Event             string `json:"event"`
	Exten             string `json:"exten"`
	Linkedid          string `json:"linkedid"`
	Priority          string `json:"priority"`
	Privilege         string `json:"privilege"`
	Timestamp         string `json:"timestamp"`
	Uniqueid          string `json:"uniqueid"`
}

type Newstate struct {
	AccountCode       string `json:"accountCode"`
	CallerIDName      string `json:"callerIDName"`
	CallerIDNum       string `json:"callerIDNumber"`
	Channel           string `json:"channel"`
	ChannelState      string `json:"channelState"`
	ChannelStateDesc  string `json:"channelStateDesc"`
	ConnectedLineName string `json:"connectedLineName"`
	ConnectedLineNum  string `json:"connectedLineNumber"`
	Context           string `json:"context"`
	Event             string `json:"event"`
	Exten             string `json:"exten"`
	Language          string `json:"language"`
	Linkedid          string `json:"linkedid"`
	Priority          string `json:"priority"`
	Privilege         string `json:"privilege"`
	Timestamp         string `json:"timestamp"`
	Uniqueid          string `json:"uniqueid"`
}

type SuccessfulAuth struct {
	Event         string `json:"event"`
	AccountID     string `json:"accountID"`
	RemoteAddress string `json:"remoteAddress"`
	Address       string `json:"address"`
}

func (event *SuccessfulAuth) GetAddress() {
	data := strings.Split(event.RemoteAddress, "/")
	if len(data) == 4 {
		event.Address = data[2]
	} else {
		event.Address = event.RemoteAddress
	}
}

type ContactStatus struct {
	Event         string `json:"event"`
	URI           string `json:"url"`
	ContactStatus string `json:"contactStatus"`
	Address       string `json:"address"`
	AOR           string `json:"extension"`
	RoundtripUsec string `json:"roundtripUsec"`
}

func (event *ContactStatus) GetAddress() {
	data := strings.Split(event.URI, "@")
	if len(data) == 2 {
		event.Address = strings.Split(data[1], ":")[0]
	}
}

type MessageWaiting struct {
	Event     string `json:"event"`
	Extension string `json:"extension"`
	New       string `json:"new"`
	Old       string `json:"old"`
	Mailbox   string `json:"mainbox"`
}

func (event *MessageWaiting) GetExtension() {
	data := strings.Split(event.Mailbox, "@")
	if len(data) == 2 {
		event.Extension = data[0]
	}
}
