package http

import (
	"FollowUp_Notice/conf"
	"FollowUp_Notice/lib"
	"FollowUp_Notice/logs"
	"encoding/json"
	"github.com/go-resty/resty/v2"
)

type Template struct {
	Template_id                 string `json:"template_id"`
	Sms_title                   string `json:"sms_title"`
	Sms_signature               string `json:"sms_signature"`
	Sms_content                 string `json:"sms_content"`
	Add_date                    string `json:"add_date"`
	Edit_date                   string `json:"edit_date"`
	Template_status             string `json:"template_status"`
	Template_status_description string `json:"template_status_description"`
}

type TemplateRes struct {
	Status      string   `json:"status"`
	Template_id string   `json:"template_id"`
	Template    Template `json:"template"`
}
type XSendRes struct {
	Status  string `json:"status"`
	Send_id string `json:"send_id"`
	Fee     int    `json:"fee"`
}

func SmsTemplate_Get(template_id string) (TemplateRes, error) {
	// Create a Resty Client
	client := resty.New()

	resp, err := client.R().
		SetQueryParams(map[string]string{
			"appid":       conf.Sms_Appid,
			"signature":   conf.Sms_Signature,
			"template_id": template_id,
		}).
		Get("https://api-v4.mysubmail.com/sms/template.json")

	if err != nil {
		return TemplateRes{}, err
	}

	temp := TemplateRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return TemplateRes{}, err
	}
	return temp, nil
}

// 创建模板
func SmsTemplate_Post(sms_signature string) (TemplateRes, error) {
	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(map[string]string{
			"appid":         conf.Sms_Appid,
			"signature":     conf.Sms_Signature,
			"sms_title":     "复诊通知",
			"sms_signature": sms_signature,
			"sms_content":   "敬爱的@var(name)，离上一次来我院治疗的复诊时间已到，为了更好关注您的病情，请于@var(time)到我院进行复诊，祝您早日康复！",
		}).
		Post("http://api.mysubmail.com/sms/template.json")

	if err != nil {
		return TemplateRes{}, err
	}

	temp := TemplateRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return TemplateRes{}, err
	}
	return temp, nil
}

// 修改模板
func SmsTemplate_Put(template_id, sms_signature string) (TemplateRes, error) {
	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(map[string]string{
			"appid":         conf.Sms_Appid,
			"signature":     conf.Sms_Signature,
			"template_id":   template_id,
			"sms_title":     "复诊通知",
			"sms_signature": sms_signature,
			"sms_content":   "敬爱的@var(code)，离上一次来我院治疗的复诊时间已到，为了更好关注您的病情，请于明日到我院进行复诊，祝您早日康复！",
		}).
		SetResult(&TemplateRes{}).
		Put("http://api.mysubmail.com/sms/template.json")

	if err != nil {
		return TemplateRes{}, err
	}

	temp := TemplateRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return TemplateRes{}, err
	}
	return temp, nil
}

func SmsTemplate_Delete(template_id string) (TemplateRes, error) {
	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(map[string]string{
			"appid":       conf.Sms_Appid,
			"signature":   conf.Sms_Signature,
			"template_id": template_id,
		}).
		SetResult(&TemplateRes{}).
		Delete("http://api.mysubmail.com/sms/template.json")

	if err != nil {
		return TemplateRes{}, err
	}

	temp := TemplateRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return TemplateRes{}, err
	}
	return temp, nil
}

// 批量发送短信
func SmsMultiSend(template_id, content string) (TemplateRes, error) {
	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(map[string]string{
			"appid":     conf.Sms_Appid,
			"signature": conf.Sms_Signature,
			"content":   content,
		}).
		SetResult(&TemplateRes{}).
		Delete("https://api-v4.mysubmail.com/sms/multisend")

	if err != nil {
		return TemplateRes{}, err
	}

	temp := TemplateRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return TemplateRes{}, err
	}
	return temp, nil
}

// 短信模板发送
func SmsXSend(template_id, to, name, time string) (XSendRes, error) {

	type Vars struct {
		Name string `json:"name"`
		Time string `json:"time"`
	}

	vars := Vars{Name: name, Time: time}

	b, _ := json.Marshal(vars)

	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(map[string]string{
			"appid":     conf.Sms_Appid,
			"signature": conf.Sms_Signature,
			"to":        to,
			"project":   template_id,
			"vars":      string(b),
		}).
		SetResult(&XSendRes{}).
		Post("https://api-v4.mysubmail.com/sms/xsend")

	if err != nil {
		return XSendRes{}, err
	}

	temp := XSendRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return XSendRes{}, err
	}
	return temp, nil
}

func SmsXSendBill(to, name, moneyFlag string) (XSendRes, error) {

	type Vars struct {
		Name string `json:"name"`
		Msg  string `json:"msg"`
	}

	vars := Vars{Name: name, Msg: moneyFlag}

	b, _ := json.Marshal(vars)

	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(map[string]string{
			"appid":     conf.Sms_Appid,
			"signature": conf.Sms_Signature,
			"to":        to,
			"project":   "LPdPa4",
			"vars":      string(b),
		}).
		SetResult(&XSendRes{}).
		Post("https://api-v4.mysubmail.com/sms/xsend")

	if err != nil {
		return XSendRes{}, err
	}

	temp := XSendRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		logs.Error(lib.FuncName(), err)
		return XSendRes{}, err
	}
	return temp, nil
}
