package logic

import (
	"errors"
	"fmt"
	"github.com/fonchain_enterprise/micro-account/api/accountFiee"
	"github.com/fonchain_enterprise/micro-account/pkg/common/utils"
	"github.com/fonchain_enterprise/micro-account/pkg/common/utils/stime"
	"github.com/fonchain_enterprise/micro-account/pkg/dao"
	"github.com/fonchain_enterprise/micro-account/pkg/model"
	"github.com/jinzhu/copier"
)

type AsChatLogic struct{}

// 创建ChatRecord
func (AsChatLogic) CreateChatRecord(data *accountFiee.ChatRecordData) (resp *accountFiee.CreateChatRecordResp, err error) {
	resp = &accountFiee.CreateChatRecordResp{
		Data: &accountFiee.ChatRecordData{},
		Msg:  "创建成功",
	}
	fmt.Println("原始数据", data)
	var modelData = model.ChatRecord{}
	if err = copier.CopyWithOption(&modelData, data, utils.CopierProtoOptions); err != nil {
		resp.Msg = "数据转换失败"
		return
	}
	fmt.Println("转换后数据", modelData)
	err = dao.AsChatDao.CreateChatRecord(&modelData)
	if err != nil {
		resp.Msg = "创建失败"
		return
	}
	if len(data.Medias) > 0 {
		model.DB.Preload("Medias").First(&modelData, modelData.ID)
	}
	if err = copier.CopyWithOption(resp.Data, &modelData, utils.CopierProtoOptions); err != nil {
		resp.Msg = "创建失败."
		return
	}
	if len(data.Medias) > 0 {
		for _, v := range data.Medias {
			mediaModelData, _ := dao.AsChatDao.GetChatMediaById(v.ID)
			resp.Data.Medias = append(resp.Data.Medias, &accountFiee.ChatMediaData{
				ID:        mediaModelData.ID,
				CreatedAt: mediaModelData.CreatedAt.Format(stime.Format_Normal_YMDhms),
				UpdatedAt: mediaModelData.UpdatedAt.Format(stime.Format_Normal_YMDhms),
				Url:       mediaModelData.Url,
				Md5:       mediaModelData.Md5,
				Size:      mediaModelData.Size,
				Ext:       mediaModelData.Ext,
				ConvText:  mediaModelData.ConvText,
				Duration:  mediaModelData.Duration,
			})
		}
	}
	return
}

// 删除ChatRecord
func (AsChatLogic) DeleteChatRecords(req *accountFiee.DeleteChatRecordRequest) (resp *accountFiee.CommonMsg, err error) {
	err = dao.AsChatDao.DeleteChatRecords(req.Id, req.Ids)
	return
}

// 更新ChatRecord
func (AsChatLogic) UpdateChatRecord(data *accountFiee.ChatRecordData) (resp *accountFiee.CommonMsg, err error) {
	resp = new(accountFiee.CommonMsg)
	if data.ID == 0 {
		resp.Msg = "ID不能为空"
		err = errors.New(resp.Msg)
		return
	}
	var modelData = model.ChatRecord{}
	if err = copier.CopyWithOption(&modelData, data, utils.CopierProtoOptions); err != nil {
		return
	}
	err = dao.AsChatDao.UpdateChatRecord(&modelData)
	return
}

// 覆盖ChatRecord
func (AsChatLogic) SaveChatRecord(data *accountFiee.ChatRecordData) (resp *accountFiee.CommonMsg, err error) {
	resp = new(accountFiee.CommonMsg)
	if data.ID == 0 {
		resp.Msg = "ID不能为空"
		err = errors.New(resp.Msg)
		return
	}
	var modelData = model.ChatRecord{}
	if err = copier.CopyWithOption(&modelData, data, utils.CopierProtoOptions); err != nil {
		return
	}
	err = dao.AsChatDao.SaveChatRecord(&modelData)
	return
}

// 使用id查询ChatRecord
func (AsChatLogic) GetChatRecordById(req *accountFiee.GetChatRecordByIdRequest) (data *accountFiee.ChatRecordData, err error) {
	data = &accountFiee.ChatRecordData{}
	modelData, err := dao.AsChatDao.GetChatRecordById(int(req.Id))
	if err != nil {
		return nil, err
	}
	if err = copier.CopyWithOption(&data, &modelData, utils.CopierProtoOptions); err != nil {
		return nil, err
	}
	return
}

