package utils

import (
	"encoding/json"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/http"
	"gopkg.in/gomail.v2"
	"mime"
	"strings"
)

var (
	AlarmMsgUrl = "http://127.0.0.1:8606/api/alarm/send"
)

// 发送邮件
func SendEmail(title, content string, touser string) bool {
	if RunMode == "debug" {
		return false
	}
	var arr []string
	sub := strings.Index(touser, ";")
	if sub >= 0 {
		spArr := strings.Split(touser, ";")
		for _, v := range spArr {
			arr = append(arr, v)
		}
	} else {
		arr = append(arr, touser)
	}
	m := gomail.NewMessage()
	m.SetHeader("From", "317699326@qq.com ")
	m.SetHeader("To", arr...)
	m.SetHeader("Subject", title+" "+GetRandString(16))
	m.SetBody("text/html", content)
	d := gomail.NewDialer("smtp.qq.com", 587, "317699326@qq.com", "oqdypwfcvruwcbea")
	if err := d.DialAndSend(m); err != nil {
		return false
	}
	return true
}

// projectName-项目名称
// runMode-运行模式
// msgBody-消息内容
// level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
func SendAlarmMsg(msgBody string, level int) {
	params := make(map[string]interface{})
	params["ProjectName"] = APPNAME
	params["RunMode"] = RunMode
	params["MsgBody"] = msgBody
	params["Level"] = level
	param, err := json.Marshal(params)
	if err != nil {
		FileLog.Info("SendAlarmMsg json.Marshal Err:" + err.Error())
		return
	}
	http.Post(AlarmMsgUrl, string(param))
}

// 发送邮件
func SendEmailByHz(title, content string, touser string) (result bool, err error) {
	//if RunMode == "debug" {
	//	result = false
	//	return result, err
	//}
	var arr []string
	sub := strings.Index(touser, ";")
	if sub >= 0 {
		spArr := strings.Split(touser, ";")
		for _, v := range spArr {
			arr = append(arr, v)
		}
	} else {
		arr = append(arr, touser)
	}
	m := gomail.NewMessage()
	m.SetHeader("From", "hzcygx@hzinsights.com")
	m.SetHeader("To", arr...)
	m.SetHeader("Subject", title)
	m.SetBody("text/html", content)
	d := gomail.NewDialer("smtp.mxhichina.com", 465, "hzcygx@hzinsights.com", "Hzinsights2024")
	if err := d.DialAndSend(m); err != nil {
		result = false
		return result, err
	}
	result = true
	return
}

// 发送带有文件的邮件
func SendEmailHaveFile(title, content string, fileName, touser string) bool {
	var arr []string
	sub := strings.Index(touser, ";")
	if sub >= 0 {
		spArr := strings.Split(touser, ";")
		for _, v := range spArr {
			arr = append(arr, v)
		}
	} else {
		arr = append(arr, touser)
	}
	m := gomail.NewMessage()
	m.SetHeader("From", "317699326@qq.com ")
	m.SetHeader("To", arr...)
	m.SetHeader("Subject", title)
	m.Attach(fileName)
	m.SetBody("text/html", content)
	d := gomail.NewDialer("smtp.qq.com", 587, "317699326@qq.com", "oqdypwfcvruwcbea")
	if err := d.DialAndSend(m); err != nil {
		return false
	}
	return true
}

// 发送邮件
func SendEmailByHongze(title, content string, touser, attachPath, attachName string) bool {
	var arr []string
	sub := strings.Index(touser, ";")
	if sub >= 0 {
		spArr := strings.Split(touser, ";")
		for _, v := range spArr {
			arr = append(arr, v)
		}
	} else {
		arr = append(arr, touser)
	}
	m := gomail.NewMessage()

	m.SetHeader("From", "hzcygx@hzinsights.com")
	m.SetHeader("To", arr...)
	m.SetHeader("Subject", title)
	m.SetBody("text/html", content)

	//body := new(bytes.Buffer)
	if attachPath != "" {
		m.Attach(attachPath,
			gomail.Rename(attachName),
			gomail.SetHeader(map[string][]string{
				"Content-Disposition": []string{
					fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", attachName)),
				},
			}))
	}
	d := gomail.NewDialer("smtp.mxhichina.com", 465, "hzcygx@hzinsights.com", "Hzinsights2024")
	if err := d.DialAndSend(m); err != nil {
		fmt.Println("send err:", err.Error())
		return false
	}
	return true
}
