package main

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"
)

type crmdMeetingAttendance struct {
	Status string `json:"status,omitempty"`
}

type crmdMeeting struct {
	crmdEntityBase
	Status    string `json:"status,omitempty"`
	DateStart string `json:"dateStart,omitempty"`
	DateEnd   string `json:"dateEnd,omitempty"`
	Duration  int    `json:"duration,omitempty"`
	Reminders []struct {
		Seconds int    `json:"seconds,omitempty"`
		Type    string `json:"type,omitempty"`
	} `json:"reminders,omitempty"`
	Venue      string `json:"venue,omitempty"`
	ParentID   string `json:"parentId,omitempty"`
	ParentType string `json:"parentType,omitempty"`
	ParentName string `json:"parentName,omitempty"`

	AccountID   string `json:"accountId,omitempty"`
	AccountName string `json:"accountName,omitempty"`

	//users, attendance
	UsersIds     []string                         `json:"usersIds,omitempty"`
	UsersNames   map[string]string                `json:"usersNames,omitempty"`
	UsersColumns map[string]crmdMeetingAttendance `json:"usersColumns"`
	//contacts, attendance
	ContactsIds     []string                         `json:"contactsIds,omitempty"`
	ContactsNames   map[string]string                `json:"contactsNames,omitempty"`
	ContactsColumns map[string]crmdMeetingAttendance `json:"contactsColumns,omitempty"`
	//lead, attendance
	LeadsIds     []string                         `json:"leadsIds,omitempty"`
	LeadsNames   map[string]string                `json:"leadsNames,omitempty"`
	LeadsColumns map[string]crmdMeetingAttendance `json:"leadsColumns"`
	//cover
	CoverID   string `json:"coverId,omitempty"`
	CoverName string `json:"coverName,omitempty"`

	//for web spa operation
	spaErrMsg []string
}

func (m crmdMeeting) save() (newMeeting crmdMeeting, err error) {
	jsonB, err := json.Marshal(m)
	if err != nil {
		return
	}

	if m.ID == "" { //create
		entity, e := crmCreateEntity("Meeting", jsonB)
		if e != nil {
			err = e
			return
		}
		newMeeting = entity.(crmdMeeting)

	} else { //patch
		entity, e := crmUpdateEntity("Meeting", m.ID, jsonB)
		if e != nil {
			err = e
			return
		}
		newMeeting = entity.(crmdMeeting)
	}
	return
}

func (m *crmdMeeting) clearSpaErr(e string) {
	m.spaErrMsg = []string{}
}

func (m *crmdMeeting) addSpaErr(e string) {
	m.spaErrMsg = append(m.spaErrMsg, e)
}

func (m crmdMeeting) ErrorMessage() string {
	v := strings.Join(m.spaErrMsg, "<br>")
	return v
}

func (m crmdMeeting) hasError() bool {
	return len(m.spaErrMsg) > 0
}

func (m crmdMeeting) CoverCacheURL() string {
	if m.CoverID != "" {
		u := CRMConfig.CacheSiteURL + "?a=" + m.CoverID
		return buildSignatureAppend2Url(u, IntraAPIConfig.CRMSecrete)
	}
	return GlobalPath.ThisSiteURL + "spa/assets/img/meetingdefault.jpg"
}

func crmGetMeeting(id string) (ret crmdMeeting, err error) {
	entity, err := crmGetEntity("Meeting", id)
	if err != nil {
		return
	}

	ret = entity.(crmdMeeting)
	return
}

func (m crmdMeeting) StartDate() string {
	t, _ := time.Parse(getCrmTimeLayout(), m.DateStart)
	AEST, _ := time.LoadLocation("Australia/Sydney")
	return t.In(AEST).Format("02 January, 2006")
}

func (m crmdMeeting) StartHour() string {
	AEST, _ := time.LoadLocation("Australia/Sydney")
	t, _ := time.Parse(getCrmTimeLayout(), m.DateStart)
	return t.In(AEST).Format("15:04")
}

func (m crmdMeeting) DurationHour() string {
	hour := m.Duration / 3600
	minute := (m.Duration % 3600) / 60
	return fmt.Sprintf("%02d:%02d", hour, minute)
}
