package main

import (
	"encoding/json"
	"log"
	"testing"
	"time"
)

func TestDecodeMeetingJson(t *testing.T) {
	msg := `
 {
    "id": "595d064a6e372fc1f",
    "name": "testmeting.",
    "deleted": false,
    "status": "Planned",
    "dateStart": "2017-07-05 15:30:00",
    "dateEnd": "2017-07-05 19:13:00",
    "duration": 13380,
    "reminders": [
        {
            "seconds": 0,
            "type": "Popup"
        },
        {
            "seconds": 18000,
            "type": "Email"
        }
    ],
    "description": "ddaadd",
    "createdAt": "2017-07-05 15:31:22",
    "modifiedAt": "2017-07-15 07:49:48",
    "googleCalendarEventId": null,
    "venue": "venue111",
    "parentId": "5961bfac40e1b4e69",
    "parentType": "Lead",
    "parentName": "Anonymous User 127.0.0.1:49261",
    "accountId": "aid",
    "accountName": "aname",
    "usersIds": [
        "1"
    ],
    "usersNames": {
        "1": "Admin"
    },
    "usersColumns": {
        "1": {
            "status": "Accepted"
        }
    },
    "contactsIds": [
        "595a292e87b0aa3a1"
    ],
    "contactsNames": {
        "595a292e87b0aa3a1": "test name"
    },
    "contactsColumns": {
        "595a292e87b0aa3a1": {
            "status": "None"
        }
    },
    "leadsIds": [
        "595a921c5032dbfa8",
        "595a920924289575a",
        "595a91074492662d9",
        "595a9105b0d8630ab",
        "595a91043fc61b85b",
        "595a9102dab66349c"
    ],
    "leadsNames": {
        "595a921c5032dbfa8": "  \u9b54\u9053\u5b50",
        "595a920924289575a": "  HIT",
        "595a91074492662d9": "  yang",
        "595a9105b0d8630ab": "  \u5b59\u8fd0\u5377\u5b50",
        "595a91043fc61b85b": "  Iphy",
        "595a9102dab66349c": "  \u6668\u516e"
    },
    "leadsColumns": {
        "595a921c5032dbfa8": {
            "status": "None1"
        },
        "595a920924289575a": {
            "status": "None2"
        },
        "595a91074492662d9": {
            "status": "None3"
        },
        "595a9105b0d8630ab": {
            "status": "None4"
        },
        "595a91043fc61b85b": {
            "status": "None5"
        },
        "595a9102dab66349c": {
            "status": "None6"
        }
    },
    "createdById": "1",
    "createdByName": "Admin",
    "modifiedById": "1",
    "modifiedByName": "Admin",
    "assignedUserId": "1",
    "assignedUserName": "Admin",
    "teamsIds": [
        "5958913a2479166db",
        "59589145af954bc38"
    ],
    "teamsNames": {
        "5958913a2479166db": "testteam1",
        "59589145af954bc38": "testteam2"
    },
    "googleCalendarId": null,
    "googleCalendarName": null,
    "coverId": "coverid",
    "coverName": "covername"
}
 `

	e := crmdMeeting{}
	err := json.Unmarshal([]byte(msg), &e)

	AssertEqual(t, err, nil, "decode json should be correct")
	AssertEqual(t, e.ID, "595d064a6e372fc1f", "ID mismatch")
	AssertEqual(t, e.Name, "testmeting.", "name mismatch")
	AssertEqual(t, e.Deleted, false, "deleted should be false")
	AssertEqual(t, e.DateStart, "2017-07-05 15:30:00", "dataStart wrong")
	AssertEqual(t, e.DateEnd, "2017-07-05 19:13:00", "dateEnd wrong")
	AssertEqual(t, e.Duration, 13380, "duration should be 13380")
	AssertEqual(t, e.Reminders[0].Seconds, 0, "reminder[0] seconds expect 0")
	AssertEqual(t, e.Reminders[0].Type, "Popup", "reminder[0] type expect popup")
	AssertEqual(t, e.Reminders[1].Seconds, 18000, "reminder[1] seconds expect 18000")
	AssertEqual(t, e.Reminders[1].Type, "Email", "reminder[1] type expect email")
	AssertEqual(t, e.Description, "ddaadd", "description not right")
	AssertEqual(t, e.CreatedAt, "2017-07-05 15:31:22", "createdAt wrong")
	AssertEqual(t, e.ModifiedAt, "2017-07-15 07:49:48", "modifiedAt wrong")
	AssertEqual(t, e.Venue, "venue111", "venu should be correct")
	AssertEqual(t, e.ParentID, "5961bfac40e1b4e69", "parentID mismatch")
	AssertEqual(t, e.ParentName, "Anonymous User 127.0.0.1:49261", "parentName mismatch")
	AssertEqual(t, e.ParentType, "Lead", "parent type should be Lead")
	AssertEqual(t, e.AccountID, "aid", "accountid should be aid")
	AssertEqual(t, e.AccountName, "aname", "accountName should be aname")
	AssertEqual(t, e.UsersIds[0], "1", "usersIDs[0] should be 1")
	AssertEqual(t, e.UsersNames["1"], "Admin", "usersNames[1]!=Admin")
	AssertEqual(t, e.UsersColumns["1"].Status, "Accepted", "status should be accepted")
	AssertEqual(t, e.ContactsIds[0], "595a292e87b0aa3a1", "contactsids[0] wrong")
	AssertEqual(t, e.ContactsNames["595a292e87b0aa3a1"], "test name", "contact name wrong")
	AssertEqual(t, e.ContactsColumns["595a292e87b0aa3a1"].Status, "None", "contact status should be none")

	AssertEqual(t, e.LeadsIds[0], "595a921c5032dbfa8", "leadsIDs[0] wrong")
	AssertEqual(t, e.LeadsIds[1], "595a920924289575a", "leadsIDs[1] wrong")
	AssertEqual(t, e.LeadsIds[2], "595a91074492662d9", "leadsIDs[2] wrong")
	AssertEqual(t, e.LeadsIds[3], "595a9105b0d8630ab", "leadsIDs[3] wrong")
	AssertEqual(t, e.LeadsIds[4], "595a91043fc61b85b", "leadsIDs[4] wrong")
	AssertEqual(t, e.LeadsIds[5], "595a9102dab66349c", "leadsIDs[5] wrong")

	AssertEqual(t, e.LeadsNames["595a921c5032dbfa8"], "  \u9b54\u9053\u5b50", "leads[0] name wrong")
	AssertEqual(t, e.LeadsNames["595a920924289575a"], "  HIT", "leads[1] name wrong")
	AssertEqual(t, e.LeadsNames["595a91074492662d9"], "  yang", "leads[2] name wrong")
	AssertEqual(t, e.LeadsNames["595a9105b0d8630ab"], "  \u5b59\u8fd0\u5377\u5b50", "leads[3] name wrong")
	AssertEqual(t, e.LeadsNames["595a91043fc61b85b"], "  Iphy", "leads[4] name wrong")
	AssertEqual(t, e.LeadsNames["595a9102dab66349c"], "  \u6668\u516e", "leads[5] name wrong")

	AssertEqual(t, e.LeadsColumns["595a921c5032dbfa8"].Status, "None1", "")
	AssertEqual(t, e.LeadsColumns["595a920924289575a"].Status, "None2", "")
	AssertEqual(t, e.LeadsColumns["595a91074492662d9"].Status, "None3", "")
	AssertEqual(t, e.LeadsColumns["595a9105b0d8630ab"].Status, "None4", "")
	AssertEqual(t, e.LeadsColumns["595a91043fc61b85b"].Status, "None5", "")
	AssertEqual(t, e.LeadsColumns["595a9102dab66349c"].Status, "None6", "")

	AssertEqual(t, e.CreatedByID, "1", "createdBy id wrong ")
	AssertEqual(t, e.CreatedByName, "Admin", "createdByName")
	AssertEqual(t, e.ModifiedByID, "1", "modified id !=1")
	AssertEqual(t, e.ModifiedByName, "Admin", "modifiedByName wrong")
	AssertEqual(t, e.AssignedUserID, "1", "assigned userid != 1")
	AssertEqual(t, e.AssignedUserName, "Admin", "assigned name != Admin")
	AssertEqual(t, e.TeamsIDs[0], "5958913a2479166db", "team[0] wrong")
	AssertEqual(t, e.TeamsIDs[1], "59589145af954bc38", "team[1] wrong")
	AssertEqual(t, e.TeamsNames["5958913a2479166db"], "testteam1", "")
	AssertEqual(t, e.TeamsNames["59589145af954bc38"], "testteam2", "")
	AssertEqual(t, e.CoverID, "coverid", "")
	AssertEqual(t, e.CoverName, "covername", "")

}

