package services

import (
	"crypto/tls"
	"eta/eta_api/models"
	"eta/eta_api/utils"
	"fmt"
	"gopkg.in/gomail.v2"
	"strconv"
	"strings"
)

func SendEmailToCompany() {
	// 获取收件人列表
	//emailCond := " AND enabled = 1 "
	emailCond := ""
	emailPars := make([]interface{}, 0)
	emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
	if e != nil {
		fmt.Println("获取收件人列表失败, Err: " + e.Error())
		return
	}
	if len(emails) == 0 {
		fmt.Println("收件人列表为空")
		return
	}

	// TODO:这是HTML模板内容
	template := `<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,viewport-fit=cover">
    <title>Horizon Insights x Fastmarkets x FGE: 2024 Macro & Commodities Outlook - Registration Open!</title>
</head>

<body style="padding:0;margin:0;background-color:#fff">
<div id="app" style="max-width:1280px;margin:0 auto;font-size:14px;min-height:100vh;">
    <div class="main-box" style="padding:25px 20px;">
        <div style="line-height:1.7;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;"
            class=" __aliyun_node_has_color">
            <div style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:center;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;"
                class=" __aliyun_node_has_color"><span
                    style="font-size:18.0px;font-weight:bold;font-style:normal;text-transform:none;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;color:#000000;text-decoration:underline;"
                    class=" __aliyun_node_has_color">2024 Macro &amp; Commodities Outlook Invite</span></div>
            <div style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;"
                class=" __aliyun_node_has_color"><span
                    style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;color:#000000;"
                    class=" __aliyun_node_has_color"><br></span></div>
            <div style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;"
                class=" __aliyun_node_has_color"><span
                    style="font-style:normal;font-weight:400;text-transform:none;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;color:#000000;"
                    class=" __aliyun_node_has_color">Dear Client,</span></div>
            <div style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;"
                class=" __aliyun_node_has_color"><span
                    style="font-style:normal;font-weight:400;text-transform:none;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;color:#000000;"
                    class=" __aliyun_node_has_color"><br></span></div>
            <div style="clear:both;">
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"></div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"></div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"><span
                        style="font-style:normal;font-weight:400;text-transform:none;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;color:#000000;"
                        class=" __aliyun_node_has_color">Happy Year-End Holidays!</span></div>
            </div>
            <div style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;"
                class=" __aliyun_node_has_color"><span
                    style="font-style:normal;font-weight:400;text-transform:none;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;color:#000000;"
                    class=" __aliyun_node_has_color"><br></span></div>
            <div style="clear:both;">
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"></div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"></div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"><span
                        style="color:#000000;font-style:normal;font-weight:400;text-transform:none;border:.0px;font-family:arial;margin:.0px;padding:.0px;outline:.0px;font-size:13.0px;"
                        class=" __aliyun_node_has_color">As we usher in the new year, Horizon Insights, in collaboration
                        with Fastmarkets and FGE, is proud to announce</span><span
                        style="color:#000000;font-style:normal;font-weight:400;text-transform:none;font-family:arial;font-size:13.0px;"
                        class=" __aliyun_node_has_color">&nbsp;our 2024 Macro &amp; Commodities Outlook which will be
                        held&nbsp;</span><span
                        style="font-weight:bold;color:#000000;font-style:normal;text-transform:none;font-family:arial;font-size:13.0px;text-decoration:none;"
                        class=" __aliyun_node_has_color">virtually</span><span
                        style="color:#000000;font-style:normal;font-weight:400;text-transform:none;font-family:arial;font-size:13.0px;text-decoration:none;"
                        class=" __aliyun_node_has_color">&nbsp;on&nbsp;</span><span
                        style="font-weight:bold;color:#000000;font-style:normal;text-transform:none;font-family:arial;font-size:13.0px;text-decoration:none;"
                        class=" __aliyun_node_has_color">10 &amp; 11 Jan 2024</span><span
                        style="color:#000000;font-style:normal;font-weight:400;text-transform:none;font-family:arial;font-size:13.0px;"
                        class=" __aliyun_node_has_color">,&nbsp;</span><span
                        style="color:#000000;font-family:arial;font-size:13.0px;font-style:normal;font-weight:bold;text-transform:none;"
                        class=" __aliyun_node_has_color">5pm - 8pm SGT</span><span
                        style="color:#000000;font-style:normal;font-weight:400;text-transform:none;font-family:arial;font-size:13.0px;"
                        class=" __aliyun_node_has_color">&nbsp;(i.e.&nbsp;</span><span
                        style="color:#000000;font-family:arial;font-size:13.0px;font-style:normal;font-weight:bold;text-transform:none;"
                        class=" __aliyun_node_has_color">9am - 12pm BST</span><span
                        style="color:#000000;font-style:normal;font-weight:400;text-transform:none;font-family:arial;font-size:13.0px;"
                        class=" __aliyun_node_has_color">). Our experienced analysts from Horizon Insights together with
                        guest speakers from our valued partners will present our views in the fields of Macro, Ferrous
                        Metals, Base Metals, Energy, as well as Petrochemicals.</span></div>
            </div>
            <div style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;"
                class=" __aliyun_node_has_color"><span
                    style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-size:13.0px;font-family:arial;"><br></span>
            </div>
            <div style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;"
                class=" __aliyun_node_has_color"><span
                    style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-size:13.0px;font-family:arial;">Please
                    see below for our event schedule:</span></div>
            <div
                style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;">
                <br></div>
            <div
                style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;">
                <span
                    style="color:#000000;font-style:normal;font-weight:400;text-transform:none;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-size:13.0px;font-family:arial;"
                    class=" __aliyun_node_has_color">&nbsp;&nbsp;<img style="vertical-align: bottom; margin: 0px;"
                        height="3301"
                        src="https://hzstatic.hzinsights.com/static/images/202312/20231228/KeDTAxIZ3UrZR5DZKKIQLdoIaOVB.png"
                        width="890"></span></div>
            <div
                style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;">
                <span
                    style="color:#000000;font-style:normal;font-weight:400;text-transform:none;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-size:13.0px;font-family:arial;"
                    class=" __aliyun_node_has_color">&nbsp; &nbsp;</span></div>
            <div
                style="clear:both;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-variant-ligatures:normal;font-variant-caps:normal;text-align:start;text-indent:.0px;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;">
                <br></div>
            <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                class=" __aliyun_node_has_color"><span
                    style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;"><br></span>
            </div>
            <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                class=" __aliyun_node_has_color"><span
                    style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;">To
                    register, simply scan the QR code in the posters above to indicate your interest. Alternatively, you
                    may also&nbsp;</span><span
                    style="font-weight:bold;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;"><a
                        href="https://forms.gle/yhPRDcri43P2QyPt8" target="_blank">register here</a></span><span
                    style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;">.</span>
            </div>
            <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                class=" __aliyun_node_has_color"><span
                    style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;color:#000000;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline;"
                    class=" __aliyun_node_has_color">Should you encounter any difficulties registering, please contact
                    Stephanie (stephanie@hzinsights.com).</span></div>
            <div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"></div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"><span
                        style="font-weight:bold;margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;">The
                        virtual meeting details will be disseminated to you a few days prior to the event.</span></div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"><br></div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"><span
                        style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;">Lastly,
                        our firm would like to wish all of you a prosperous Happy New Year! We look forward to seeing
                        everyone on the 10th &amp; 11th.</span></div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"><span
                        style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;"><br></span>
                </div>
                <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;color:#000000;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;text-align:start;text-indent:.0px;text-transform:none;text-decoration-thickness:initial;text-decoration-style:initial;text-decoration-color:initial;clear:both;"
                    class=" __aliyun_node_has_color"><span
                        style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:arial;font-size:13.0px;">Cheers!</span>
                </div>
            </div>
            <div><br></div>
            <div>
                <div style="clear:both;"><span style="font-family:arial;font-size:13.0px;color:#000000;"
                        class=" __aliyun_node_has_color"><br></span></div>
                <div style="clear:both;">
                    <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;clear:both;font-variant-caps:normal;text-align:start;text-indent:.0px;"
                        class="x___aliyun_node_has_color"><br></div>
                    <div style="margin:.0px;padding:.0px;border:.0px;outline:.0px;clear:both;font-variant-caps:normal;text-align:start;text-indent:.0px;"
                        class="x___aliyun_node_has_color"></div>
                </div>
            </div>
            <div><br></div>
            <div><br></div>
            <div><br></div>
            <div style="line-height:20.0px;clear:both;"><br></div>
        </div>
    </div>
</div>
</body>

</html>`

	// 推送信息
	sendData := make([]*EnglishReportSendEmailRequest, 0)
	for i := range emails {
		r := new(EnglishReportSendEmailRequest)
		r.EmailId = emails[i].Id
		r.Email = strings.Replace(emails[i].Email, " ", "", -1)
		r.Subject = "Horizon Insights x Fastmarkets x FGE: 2024 Macro & Commodities Outlook - Registration Open!" // TODO:这是主题
		r.FromAlias = "Horizon FICC"                                                                              // TODO:这是推送人(中文)

		r.HtmlBody = template
		sendData = append(sendData, r)
	}
	if len(sendData) == 0 {
		fmt.Println("无邮件可推送")
		return
	}

	// 请求阿里云接口批量推送
	aliEmail := new(AliyunEmail)
	resultList, e := aliEmail.BatchSendEmail(sendData)
	if e != nil {
		fmt.Println("批量推送失败, Err: " + e.Error())
		return
	}
	for _, r := range resultList {
		utils.FileLog.Info("email: %s, ok: %v, res: %s", r.Email, r.Ok, r.ResultData)
		if r.Ok {
			fmt.Println("发送成功")
		} else {
			fmt.Println("发送失败:" + r.ResultData)
		}
	}
}

