// Package ws -----------------------------
// @file      : handler.go
// @author    : JJXu
// @contact   : wavingbear@163.com
// @time      : 2022/10/23 11:13:43
// -------------------------------------------
package ws

import (
	"context"
	"encoding/json"
	"fonchain-fiee/api/account"
	"fonchain-fiee/api/accountFiee"
	"fonchain-fiee/pkg/config"
	"fonchain-fiee/pkg/e"
	"fonchain-fiee/pkg/service"
	"fonchain-fiee/pkg/utils/secret"
)

func AuthorizationVerify(sourceData []byte) (userInfo *accountFiee.ChatUserData, ok bool, err error) {
	var msg AuthorizationInfo
	err = json.Unmarshal(sourceData, &msg)
	if err != nil {
		return
	}
	if msg.Type != AuthorizationType {
		return
	}
	if msg.Content.Auth == "" {
		return
	}
	var ctx = context.Background()
	var accountInfo accountFiee.ChatUserData
	switch msg.Content.Domain {
	case config.Domain:
		//fiee token校验
		msg.Content.Auth, err = secret.GetJwtFromStr(msg.Content.Auth)
		if err != nil {
			return
		}
		var fieeJwtInfo *accountFiee.DecryptJwtResponse
		fieeJwtInfo, err = service.AccountFieeProvider.DecryptJwt(ctx, &accountFiee.DecryptJwtRequest{Token: msg.Content.Auth, Domain: msg.Content.Domain})
		if err != nil || fieeJwtInfo.IsOffline {
			return
		}
		accountInfo.Origin = msg.Content.Domain
		accountInfo.OriginId = int64(fieeJwtInfo.ID)
		accountInfo.Account = fieeJwtInfo.Account
		accountInfo.NickName = fieeJwtInfo.NickName
	case "fontree":
		//erp token校验
		msg.Content.Auth, err = secret.GetJwtFromStr(msg.Content.Auth)
		if err != nil {
			return
		}
		var fontreeJwtInfo *account.DecryptJwtResponse
		fontreeJwtInfo, err = service.AccountProvider.DecryptJwt(ctx, &account.DecryptJwtRequest{Token: msg.Content.Auth, Domain: msg.Content.Domain})
		if err != nil || fontreeJwtInfo.IsOffline {
			return
		}
		accountInfo.Origin = msg.Content.Domain
		accountInfo.OriginId = int64(fontreeJwtInfo.ID)
		accountInfo.Account = fontreeJwtInfo.Account
		accountInfo.NickName = fontreeJwtInfo.NickName
	}

	//查询是否已经注册
	var chatUserQuery *accountFiee.GetChatUserListResp
	chatUserQuery, err = service.AccountFieeProvider.GetChatUserList(ctx, &accountFiee.GetChatUserListRequest{
		Query:    &accountFiee.ChatUserData{OriginId: int64(accountInfo.ID), Origin: msg.Content.Domain},
		Page:     1,
		PageSize: 1,
	})
	//如果找不到聊天用户则创建
	if err != nil || chatUserQuery.Total > 0 {
		//注册客服
		createUserRes, err := service.AccountFieeProvider.CreateChatUser(ctx, &accountFiee.ChatUserData{
			NickName: accountInfo.NickName,
			Account:  accountInfo.Account,
			Role:     2,
			Origin:   msg.Content.Domain,
			OriginId: int64(accountInfo.ID),
		})
		if err != nil {
			return
		}
		userInfo = createUserRes.GetData()
	}
	ok = true
	return
}
func HandleMessage(sourceData []byte, cli *Client) {
	var msg WsInfo
	err := json.Unmarshal(sourceData, &msg)
	if err != nil {
		cli.Send <- WsErrorInvalidDataFormat(msg.From)
		return
	}
	switch msg.Type {
	default:
		cli.Send <- WsErrorUnknownMessageType(msg.From)
	case TestType:
		var newMsg = WsInfo{
			Type:    TestType,
			Content: msg.Content,
			From:    "0",
			To:      msg.From,
		}
		byteMsg, _ := json.Marshal(newMsg)
		cli.Send <- byteMsg
	case ChatType:
		if msg.From == "" {
			//客户端id不能为空
			cli.Send <- WsErrorMessage(ChatType, "null", e.ErrInvalidClientId, nil)
			return
		}
		var chatInfo ChatInfo
		_ = json.Unmarshal(sourceData, &chatInfo)
		//解析Content
		if clients, ok := cli.Room.clients[chatInfo.Content.TargetUserId]; ok {
			for _, targetObj := range clients {
				if targetObj != nil {
					targetObj.Send <- WsChatMessage(msg.From, chatInfo.Content.TargetClientId, chatInfo.Content.Msg)
				}
			}
		} else {
			//对方不在线
			cli.Send <- WsErrorMessage(ChatType, msg.From, e.ErrTargetOutLine, nil)
		}
	}
}

type ChatInfo struct {
	Type    WsType      `json:"type"`    //消息类型
	Content ChatContent `json:"content"` //消息内容
	From    string      `json:"from"`    //发送者 0为服务端,客户端填写clientId
	To      string      `json:"to"`      //接收者 接收消息的用户id
}
type ChatContent struct {
	TargetUuid     string `json:"targetUuid"`
	TargetUserId   int64  `json:"targetUserId"`
	TargetClientId string `json:"targetClientId"`
	Msg            string `json:"msg"`
}
