package asChat

import (
	"encoding/json"
	"fmt"
	"fonchain-fiee/api/account"
	"fonchain-fiee/api/accountFiee"
	"fonchain-fiee/cmd/config"
	"fonchain-fiee/pkg/service"
	"fonchain-fiee/pkg/service/asChat/dto"
	"fonchain-fiee/pkg/utils"
	"fonchain-fiee/pkg/utils/secret"
	"fonchain-fiee/pkg/utils/stime"
	"math/rand"
	"time"

	"github.com/gin-gonic/gin"
)

var Handler = &ChatAutoReplyRulerHandler{}

type ChatAutoReplyRulerHandler struct {
}

// 创建自动回复规则
func (a *ChatAutoReplyRulerHandler) CreateChatAutoReplyRuler(c *gin.Context) {
	var req dto.ChatAutoReplyData
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	rulerBytes, _ := json.Marshal(req.Rules)
	protoReq := accountFiee.ChatAutoReplyRulerData{
		Title:    req.Title,
		Ruler:    string(rulerBytes),
		Response: req.Response,
	}
	_, err := service.AccountFieeProvider.CreateChatAutoReplyRuler(c, &protoReq)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c)
}

// 删除自动回复规则
func (a *ChatAutoReplyRulerHandler) DeleteChatAutoReplyRuler(c *gin.Context) {
	var req accountFiee.DeleteChatAutoReplyRulerRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	_, err := service.AccountFieeProvider.DeleteChatAutoReplyRuler(c, &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c)
}

// 更新自动回复规则
func (a *ChatAutoReplyRulerHandler) UpdateChatAutoReplyRuler(c *gin.Context) {
	var req dto.ChatAutoReplyData
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	protoReq := req.ToProtoData()
	_, err := service.AccountFieeProvider.UpdateChatAutoReplyRuler(c, protoReq)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c)
}

// 使用id查询自动回复规则
func (a *ChatAutoReplyRulerHandler) GetChatAutoReplyRulerDetail(c *gin.Context) {
	var req accountFiee.GetChatAutoReplyRulerByIdRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	resp, err := service.AccountFieeProvider.GetChatAutoReplyRulerDetail(c, &req)
	if err != nil {
		service.Error(c, err)
		return
	}
	tmp := dto.ChatAutoReplyData{}
	tmp.Parse(resp)
	service.Success(c, tmp)
}

// 批量查询自动回复规则
func (a *ChatAutoReplyRulerHandler) GetChatAutoReplyRulerList(c *gin.Context) {
	var req dto.GetChatAutoReplyRulerListRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	var protoReq = accountFiee.GetChatAutoReplyRulerListRequest{Query: &accountFiee.ChatAutoReplyRulerData{}}
	utils.RequestDataConvert(&req, &protoReq)
	resp, err := service.AccountFieeProvider.GetChatAutoReplyRulerList(c, &protoReq)
	if err != nil {
		service.Error(c, err)
		return
	}
	var data []dto.ChatAutoReplyData
	for _, v := range resp.List {
		tmp := dto.ChatAutoReplyData{}
		tmp.Parse(v)
		data = append(data, tmp)
	}
	service.Success(c, map[string]interface{}{
		"data":     data,
		"page":     resp.Page,
		"pagesize": resp.PageSize,
		"total":    resp.Total,
	})
}
func (a *ChatAutoReplyRulerHandler) ErpLoginDemo(c *gin.Context) {
	var req dto.ErpLoginDemoReq
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	loginRes, err := service.AccountProvider.Login(c, &account.LoginRequest{
		Domain:   "fontree",
		TelNum:   req.TelNum,
		Password: req.Password,
	})
	if err != nil {
		if err.Error() == "没有找到数据" || err.Error() == "No data found" {
			registerRequest := account.RegistRequest{
				Domain:    "fontree",
				NickName:  req.TelNum,
				TelNum:    req.TelNum,
				Password:  req.Password,
				EnterDate: time.Now().Format(stime.Format_Normal_YMD),
				Extend:    &account.Extend{JumpTo: "onsite"}, //origin-老平台  onsite 当前
				JobNum:    fmt.Sprintf("%d", rand.Intn(1000)),
			}
			registerRes, errs := service.AccountProvider.Register(c, &registerRequest)
			if errs != nil {
				service.Error(c, errs)
				return
			}
			service.Success(c, registerRes)
		} else {
			service.Error(c, err)
		}
		return
	}

	departmentName := ""
	if loginRes.AccountInfo != nil && len(loginRes.AccountInfo.Departments) > 0 {
		departmentName = loginRes.AccountInfo.Departments[0].Name
	}
	loginRes.Token, err = secret.CombineSecret("xxx", departmentName, loginRes.Token)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, loginRes)
}
func (a *ChatAutoReplyRulerHandler) FieeLoginDemo(c *gin.Context) {
	var req dto.ErpLoginDemoReq
	if err := c.ShouldBindJSON(&req); err != nil {
		service.Error(c, err)
		return
	}
	loginRes, err := service.AccountFieeProvider.Login(c, &accountFiee.LoginRequest{
		Domain:   config.AppConfig.System.Domain,
		TelNum:   req.TelNum,
		Password: req.Password,
	})
	if err != nil {
		if err.Error() == "账号不存在" || err.Error() == "Account does not exist" {
			registerRequest := accountFiee.RegistRequest{
				Domain:   config.AppConfig.System.Domain,
				NickName: req.TelNum,
				TelNum:   req.TelNum,
				//Password:  req.Password,
				//EnterDate: time.Now().Format(stime.Format_Normal_YMD),
				//Extend:    &account.Extend{JumpTo: "onsite"}, //origin-老平台  onsite 当前
				//JobNum:    fmt.Sprintf("%d", rand.Intn(1000)),
			}
			registerRes, errs := service.AccountFieeProvider.Register(c, &registerRequest)
			if errs != nil {
				service.Error(c, errs)
				return
			}
			service.Success(c, registerRes)
		} else {
			service.Error(c, err)
		}
		return
	}

	departmentName := ""
	if loginRes.AccountInfo != nil && len(loginRes.AccountInfo.Departments) > 0 {
		departmentName = loginRes.AccountInfo.Departments[0].Name
	}
	loginRes.Token, err = secret.CombineSecret("xxx", departmentName, loginRes.Token)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, loginRes)
}
func (a *ChatAutoReplyRulerHandler) Test(c *gin.Context) {
	fmt.Println(c.GetHeader("domain"))
}
