package main

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

func TestDecodeLeadInfo(t *testing.T) {
	msg := `{
            "id": "58f4aa0682ea74bb8",
            "name": "same email",
            "deleted": false,
            "salutationName": "Mr",
            "firstName": "same",
            "lastName": "email",
            "title": "title",
            "status": "New",
            "source": "",
            "industry": "",
            "opportunityAmount": null,
            "website": "",
            "addressStreet": "",
            "addressCity": "",
            "addressState": "",
            "addressCountry": "",
            "addressPostalCode": "",
            "emailAddress": "test@gmail.com",
            "phoneNumber": "456",
            "doNotCall": false,
            "description": "",
            "createdAt": "2017-04-17 11:41:58",
            "modifiedAt": "2017-04-18 04:41:06",
            "accountName": "",
            "password": "abcdefg",
            "wechat_hitxy_id": "the user id obtained from wechat public account",
            "verifier": [
                "231823091",
                "adkdaifaskfsafsa",
                "394kfdjafdsa",
                "39410498321041"
            ],
            "opportunityAmountCurrency": null,
            "opportunityAmountConverted": null,
            "createdById": "1",
            "createdByName": "Admin",
            "modifiedById": "58ef420cac3cf6c95",
            "modifiedByName": "wechat robot",
            "assignedUserId": "1",
            "assignedUserName": "Admin",
            "campaignId": null,
            "createdAccountId": null,
            "createdContactId": null,
            "createdOpportunityId": null
        }`

	r := crmdLead{}
	err := json.Unmarshal([]byte(msg), &r)
	//log.Println(r)
	AssertEqual(t, err, nil, "Decode CRM Lead Json shold be correct")

	AssertEqual(t, r.ID, "58f4aa0682ea74bb8", "ID mismatch ")
	AssertEqual(t, r.Name, "same email", "Name mismatch")
	AssertEqual(t, r.Deleted, false, "")
	AssertEqual(t, r.SalutationName, "Mr", "")
	AssertEqual(t, r.FirstName, "same", "")
	AssertEqual(t, r.LastName, "email", "")
	AssertEqual(t, r.Title, "title", "")
	AssertEqual(t, r.Status, "New", "")
	AssertEqual(t, r.Source, "", "")
	AssertEqual(t, r.Industry, "", "")
	AssertEqual(t, r.OpportunityAmount, 0, "")
	AssertEqual(t, r.Website, "", "")
	AssertEqual(t, r.AddressStreet, "", "")
	AssertEqual(t, r.AddressCity, "", "")
	AssertEqual(t, r.AddressState, "", "")
	AssertEqual(t, r.AddressCountry, "", "")
	AssertEqual(t, r.AddressPostalCode, "", "")
	AssertEqual(t, r.EmailAddress, "test@gmail.com", "")
	AssertEqual(t, r.PhoneNumber, "456", "")
	AssertEqual(t, r.DoNotCall, false, "")
	AssertEqual(t, r.Description, "", "")
	AssertEqual(t, r.CreatedAt, "2017-04-17 11:41:58", "")
	AssertEqual(t, r.ModifiedAt, "2017-04-18 04:41:06", "")
	AssertEqual(t, r.AccountName, "", "")
	AssertEqual(t, r.Password, "abcdefg", "")
	AssertEqual(t, r.WechatHitxyID, "the user id obtained from wechat public account", "")
	AssertEqual(t, r.Verifier[0], "231823091", "")
	AssertEqual(t, r.Verifier[1], "adkdaifaskfsafsa", "")
	AssertEqual(t, r.Verifier[2], "394kfdjafdsa", "")
	AssertEqual(t, r.Verifier[3], "39410498321041", "")
	AssertEqual(t, r.OpportunityAmountCurrency, "", "")
	AssertEqual(t, r.OpportunityAmountConverted, 0, "")
	AssertEqual(t, r.CreatedByID, "1", "")
	AssertEqual(t, r.CreatedByName, "Admin", "")
	AssertEqual(t, r.ModifiedByID, "58ef420cac3cf6c95", "")
	AssertEqual(t, r.ModifiedByName, "wechat robot", "")
	AssertEqual(t, r.AssignedUserID, "1", "")
	AssertEqual(t, r.AssignedUserName, "Admin", "")
	AssertEqual(t, r.CampaignID, "", "")
	AssertEqual(t, r.CreatedAccountID, "", "")
	AssertEqual(t, r.CreatedContactID, "", "")
	AssertEqual(t, r.CreatedOpportunityID, "", "")

}

