package main

import "log"

type getBasicUserInfoDef struct {
	chatProcedureID
}

var procGetBasicUserInfo = getBasicUserInfoDef{}

func (m *getBasicUserInfoDef) init(ss *openIDSessionData) {
	ss.Procedure = m.getID()
	ss.refreshExpire(600)
}

func (m *getBasicUserInfoDef) clean(ss *openIDSessionData) {
	ss.Procedure = ""
}
func (m *getBasicUserInfoDef) start(ss *openIDSessionData, in *InWechatMsg) {
	procGetBasicUserInfo.summary(ss)
	procGetBasicUserInfo.clean(ss)
}

func (m *getBasicUserInfoDef) serve(ss *openIDSessionData, in *InWechatMsg) {

}
func (m *getBasicUserInfoDef) summary(ss *openIDSessionData) {
	//build article message with user's picture and email,phone
	articles := []sendNewsArticle{}
	lead, found, err := crmFindLeadByOpenID(ss.OpenID)
	if err != nil {
		log.Println(err)
		kfSendTxt(ss.OpenID, "系统正在努力维护中，请稍候，请联络人工查询服务")
	}

	if !found {
		lead, err = crmCreateLeadByOpenID(ss.OpenID)
		if err == nil {
			kfSendTxt(ss.OpenID, "您在系统中并没有记录, 为您创建新纪录")
		} else {
			kfSendTxt(ss.OpenID, "系统正在努力维护中，请稍候，请联络人工查询服务")
			log.Printf("ERROR creating Lead for OpenID %s\n %s", ss.OpenID, err)
			return
		}
	}
	//main photo
	articles = append(articles, lead.getBasicUserInfoPhoto())
	//email
	articles = append(articles, lead.getBasicUserInfoEmail())
	//phone
	articles = append(articles, lead.getBasicUserInfoPhone())
	//edit
	articles = append(articles, lead.getBasicUserInfoEditButton())

	kfSendNews(ss.OpenID, "总标题", "总描述", articles)
	//kfSendTemplateMsg(ss.OpenID)
}

func (m *getBasicUserInfoDef) intro(ss *openIDSessionData, in *InWechatMsg) {
	in.replyText("")
	//m.summary(ss)
}

func (m crmdLead) getBasicUserInfoPhoto() (ret sendNewsArticle) {
	a := sendNewsArticle{}
	a.Description = ""
	a.PicURL = m.avatarCacheURL()
	a.Title = m.Name
	a.URL = m.avatarCacheURL()
	log.Println(a.URL)
	return a
}

func (m crmdLead) getBasicUserInfoEmail() (ret sendNewsArticle) {
	a := sendNewsArticle{}
	a.Description = ""
	a.PicURL = ""
	if m.EmailAddress == "" {
		a.Title = "电邮: 妹有，妹有啊"
	} else {
		a.Title = "电邮: " + m.EmailAddress
	}
	a.URL = ""
	return a
}

func (m crmdLead) getBasicUserInfoPhone() (ret sendNewsArticle) {
	a := sendNewsArticle{}
	a.Description = ""
	a.PicURL = ""
	if m.PhoneNumber == "" {
		a.Title = "电话: zhei4 个真妹有啊"
	} else {
		a.Title = "电话: " + m.PhoneNumber
	}
	a.URL = ""
	return a
}

func (m crmdLead) getBasicUserInfoEditButton() (ret sendNewsArticle) {
	a := sendNewsArticle{}
	a.Description = ""
	a.PicURL = ""
	a.Title = "点击这里编辑您的资料"
	a.URL = "https://google.com.au"
	return a
}