// 批量查询ChatRecord
func (AsChatLogic) GetChatRecordList(req *accountFiee.GetChatRecordListRequest) (resp *accountFiee.GetChatRecordListResp, err error) {
	resp = &accountFiee.GetChatRecordListResp{
		List:     []*accountFiee.ChatRecordData{},
		Page:     req.Page,
		PageSize: req.PageSize,
	}
	var data = []model.ChatRecord{}
	data, resp.Total, err = dao.AsChatDao.GetChatRecordList(req)
	if err != nil {
		return
	}
	fmt.Println("GetChatRecordList len(data):", len(data))
	if err = copier.CopyWithOption(&resp.List, &data, utils.CopierProtoOptions); err != nil {
		return
	}
	fmt.Println("GetChatRecordList len(resp.List):", len(resp.List))
	return
}
func (AsChatLogic) RegisterWaiter(request *accountFiee.RegisterWaiterRequest) (resp *accountFiee.RegisterWaiterResp, err error) {
	resp = &accountFiee.RegisterWaiterResp{
		UserId: 0,
	}
	var waiterUserInfo = model.ChatUser{
		OriginId: request.OriginId,
		Account:  request.Account,
		Role:     2,
		NickName: request.NickName,
		Origin:   request.Origin,
		Avatar:   request.Avatar,
	}
	err = dao.ChatUserDao.CreateChatUser(&waiterUserInfo)
	if err != nil {
		return
	}
	return
}

// 创建ChatMedia
func (AsChatLogic) CreateChatMedia(data *accountFiee.ChatMediaData) (resp *accountFiee.CreateChatMediaResp, err error) {
	resp = &accountFiee.CreateChatMediaResp{
		Data: &accountFiee.ChatMediaData{},
		Msg:  "创建成功",
	}
	var modelData = model.ChatMedia{}
	if err = copier.CopyWithOption(&modelData, data, utils.CopierProtoOptions); err != nil {
		resp.Msg = "数据转换失败"
		return
	}
	err = dao.AsChatDao.CreateChatMedia(&modelData)
	if err != nil {
		resp.Msg = "创建失败"
		return
	}
	if err = copier.CopyWithOption(resp.Data, &modelData, utils.CopierProtoOptions); err != nil {
		resp.Msg = "创建失败."
		return
	}
	return
}

// 删除ChatMedia
func (AsChatLogic) DeleteChatMedias(req *accountFiee.DeleteChatMediaRequest) (resp *accountFiee.CommonMsg, err error) {
	err = dao.AsChatDao.DeleteChatMedias(req.Id, req.Ids)
	return
}

// 更新ChatMedia
func (AsChatLogic) UpdateChatMedia(data *accountFiee.ChatMediaData) (resp *accountFiee.CommonMsg, err error) {
	resp = new(accountFiee.CommonMsg)
	if data.ID == 0 {
		resp.Msg = "ID不能为空"
		err = errors.New(resp.Msg)
		return
	}
	var modelData = model.ChatMedia{}
	if err = copier.CopyWithOption(&modelData, data, utils.CopierProtoOptions); err != nil {
		return
	}
	err = dao.AsChatDao.UpdateChatMedia(&modelData)
	return
}

// 覆盖ChatMedia
func (AsChatLogic) SaveChatMedia(data *accountFiee.ChatMediaData) (resp *accountFiee.CommonMsg, err error) {
	resp = new(accountFiee.CommonMsg)
	if data.ID == 0 {
		resp.Msg = "ID不能为空"
		err = errors.New(resp.Msg)
		return
	}
	var modelData = model.ChatMedia{}
	if err = copier.CopyWithOption(&modelData, data, utils.CopierProtoOptions); err != nil {
		return
	}
	err = dao.AsChatDao.SaveChatMedia(&modelData)
	return
}

// 使用id查询ChatMedia
func (AsChatLogic) GetChatMediaById(req *accountFiee.GetChatMediaByIdRequest) (data *accountFiee.ChatMediaData, err error) {
	data = &accountFiee.ChatMediaData{}
	modelData, err := dao.AsChatDao.GetChatMediaById(req.Id)
	if err != nil {
		return nil, err
	}
	if err = copier.CopyWithOption(&data, &modelData, utils.CopierProtoOptions); err != nil {
		return nil, err
	}
	return
}

// 批量查询ChatMedia
func (AsChatLogic) GetChatMediaList(req *accountFiee.GetChatMediaListRequest) (resp *accountFiee.GetChatMediaListResp, err error) {
	resp = &accountFiee.GetChatMediaListResp{
		List:     []*accountFiee.ChatMediaData{},
		Page:     req.Page,
		PageSize: req.PageSize,
	}
	var data = []model.ChatMedia{}
	data, resp.Total, err = dao.AsChatDao.GetChatMediaList(req)
	if err != nil {
		return
	}
	if err = copier.CopyWithOption(&resp.List, &data, utils.CopierProtoOptions); err != nil {
		return
	}
	return
}

func (AsChatLogic) GetChatUserList2(req *accountFiee.GetChatUserListRequest2) (resp *accountFiee.GetChatUserListResp2, err error) {
	resp = &accountFiee.GetChatUserListResp2{
		List:     []*accountFiee.ChatUser2{},
		Page:     req.Page,
		PageSize: req.PageSize,
	}
	resp.List, resp.Total, err = dao.AsChatDao.GetChatUserList(req)
	if err != nil {
		return
	}
	return
}