func TestcrmdLeadByCompleteInfo(t *testing.T) {
	msg := `{
	"id": "58f4aa0682ea74bb8",
    "name": "same email",
    "deleted": false,
    "salutationName": "",
    "firstName": "same",
    "lastName": "email",
    "title": "",
    "status": "New",
    "source": "",
    "industry": "",
    "opportunityAmount": 120,
    "website": "",
    "addressStreet": "",
    "addressCity": "",
    "addressState": "",
    "addressCountry": "",
    "addressPostalCode": "",
    "emailAddress": "test@gmail.com",
    "phoneNumber": "456",
    "doNotCall": false,
    "description": "",
    "createdAt": "2017-04-17 11:41:58",
    "modifiedAt": "2017-06-04 04:08:16",
    "accountName": "",
    "password": "abcdefg",
    "wechat_hitxy_id": "the user id obtained from wechat public account",
    "verifier": [
        "231823091",
        "adkdaifaskfsafsa",
        "394kfdjafdsa",
        "39410498321041"
    ],
    "opportunityAmountCurrency": "AUD",
    "opportunityAmountConverted": 120,
    "emailAddressData": [
        {
            "emailAddress": "test@gmail.com",
            "lower": "test@gmail.com",
            "primary": true,
            "optOut": false,
            "invalid": false
        },
        {
            "emailAddress": "abc@hotmail.com",
            "lower": "abc@hotmail.com",
            "primary": false,
            "optOut": false,
            "invalid": false
        },
        {
            "emailAddress": "def@jjx.com.au",
            "lower": "def@jjx.com.au",
            "primary": false,
            "optOut": false,
            "invalid": false
        }
    ],
    "phoneNumberData": [
        {
            "phoneNumber": "456",
            "primary": true,
            "type": "Home"
        },
        {
            "phoneNumber": "123",
            "primary": false,
            "type": "Mobile"
        }
    ],
    "createdById": "1",
    "createdByName": "Admin",
    "modifiedById": "1",
    "modifiedByName": "Admin",
    "assignedUserId": "1",
    "assignedUserName": "Admin",
    "teamsIds": [],
    "teamsNames": {},
    "campaignId": null,
    "campaignName": null,
    "createdAccountId": null,
    "createdAccountName": null,
    "createdContactId": null,
    "createdContactName": null,
    "createdOpportunityId": null,
    "createdOpportunityName": null,
    "imagesIds": [],
    "imagesNames": {},
    "isFollowed": true,
    "followersIds": [
        "1"
    ],
    "followersNames": {
        "1": "Admin"
    },
    "imagesTypes": {}
	}`
	r := crmdLead{}
	err := json.Unmarshal([]byte(msg), &r)
	//log.Println(r)
	AssertEqual(t, err, nil, "")

	AssertEqual(t, r.ID, "58f4aa0682ea74bb8", "ID mismatch ")
	AssertEqual(t, r.Name, "same email", "Name mismatch")
	AssertEqual(t, r.Deleted, false, "")
	AssertEqual(t, r.SalutationName, "", "")
	AssertEqual(t, r.FirstName, "same", "")
	AssertEqual(t, r.LastName, "email", "")
	AssertEqual(t, r.Title, "", "")
	AssertEqual(t, r.Status, "New", "")
	AssertEqual(t, r.Source, "", "")
	AssertEqual(t, r.Industry, "", "")
	AssertEqual(t, r.OpportunityAmount, 120, "")
	AssertEqual(t, r.Website, "", "")
	AssertEqual(t, r.AddressStreet, "", "")
	AssertEqual(t, r.AddressCity, "", "")
	AssertEqual(t, r.AddressState, "", "")
	AssertEqual(t, r.AddressCountry, "", "")
	AssertEqual(t, r.AddressPostalCode, "", "")
	AssertEqual(t, r.EmailAddress, "test@gmail.com", "")
	AssertEqual(t, r.PhoneNumber, "456", "")
	AssertEqual(t, r.DoNotCall, false, "")
	AssertEqual(t, r.Description, "", "")
	AssertEqual(t, r.CreatedAt, "2017-04-17 11:41:58", "")
	AssertEqual(t, r.ModifiedAt, "2017-06-04 04:08:16", "")
	AssertEqual(t, r.AccountName, "", "")
	AssertEqual(t, r.Password, "abcdefg", "")
	AssertEqual(t, r.WechatHitxyID, "the user id obtained from wechat public account", "")
	AssertEqual(t, r.Verifier[0], "231823091", "")
	AssertEqual(t, r.Verifier[1], "adkdaifaskfsafsa", "")
	AssertEqual(t, r.Verifier[2], "394kfdjafdsa", "")
	AssertEqual(t, r.Verifier[3], "39410498321041", "")
	AssertEqual(t, r.OpportunityAmountCurrency, "AUD", "")
	AssertEqual(t, r.OpportunityAmountConverted, 120, "")
	AssertEqual(t, r.CreatedByID, "1", "")
	AssertEqual(t, r.CreatedByName, "Admin", "")
	AssertEqual(t, r.ModifiedByID, "1", "")
	AssertEqual(t, r.ModifiedByName, "Admin", "")
	AssertEqual(t, r.AssignedUserID, "1", "")
	AssertEqual(t, r.AssignedUserName, "Admin", "")
	AssertEqual(t, r.CampaignID, "", "")
	AssertEqual(t, r.CreatedAccountID, "", "")
	AssertEqual(t, r.CreatedContactID, "", "")
	AssertEqual(t, r.CreatedOpportunityID, "", "")

	AssertEqual(t, r.EmailAddressData[0].EmailAddress, "test@gmail.com", "")
	AssertEqual(t, r.EmailAddressData[0].Lower, "test@gmail.com", "")
	AssertEqual(t, r.EmailAddressData[0].Primary, true, "")
	AssertEqual(t, r.EmailAddressData[0].OptOut, false, "")
	AssertEqual(t, r.EmailAddressData[0].Invalid, false, "")

	AssertEqual(t, r.EmailAddressData[1].EmailAddress, "abc@hotmail.com", "")
	AssertEqual(t, r.EmailAddressData[1].Lower, "abc@hotmail.com", "")
	AssertEqual(t, r.EmailAddressData[1].Primary, false, "")
	AssertEqual(t, r.EmailAddressData[1].OptOut, false, "")
	AssertEqual(t, r.EmailAddressData[1].Invalid, false, "")

	AssertEqual(t, r.EmailAddressData[2].EmailAddress, "def@jjx.com.au", "")
	AssertEqual(t, r.EmailAddressData[2].Lower, "def@jjx.com.au", "")
	AssertEqual(t, r.EmailAddressData[2].Primary, false, "")
	AssertEqual(t, r.EmailAddressData[2].OptOut, false, "")
	AssertEqual(t, r.EmailAddressData[2].Invalid, false, "")

	AssertEqual(t, r.PhoneNumberData[0].PhoneNumber, "456", "")
	AssertEqual(t, r.PhoneNumberData[0].Primary, true, "")
	AssertEqual(t, r.PhoneNumberData[0].Type, "Home", "")

	AssertEqual(t, r.PhoneNumberData[1].PhoneNumber, "123", "")
	AssertEqual(t, r.PhoneNumberData[1].Primary, false, "")
	AssertEqual(t, r.PhoneNumberData[1].Type, "Mobile", "")

}

