package main

import (
	"encoding/json"
	"log"
)

// {
//     "id": "595072499036b8a52",
//     "deleted": false,
//     "post": "another stream",
//     "data": {},
//     "type": "Post",
//     "isInternal": false,
//     "createdAt": "2017-06-26 02:32:41",
//     "modifiedAt": "2017-06-26 02:32:41",
//     "parentId": "595071f8450974b72",
//     "parentType": "Lead",
//     "attachmentsIds": [],
//     "createdById": "1"
// }

//    {
//         "id": "595072499036b8a52",
//         "deleted": false,
//         "post": "another stream",
//         "data": {},
//         "type": "Post",
//         "targetType": null,
//         "number": 34,
//         "isGlobal": false,
//         "createdByGender": "",
//         "isInternal": false,
//         "createdAt": "2017-06-26 02:32:41",
//         "modifiedAt": "2017-06-26 02:50:58",
//         "parentId": "595071f8450974b72",
//         "parentType": "Lead",
//         "parentName": "  \u5218\u5efa\u660e",
//         "relatedId": null,
//         "relatedType": null,
//         "attachmentsIds": [
//             "5950769075cd82e67"
//         ],
//         "attachmentsNames": {
//             "5950769075cd82e67": "flag_gb.png"
//         },
//         "createdById": "1",
//         "createdByName": "Admin",
//         "modifiedById": "1",
//         "modifiedByName": "Admin",
//         "superParentId": null,
//         "superParentType": null,
//         "attachmentsTypes": {
//             "5950769075cd82e67": "image\/png"
//         }
//     }

type crmdStream struct {
	ID      string `json:"id"`
	Deleted bool   `json:"deleted"`
	Post    string `json:"post"`
	//Data passed
	Type string `json:"type"`
	//targetType unknown, why null
	Number           int               `json:"number,omitempty"`
	IsGlobal         bool              `json:"isGlobal,omitempty"`
	CreatedByGender  string            `json:"createdByGender,omitempty"`
	IsInternal       bool              `json:"isInternal"`
	CreateAt         string            `json:"createdAt"`
	ModifiedAt       string            `json:"modifiedAt"`
	ParentID         string            `json:"parentId"`
	ParentType       string            `json:"parentType"`
	ParentName       string            `json:"parentName,omitempty"`
	RelatedID        string            `json:"relatedId,omitempty"`
	RelatedType      string            `json:"relatedType,omitempty"`
	AttachmentsIDs   []string          `json:"attachmentsIds"`
	AttachmentsNames map[string]string `json:"attachmentsNames,omitempty"`
	AttachmentsTypes map[string]string `json:"attachmentsTypes,omitempty"`
	CreatedByID      string            `json:"createdById"`
	CreatedByName    string            `json:"createdByName,omitempty"`
	ModifiedByID     string            `json:"modifiedById,omitempty"`
	ModifiedByName   string            `json:"modifiedByName,omitempty"`
	SuperParentID    string            `json:"superParentId,omitempty"`
	SuperParentType  string            `json:"superParentType,omitempty"`
}

//add note
func add2Stream(parentID, parentType, content string, attachmentsIDs []string) (resp string, err error) {
	s := crmdStream{}
	s.AttachmentsIDs = attachmentsIDs
	s.IsInternal = false
	s.ParentID = parentID
	s.ParentType = parentType
	s.Post = content
	s.Type = "Post"
	url := "https://c.hitxy.org.au/api/v1/Note"

	headers := crmPrepareLeadUploadHTTPHeader()

	b, err := json.Marshal(s)
	if err != nil {
		log.Fatal(err)
		return
	}
	resp, err = postRAW(b, url, headers)
	return
}
