package controllers

import (
	"exhibitionCentre/base"
	"exhibitionCentre/logging"
	"exhibitionCentre/models"
	"exhibitionCentre/utils"
)

// 展品详情消息
type ExhibitDetailRequest struct {
	ID int `mapper:"id" json:"id"`
}
type ExhibitDetailResponse struct {
	ID                 int                    `json:"id"`                  // 展品id
	Title              string                 `json:"title"`               // 展品名称
	AuthorIntroduction string                 `json:"author_introduction"` // 作者介绍
	Audio              string                 `json:"audio"`               // 展品语音介绍
	Introduction       string                 `json:"introduction"`        // 展品文字介绍
	Details            []models.ExhibitDetail `json:"details"`             // 展品图文介绍
	Likes              int                    `json:"likes"`               // 展品点赞数
	Flowers            int                    `json:"flowers"`             // 展品送花数
	Applauses          int                    `json:"applauses"`           // 展品鼓掌数
	RoomIndex          int                    `json:"room_index"`          // 1为开幕式，2为展厅，3为演播厅
}

// 点赞消息
type ExhibitLikeRequest struct {
	ID int `mapper:"id" json:"id"`
}
type ExhibitLikeResponse struct {
	Count int `json:"count"`
}

// 送花消息
type ExhibitSendFlowerRequest struct {
	ID int `mapper:"id" json:"id"`
}
type ExhibitSendFlowerResponse struct {
	Count int `json:"count"`
}

// 鼓掌消息
type ExhibitGiveApplauseRequest struct {
	ID int `mapper:"id" json:"id"`
}
type ExhibitGiveApplauseResponse struct {
	Count int `json:"count"`
}

// 出价消息
type ExhibitOfferRequest struct {
	ID       int    `json:"id"`       // 展品id
	UserName string `json:"username"` // 姓名
	Phone    string `json:"phone"`    // 手机号
	Bid      int    `json:"bid"`      // 报价
}

// todo 测试
// 获取展位详情信息
func GetExhibitDetail(router *base.Router) (int, []byte) {
	// 解析request数据
	var request ExhibitDetailRequest
	if err := utils.ParseQuery(router.Req, &request); err != nil {
		logging.Log.Errorf("解析数据失败, err: %v\n", err.Error())
		return 400, utils.SerializeErrorData("获取展品失败!")
	}
	// 数据库获取展品
	exhibit, err := models.GetExhibit(request.ID)
	if err != nil {
		logging.Log.Errorf("数据库获取展品失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("获取展品失败!")
	}
	// 解析展品详情
	var details []models.ExhibitDetail
	if len(exhibit.Details) > 0 {
		if err := utils.JsonParse([]byte(exhibit.Details), &details); err != nil {
			logging.Log.Error("解析models.Exhibit.Details数据失败: ", err.Error())
			return 400, utils.SerializeErrorData("获取展品失败!")
		}
	}
	resp := ExhibitDetailResponse{
		ID:                 int(exhibit.ID),
		Title:              exhibit.Title,
		AuthorIntroduction: exhibit.AuthorIntroduction,
		Audio:              exhibit.Audio,
		Introduction:       exhibit.Introduction,
		Likes:              exhibit.Likes,
		Flowers:            exhibit.Flowers,
		Applauses:          exhibit.Applauses,
		Details:            details,
		RoomIndex:          exhibit.RoomIndex,
	}
	return 200, utils.Serialize(&resp)
}

// 点赞
func ExhibitLike(router *base.Router) (int, []byte) {
	// 解析request数据
	var request ExhibitLikeRequest
	if err := utils.ParseQuery(router.Req, &request); err != nil {
		logging.Log.Errorf("解析数据失败, err: %v\n", err.Error())
		return 400, utils.SerializeErrorData("点赞失败!")
	}
	// 数据库获取展品
	exhibit, err := models.GetExhibit(request.ID)
	if err != nil {
		logging.Log.Errorf("数据库获取展品失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("点赞失败!")
	}
	// 数据库获取展览
	exhibition, err := models.GetExhibition(int(exhibit.ExhibitionId))
	if err != nil {
		logging.Log.Errorf("数据库获取展览失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("点赞失败!")
	}
	exhibit.Likes += 1
	exhibition.Likes += 1
	models.UpdateExhibit(exhibit)
	models.UpdateExhibition(exhibition)

	resp := ExhibitLikeResponse{
		Count: exhibit.Likes,
	}
	return 200, utils.Serialize(&resp)
}

// 送花
func ExhibitSendFlower(router *base.Router) (int, []byte) {
	// 解析request数据
	var request ExhibitSendFlowerRequest
	if err := utils.ParseQuery(router.Req, &request); err != nil {
		logging.Log.Errorf("解析数据失败, err: %v\n", err.Error())
		return 400, utils.SerializeErrorData("参数错误!")
	}
	// 数据库获取展品
	exhibit, err := models.GetExhibit(request.ID)
	if err != nil {
		logging.Log.Errorf("数据库获取展品失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("送花失败!")
	}
	// 数据库获取展览
	exhibition, err := models.GetExhibition(int(exhibit.ExhibitionId))
	if err != nil {
		logging.Log.Errorf("数据库获取展览失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("送花失败!")
	}
	exhibit.Flowers += 1
	exhibition.Flowers += 1
	models.UpdateExhibit(exhibit)
	models.UpdateExhibition(exhibition)

	resp := ExhibitSendFlowerResponse{
		Count: exhibit.Flowers,
	}
	return 200, utils.Serialize(&resp)
}

// 鼓掌
func ExhibitGiveApplause(router *base.Router) (int, []byte) {
	// 解析request数据
	var request ExhibitGiveApplauseRequest
	if err := utils.ParseQuery(router.Req, &request); err != nil {
		logging.Log.Errorf("解析数据失败, err: %v\n", err.Error())
		return 400, utils.SerializeErrorData("鼓掌失败!")
	}
	// 数据库获取展品
	exhibit, err := models.GetExhibit(request.ID)
	if err != nil {
		logging.Log.Errorf("数据库获取展品失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("鼓掌失败!")
	}
	// 数据库获取展览
	exhibition, err := models.GetExhibition(int(exhibit.ExhibitionId))
	if err != nil {
		logging.Log.Errorf("数据库获取展览失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("鼓掌失败!")
	}
	exhibit.Applauses += 1
	exhibition.Applauses += 1
	models.UpdateExhibit(exhibit)
	models.UpdateExhibition(exhibition)

	resp := ExhibitGiveApplauseResponse{
		Count: exhibit.Applauses,
	}
	return 200, utils.Serialize(&resp)
}

// 出价
func ExhibitOffer(router *base.Router) (int, []byte) {
	// 解析request数据
	var info ExhibitOfferRequest
	if err := utils.JsonParse(router.Data, &info); err != nil {
		logging.Log.Error("解析数据失败: ", err.Error())
		return 400, utils.SerializeErrorData("出价失败!")
	}
	// 数据库获取展品
	exhibit, err := models.GetExhibit(info.ID)
	if err != nil {
		logging.Log.Errorf("数据库获取展品失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("出价失败!")
	}
	// 构造订单
	order := models.Order{
		UserName:     info.UserName,
		Phone:        info.Phone,
		Price:        info.Bid,
		ExhibitId:    exhibit.ID,
		ExhibitionId: exhibit.ExhibitionId,
	}
	// 数据库保存订单
	if err := models.CreateOrder(&order); err != nil {
		logging.Log.Errorf("数据库创建展品订单失败: %v\n", err.Error())
		return 400, utils.SerializeErrorData("出价失败!")
	}
	return 200, []byte("{}")
}