func TestCreateMeeting(t *testing.T) {

	lead := crmdLead{}
	lead.FirstName = "ff" + RandStringRunes(10)
	lead.LastName = "ll" + RandStringRunes(10)
	lead.Status = "testonly"
	lead.Password = "apa"
	lead.ForceDuplicate = true
	newLead, err := lead.Save()
	AssertEqual(t, err, nil, "create temp lead for meeting test should be ok")
	AssertEqual(t, newLead.ID != "", true, "temp new lead should have ID")

	meeting := crmdMeeting{}
	meeting.Name = "至高无上的会议"
	meeting.DateStart = time.Now().Format(getCrmTimeLayout())
	meeting.Duration = 3600 //1hr
	meeting.Description = "一些无聊的文字，凑在一起"
	meeting.ParentID = newLead.ID
	meeting.ParentType = "Lead"

	newMeeting, err := meeting.save()
	AssertEqual(t, err, nil, "create meeting should be successful")
	AssertEqual(t, newMeeting.ID != "", true, "newly created meeting should have ID")

	AssertEqual(t, newMeeting.Name, meeting.Name, "meeting name test fail")
	AssertEqual(t, newMeeting.DateStart, meeting.DateStart, "meeting dateSTart test fail")
	AssertEqual(t, newMeeting.Duration, meeting.Duration, "meeting duration test fail")
	AssertEqual(t, newMeeting.Description, meeting.Description, "meeting Description test fail")
	AssertEqual(t, newMeeting.ParentID, meeting.ParentID, "meeting parentID test fail")
	AssertEqual(t, newMeeting.ParentType, meeting.ParentType, "meeting ParentType test fail")
	AssertEqual(t, newMeeting.Status, "Planned", "meeting status is planned")

	AssertEqual(t, newMeeting.ParentName, "", "parentName not expected to be updated")
	entity, err := crmGetEntity("Meeting", newMeeting.ID)
	AssertEqual(t, err, nil, "re read saved meeting should be ok")
	anotherRead, ok := entity.(crmdMeeting)
	AssertEqual(t, ok, true, "reading entity shold be crmdMeeting")
	AssertEqual(t, anotherRead.ParentName, newLead.Name, "")

	deleted, err := crmDeleteEntity("Meeting", newMeeting.ID)
	AssertEqual(t, err, nil, "delet testmeeting should be successful")
	AssertEqual(t, deleted, true, "temp meeting should have been deleted")

	deleted, err = crmDeleteEntity("Lead", newLead.ID)
	AssertEqual(t, err, nil, "delet tmp lead should be successful")
	AssertEqual(t, deleted, true, "temp lead should have been deleted")

}

func TestDecodeDate(t *testing.T) {
	layout := "2 January, 2006 15:04 MST"
	value := "23 July, 2017 10:30 AEST"
	tt, err := time.Parse(layout, value)
	if err != nil {
		log.Print(err)
		return
	}
	log.Println(tt)

	utc, err := time.LoadLocation("UTC")
	log.Println(tt.In(utc))
}
