package app

import (
	"bytes"
	"encoding/json"
	"ffie-admin/server/global"
	"ffie-admin/server/utils"
	"ffie-admin/server/utils/errno"
	"io/ioutil"
	"net/http"
	"strconv"
	"time"
)

type Header struct {
	FzmCaTimestamp string `json:"fzm-ca-timestamp"`
	FzmCaAppKey    string `json:"fzm-ca-appkey"`
	FzmCaAppIp     string `json:"fzm-ca-ip"`
	FzmCaOS        string `json:"fzm-ca-os"`
	FzmCaSignature string `json:"fzm-ca-signature"`
}

type MnemonicPhraseEmailReq struct {
	Email   string `json:"email"`
	Content string `json:"content"`
	Title   string `json:"title"`
	IsHtml  string `json:"isHtml"`
}

type ValidateEmailReq struct {
	BusinessId string `json:"businessId"`
	CodeType   string `json:"codetype"`
	Email      string `json:"email"`
	Param      string `json:"param"`
	Ticket     string `json:"ticket"`
}

type ValidateEmailResp struct {
	Message string `json:"message"`
	Error   string `json:"error"`
	Code    string `json:"code"`
}

type ValidateCodeReq struct {
	T        string `json:"t"`
	Gid      string `json:"gid"`
	Code     string `json:"code"`
	Guide    string `json:"guide"`
	Email    string `json:"email"`
	Mobile   string `json:"mobile"`
	Country  string `json:"country"`
	CodeType string `json:"codetype"`
}

type ValidateCodeResp struct {
	Message string `json:"message"`
	Error   string `json:"error"`
	Code    string `json:"code"`
}

const HostValidate = "http://47.100.212.109:13803"
const AppKey = "Bityuan"
const AppSecret = "n3ZpNBD3p5P4&1VH"

func (validateCode ValidateCodeReq) ValidateCode() error {
	params := ValidateCodeReq{
		Code:     validateCode.Code,
		CodeType: validateCode.CodeType,
		Email:    validateCode.Email,
		Guide:    "0",
		T:        "email",
	}
	jsonStr, err := json.Marshal(params)
	req, err := http.NewRequest("POST", HostValidate+"/validate/code", bytes.NewBuffer(jsonStr))
	paramsMap := make(map[string]string)
	j, _ := json.Marshal(params)
	json.Unmarshal(j, &paramsMap)
	timestamp := strconv.FormatInt(time.Now().Unix(), 10)
	sign := utils.Signature(AppKey, paramsMap, AppSecret, timestamp)

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Fzm-Ca-Timestamp", timestamp)
	req.Header.Set("Fzm-Ca-AppKey", AppKey)
	req.Header.Set("Fzm-Ca-Ip", "127.0.0.1")
	req.Header.Set("Fzm-Ca-Signature", sign)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	var validateCodeResp ValidateCodeResp
	err = json.Unmarshal(body, &validateCodeResp)
	if err != nil {
		return err
	}

	if validateCodeResp.Code != "200" {
		return errno.ErrValidateCode
	}

	return nil
}

func (email ValidateEmailReq) SendEmail() error {
	var params ValidateEmailReq
	params.Email = email.Email
	params.CodeType = email.CodeType
	params.Param = "FzmRandom5"
	jsonStr, err := json.Marshal(params)
	req, err := http.NewRequest("POST", HostValidate+"/send/email2", bytes.NewBuffer(jsonStr))
	paramsMap := make(map[string]string)
	j, _ := json.Marshal(params)
	json.Unmarshal(j, &paramsMap)
	timestamp := strconv.FormatInt(time.Now().Unix(), 10)
	sign := utils.Signature(AppKey, paramsMap, AppSecret, timestamp)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Fzm-Ca-Timestamp", timestamp)
	req.Header.Set("Fzm-Ca-AppKey", AppKey)
	req.Header.Set("Fzm-Ca-Ip", "127.0.0.1")
	req.Header.Set("Fzm-Ca-Signature", sign)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	var validateEmailResp ValidateEmailResp
	err = json.Unmarshal(body, &validateEmailResp)

	if err != nil {
		global.GVA_LOG.Info("发送邮箱返回-1")
		global.GVA_LOG.Info(validateEmailResp.Code)
		global.GVA_LOG.Info(validateEmailResp.Error)
		global.GVA_LOG.Info(validateEmailResp.Message)
		global.GVA_LOG.Error(err.Error())
		return err
	}

	if validateEmailResp.Code != "200" {
		return errno.ErrEmailTemplate
	}

	return nil
}

func (email MnemonicPhraseEmailReq) SendEmail() error {
	var params MnemonicPhraseEmailReq
	params.Email = email.Email
	params.Title = email.Title
	params.Content = email.Content
	params.IsHtml = "1"
	jsonStr, err := json.Marshal(params)
	req, err := http.NewRequest("POST", HostValidate+"/send/email-user", bytes.NewBuffer(jsonStr))
	paramsMap := make(map[string]string)
	j, _ := json.Marshal(params)
	json.Unmarshal(j, &paramsMap)
	timestamp := strconv.FormatInt(time.Now().Unix(), 10)
	sign := utils.Signature(AppKey, paramsMap, AppSecret, timestamp)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Fzm-Ca-Timestamp", timestamp)
	req.Header.Set("Fzm-Ca-AppKey", AppKey)
	req.Header.Set("Fzm-Ca-Ip", "127.0.0.1")
	req.Header.Set("Fzm-Ca-Signature", sign)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	var validateEmailResp ValidateEmailResp
	err = json.Unmarshal(body, &validateEmailResp)

	if err != nil {
		global.GVA_LOG.Info("发送邮箱返回-2")
		global.GVA_LOG.Info(validateEmailResp.Code)
		global.GVA_LOG.Info(validateEmailResp.Error)
		global.GVA_LOG.Info(validateEmailResp.Message)
		global.GVA_LOG.Error(err.Error())
		return err
	}

	if validateEmailResp.Code != "200" {
		return errno.ErrEmailTemplate
	}

	return nil
}