func TestSearchResult(t *testing.T) {
	msg := `{
    "total": 2,
    "list": [
        {
            "id": "58f4aa0682ea74bb8",
            "name": "same email",
            "deleted": false,
            "salutationName": "",
            "firstName": "same",
            "lastName": "email",
            "title": "",
            "status": "New",
            "source": "",
            "industry": "",
            "opportunityAmount": 120,
            "website": "",
            "addressStreet": "",
            "addressCity": "",
            "addressState": "",
            "addressCountry": "",
            "addressPostalCode": "",
            "emailAddress": "test@gmail.com",
            "phoneNumber": "456",
            "doNotCall": true,
            "description": "",
            "createdAt": "2017-04-17 11:41:58",
            "modifiedAt": "2017-06-04 06:57:48",
            "accountName": "",
            "password": "abcdefg",
            "wechat_hitxy_id": "the user id obtained from wechat public account",
            "verifier": [
                "231823091",
                "adkdaifaskfsafsa",
                "394kfdjafdsa",
                "39410498321041"
            ],
            "opportunityAmountCurrency": "AUD",
            "opportunityAmountConverted": 120,
            "createdById": "1",
            "createdByName": "Admin",
            "modifiedById": "1",
            "modifiedByName": "Admin",
            "assignedUserId": "1",
            "assignedUserName": "Admin",
            "campaignId": null,
            "createdAccountId": null,
            "createdContactId": null,
            "createdOpportunityId": null
        },
        {
            "id": "58f4a849502da6f33",
            "name": "not searchable",
            "deleted": false,
            "salutationName": "Mr.",
            "firstName": "not",
            "lastName": "searchable",
            "title": "",
            "status": "New",
            "source": "",
            "industry": "",
            "opportunityAmount": null,
            "website": "",
            "addressStreet": "",
            "addressCity": "",
            "addressState": "",
            "addressCountry": "",
            "addressPostalCode": "",
            "emailAddress": null,
            "phoneNumber": null,
            "doNotCall": false,
            "description": "",
            "createdAt": "2017-04-17 11:34:33",
            "modifiedAt": "2017-04-18 05:09:36",
            "accountName": "",
            "password": null,
            "wechat_hitxy_id": "the user id obtained from wechat public account",
            "verifier": [],
            "opportunityAmountCurrency": null,
            "opportunityAmountConverted": null,
            "createdById": "1",
            "createdByName": "Admin",
            "modifiedById": "58ef420cac3cf6c95",
            "modifiedByName": "wechat robot",
            "assignedUserId": "1",
            "assignedUserName": "Admin",
            "campaignId": null,
            "createdAccountId": null,
            "createdContactId": null,
            "createdOpportunityId": null
        }
    ]
}`
	r := crmSearchLead{}
	err := json.Unmarshal([]byte(msg), &r)
	AssertEqual(t, err, nil, "")
	AssertEqual(t, r.Total, 2, "")
	AssertEqual(t, len(r.List), 2, "")
	AssertEqual(t, r.List[0].ID, "58f4aa0682ea74bb8", "")
	AssertEqual(t, r.List[1].ID, "58f4a849502da6f33", "")
	AssertEqual(t, r.List[1].AssignedUserName, "Admin", "")
}

func TestSearchResultZero(t *testing.T) {
	msg := `{"total":0,"list":[]}`
	r := crmSearchLead{}
	err := json.Unmarshal([]byte(msg), &r)
	AssertEqual(t, err, nil, "")
	AssertEqual(t, r.Total, 0, "")
	AssertEqual(t, len(r.List), 0, "")
}

func TestFindOpenIDOnline(t *testing.T) {
	SetupConfig()
	info, found, err := crmFindOpenID("weid1")
	log.Println(info)
	log.Println(found)
	log.Println(err)
}

func TestLeadJsonCompose(t *testing.T) {
	info := crmdLead{}
	info.FirstName = "中文First"

}

func TestGetLead(t *testing.T) {
	SetupConfig()
	id := "595071f8450974b72"
	r, err := crmGetLead(id)
	AssertEqual(t, err, nil, "decode json should be nil")
	AssertEqual(t, r.ID, id, "Lead id should match")
}
