package services

import (
	"encoding/json"
	"eta/eta_mobile/utils"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strings"
)

const (
	LoginAuthCodeSource = 2
)

// MiddleServerResultData 中间服务响应体
type MiddleServerResultData struct {
	Code   int         `json:"code" description:"状态码"`
	Msg    string      `json:"msg" description:"提示信息"`
	Data   interface{} `json:"data" description:"返回数据"`
	ErrMsg string      `json:"-" description:"错误信息，不用返回给前端，只是做日志记录"`
}

// GetLoginAuthCodeReq 获取登录编码请求体
type GetLoginAuthCodeReq struct {
	AdminName string `json:"admin_name" description:"用户名"`
	Source    int    `json:"source" description:"来源: 1-CRM; 2-ETA"`
}

// GetAuthCodeFromMiddleServer CRM_ETA服务-获取登录编码
func GetAuthCodeFromMiddleServer(adminName string) (authCode string, err error) {
	if utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeSandbox && utils.BusinessCode != utils.BusinessCodeDebug {
		return
	}
	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/auth/auth_code")
	param := GetLoginAuthCodeReq{
		Source:    LoginAuthCodeSource,
		AdminName: adminName,
	}
	data, e := json.Marshal(param)
	if e != nil {
		err = fmt.Errorf("data json marshal err: %s", e.Error())
		return
	}

	body := ioutil.NopCloser(strings.NewReader(string(data)))
	client := &http.Client{}
	req, e := http.NewRequest("POST", url, body)
	if e != nil {
		err = fmt.Errorf("http create request err: %s", e.Error())
		return
	}

	contentType := "application/json;charset=utf-8"
	req.Header.Set("Content-Type", contentType)
	req.Header.Set("Authorization", utils.CrmEtaAuthorization)
	resp, e := client.Do(req)
	if e != nil {
		err = fmt.Errorf("http client do err: %s", e.Error())
		return
	}
	defer func() {
		_ = resp.Body.Close()
	}()
	b, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		err = fmt.Errorf("resp body read err: %s", e.Error())
		return
	}
	if len(b) == 0 {
		err = fmt.Errorf("resp body is empty")
		return
	}
	// 生产环境解密, 注意有个坑前后的双引号
	if utils.RunMode == "release" {
		str := string(b)
		str = strings.Trim(str, `"`)
		b = utils.DesBase64Decrypt([]byte(str), utils.DesKey)
	}

	result := new(MiddleServerResultData)
	if e = json.Unmarshal(b, &result); e != nil {
		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
		return
	}
	if result.Code != 200 {
		err = fmt.Errorf("result: %s", string(b))
		return
	}

	code, ok := result.Data.(string)
	if !ok {
		err = fmt.Errorf("auth code empty")
		return
	}
	authCode = code
	return
}

// GetCrmTokenReq 获取Token请求体
type GetCrmTokenReq struct {
	AuthCode string `json:"auth_code"`
}

// GetCrmTokenDataResp 获取Token信息响应体
type GetCrmTokenDataResp struct {
	Code int             `json:"code" description:"状态码"`
	Msg  string          `json:"msg" description:"提示信息"`
	Data GetCrmTokenData `json:"data" description:"返回数据"`
}

// GetCrmTokenData Token数据
type GetCrmTokenData struct {
	Authorization   string `description:"Auth Token"`
	AdminName       string `description:"系统用户名称"`
	RealName        string `description:"系统用户姓名"`
	RoleName        string `description:"角色名称"`
	RoleTypeCode    string `description:"角色类型编码"`
	SysRoleTypeCode string `description:"角色类型编码"`
	AdminId         int    `description:"系统用户id"`
	ProductName     string `description:"产品名称：admin,ficc,权益"`
	Authority       int    `description:"管理权限，0：无，1：部门负责人，2：小组负责人,或者ficc销售主管，4:ficc销售组长"`
}

// CodeLoginFromMiddleServer 中间服务-编码登录
func CodeLoginFromMiddleServer(authCode string) (tokenResp GetCrmTokenData, err error) {
	if utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeSandbox && utils.BusinessCode != utils.BusinessCodeDebug {
		return
	}
	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/auth/eta_token")
	param := GetCrmTokenReq{
		AuthCode: authCode,
	}
	data, e := json.Marshal(param)
	if e != nil {
		err = fmt.Errorf("data json marshal err: %s", e.Error())
		return
	}

	body := io.NopCloser(strings.NewReader(string(data)))
	client := &http.Client{}
	req, e := http.NewRequest("POST", url, body)
	if e != nil {
		err = fmt.Errorf("http create request err: %s", e.Error())
		return
	}

	contentType := "application/json;charset=utf-8"
	req.Header.Set("Content-Type", contentType)
	req.Header.Set("Authorization", utils.CrmEtaAuthorization)
	resp, e := client.Do(req)
	if e != nil {
		err = fmt.Errorf("http client do err: %s", e.Error())
		return
	}
	defer func() {
		_ = resp.Body.Close()
	}()
	b, e := io.ReadAll(resp.Body)
	if e != nil {
		err = fmt.Errorf("resp body read err: %s", e.Error())
		return
	}
	if len(b) == 0 {
		err = fmt.Errorf("resp body is empty")
		return
	}
	// 生产环境解密, 注意有个坑前后的双引号
	if utils.RunMode == "release" {
		str := string(b)
		str = strings.Trim(str, `"`)
		b = utils.DesBase64Decrypt([]byte(str), utils.DesKey)
	}

	result := new(GetCrmTokenDataResp)
	if e = json.Unmarshal(b, &result); e != nil {
		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
		return
	}
	if result.Code != 200 {
		err = fmt.Errorf("result: %s", string(b))
		return
	}
	tokenResp = result.Data
	return
}
