package main

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

func TestConvertLead2Contact(t *testing.T) {
	//create a lead
	l := crmdLead{}
	l.FirstName = "testConvert" + time.Now().Format(getCrmTimeLayout())
	l.LastName = "temp lead (deletable)"
	l.Status = "Deletable"
	l.Password = "password"
	l.EmailAddress = "non-exit-fake@badmail.com"
	l.PhoneNumber = "12345678-1"
	l.ForceDuplicate = true
	jsonB, err := json.Marshal(l)
	AssertEqual(t, err, nil, "")
	entity, err := crmCreateEntity("Lead", jsonB)
	AssertEqual(t, err, nil, "")
	lead := entity.(crmdLead)
	AssertEqual(t, lead.ID != "", true, "")

	//conver to contact
	cc := crmdContact{}
	err = cc.convertFromLead(lead.ID)
	AssertEqual(t, err, nil, "")
	AssertEqual(t, cc.OriginalLeadID, lead.ID, "")
	AssertEqual(t, cc.FirstName, lead.FirstName, "")
	AssertEqual(t, cc.LastName, lead.LastName, "")
	AssertEqual(t, cc.EmailAddress, lead.EmailAddress, "")
	AssertEqual(t, cc.PhoneNumber, lead.PhoneNumber, "")

	//delete lead
	crmDeleteEntity("Lead", lead.ID)

	//delete contact
	crmDeleteEntity("Contact", cc.ID)
}

func TestDecodeContact(t *testing.T) {
	msg := `{
    "id": "57efb926aa6725fdd",
    "name": "Vincent Zhang",
    "deleted": false,
    "salutationName": "Mr.",
    "firstName": "Vincent",
    "lastName": "Zhang",
    "accountId": "57e3de733402c8d5f",
    "title": "IT Admin",
    "description": "somedescription",
    "emailAddress": "testemail@gmail.com",
    "phoneNumber": "12315",
    "doNotCall": false,
    "addressStreet": "street",
    "addressCity": "city",
    "addressState": "state",
    "addressCountry": "au",
    "addressPostalCode": "123",
    "accountType": "Customer",
    "createdAt": "2016-10-01 13:24:54",
    "modifiedAt": "2016-10-10 06:18:54",
    "emailAddressData": [],
    "phoneNumberData": [],
    "accountName": "Macquarie Mandarin",
    "accountsIds": [
        "57e3de733402c8d5f"
    ],
    "accountsNames": {
        "57e3de733402c8d5f": "Macquarie Mandarin"
    },
    "accountsColumns": {
        "57e3de733402c8d5f": {
            "role": "IT Admin"
        }
    },
    "campaignId": "cid",
    "campaignName": "campaignname",
    "createdById": "1",
    "createdByName": "spp",
    "modifiedById": "1",
    "modifiedByName": "spp",
    "assignedUserId": "1",
    "assignedUserName": "spp",
    "teamsIds": [],
    "teamsNames": {},
    "portalUserId": "pi",
    "portalUserName": "pu",
    "originalLeadId": "oid",
    "originalLeadName": "oname",
    "isFollowed": true,
    "followersIds": [
        "1"
    ],
    "followersNames": {
        "1": "spp"
    },
    "avatarId": "57fb32cd3de8755df",
    "avatarName": "06_vincen.jpg",
    "namecardId": null,
    "namecardName": null,
    "namecardbackId": null,
    "namecardbackName": null
	}`

	cc := crmdContact{}
	err := json.Unmarshal([]byte(msg), &cc)
	//log.Println(err)
	AssertEqual(t, err, nil, "decode contact should be correct")
	AssertEqual(t, cc.ID, "57efb926aa6725fdd", "contact ID mismatch")
	AssertEqual(t, cc.Name, "Vincent Zhang", "contact name mismatch")
	AssertEqual(t, cc.Deleted, false, "deleted should be false")
	AssertEqual(t, cc.SalutationName, "Mr.", "solutation should be Mr.")
	AssertEqual(t, cc.FirstName, "Vincent", "first name should be Vincent")
	AssertEqual(t, cc.LastName, "Zhang", "last name should be zhang")
	AssertEqual(t, cc.AccountID, "57e3de733402c8d5f", "accound id should be 57e3de733402c8d5f")
	AssertEqual(t, cc.Title, "IT Admin", "title should be IT Admin")
	AssertEqual(t, cc.Description, "somedescription", "description not right")
	AssertEqual(t, cc.EmailAddress, "testemail@gmail.com", "email address not right")
	AssertEqual(t, cc.PhoneNumber, "12315", "")
	AssertEqual(t, cc.DoNotCall, false, "do not call should be false ")
	AssertEqual(t, cc.AddressStreet, "street", "street not right")
	AssertEqual(t, cc.AddressCity, "city", "city not right")
	AssertEqual(t, cc.AddressState, "state", "state not right")
	AssertEqual(t, cc.AddressCountry, "au", "country mismatch")
	AssertEqual(t, cc.AddresPostalCode, "123", "post code not right")
	AssertEqual(t, cc.AccountType, "Customer", "account type shold be customer")
	AssertEqual(t, cc.CreatedAt, "2016-10-01 13:24:54", "")
	AssertEqual(t, cc.ModifiedAt, "2016-10-10 06:18:54", "")
	AssertEqual(t, cc.AccountName, "Macquarie Mandarin", "")
	AssertEqual(t, cc.AccountIDs[0], "57e3de733402c8d5f", "first account id mismatch")
	AssertEqual(t, cc.AccountNames["57e3de733402c8d5f"], "Macquarie Mandarin", "")
	AssertEqual(t, cc.AccountsColumns["57e3de733402c8d5f"].Role, "IT Admin", "")
	AssertEqual(t, cc.CampaignName, "campaignname", "")
	AssertEqual(t, cc.CampaignID, "cid", "campaignID mismatch")
	AssertEqual(t, cc.CreatedByID, "1", "creatd by should be 1")
	AssertEqual(t, cc.CreatedByName, "spp", "created by name not match")
	AssertEqual(t, cc.ModifiedByID, "1", "modified by should be 1")
	AssertEqual(t, cc.ModifiedByName, "spp", "modified by name not match")
	AssertEqual(t, cc.AssignedUserID, "1", "assigned user id should be 1")
	AssertEqual(t, cc.AssignedUserName, "spp", "assigned user name not match")
	AssertEqual(t, len(cc.TeamsIDs), 0, "")
	AssertEqual(t, len(cc.TeamsNames), 0, "")
	AssertEqual(t, cc.PortalUserID, "pi", "")
	AssertEqual(t, cc.PortalUserName, "pu", "")
	AssertEqual(t, cc.OriginalLeadID, "oid", "")
	AssertEqual(t, cc.OriginalLeadName, "oname", "")
	AssertEqual(t, cc.IsFollowed, true, "is followed should be true")
	AssertEqual(t, cc.FollwersIDs[0], "1", "follower id should be 1")
	AssertEqual(t, cc.FollowersNames["1"], "spp", "follower name should be sp")

}