type SendEmailReq struct {
	Title   string   `description:"标题"`
	Content string   `description:"内容"`
	ToUser  []string `description:"收信人邮箱"`
}

func SendEmail(req SendEmailReq) (success bool, err error) {
	if req.Title == "" {
		err = fmt.Errorf("邮件主题不可为空")
		return
	}
	if req.Content == "" {
		err = fmt.Errorf("邮件内容不可为空")
		return
	}
	if len(req.ToUser) <= 0 {
		err = fmt.Errorf("收信人不可为空")
		return
	}

	// 邮箱配置
	confMap, e := models.GetBusinessConf()
	if e != nil {
		err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
		return
	}
	checkArr := []string{
		models.BusinessConfEmailServerHost, models.BusinessConfEmailServerPort,
		models.BusinessConfEmailSender, models.BusinessConfEmailSenderUserName,
		models.BusinessConfEmailSenderPassword,
	}
	for _, v := range checkArr {
		if confMap[v] == "" {
			err = fmt.Errorf("%s配置有误", v)
			return
		}
	}

	port, _ := strconv.Atoi(confMap[models.BusinessConfEmailServerPort])
	if port <= 0 {
		port = 587 // 默认587端口
	}
	m := gomail.NewMessage()
	m.SetHeader("From", confMap[models.BusinessConfEmailSender])
	m.SetHeader("To", req.ToUser...)
	m.SetHeader("Subject", req.Title)
	m.SetBody("text/html", req.Content)
	d := gomail.NewDialer(confMap[models.BusinessConfEmailServerHost], port, confMap[models.BusinessConfEmailSenderUserName], confMap[models.BusinessConfEmailSenderPassword])
	// 解决x509报错的问题。证书不通过。跳过证书验证
	config := &tls.Config{ServerName: confMap[models.BusinessConfEmailServerHost], InsecureSkipVerify: true}
	d.TLSConfig = config
	if e = d.DialAndSend(m); e != nil {
		err = fmt.Errorf("邮件发送失败, Err: %s", e.Error())
		return
	}
	success = true
	return
}