package app

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"bwallet/service/live_info_service"
	live_title_record_service2 "bwallet/service/live_title_record_service"
	"bwallet/validate_service"
	"github.com/Unknwon/com"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

func AddLive(c *gin.Context) {
	info := validate_service.LiveInfo{}
	c.ShouldBindJSON(&info)

	//方法一
	if ok, errors := validate_service.ValidateInputs(info); !ok {
		for _, err := range errors {
			handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
			return
		}
	}

	infoService := live_info_service.LiveInfo{
		Title:      info.Title,
		Cover:      info.Cover,
		CategoryId: info.CategoryId,
		Duration:   info.Duration,
		ApplyId:    info.ApplyId,
		ApplyName:  info.ApplyName,
		PlatformId: info.PlatformId,
		Price:      info.Price,
	}

	if err := infoService.Add(); err != nil {
		handler.SendResponse(c, errno.ErrAddLiveInfo, nil)
		return
	}

	handler.SendResponse(c, nil, nil)
}

func GetLiveInfos(c *gin.Context) {
	platform_id, _ := strconv.Atoi(c.Request.Header.Get("FZM-PLATFORM-ID"))
	valid := validation.Validation{}
	valid.Min(platform_id, 1, "platform_id").Message("平台Id不能为空")
	if valid.HasErrors() {
		handler.SendResponse(c, valid.Errors[0], nil)
		return
	}

	status := com.StrTo(c.DefaultQuery("status", "0")).MustUint8()
	live_status := com.StrTo(c.DefaultQuery("live_status", "0")).MustUint8()
	infoService := live_info_service.LiveInfo{
		Status:     status,
		LiveStatus: live_status,
		PlatformId: int64(platform_id),
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

	total, err := infoService.Count()
	if err != nil {
		handler.SendResponse(c, errno.ErrCountLiveInfo, nil)
		return
	}

	info, err := infoService.GetAllForClient()
	if err != nil {
		handler.SendResponse(c, errno.InternalServerError, nil)
		return
	}

	data := make(map[string]interface{})
	data["items"] = info
	data["total"] = total

	handler.SendResponse(c, nil, data)
}

func ModifyTitle(c *gin.Context) {
	platform_id, _ := strconv.Atoi(c.Request.Header.Get("FZM-PLATFORM-ID"))
	valid := validation.Validation{}
	valid.Min(platform_id, 1, "platform_id").Message("平台Id不能为空")
	if valid.HasErrors() {
		handler.SendResponse(c, valid.Errors[0], nil)
		return
	}

	info := validate_service.ModifyLiveTitle{}
	c.ShouldBindJSON(&info)

	if ok, errors := validate_service.ValidateInputs(info); !ok {
		for _, err := range errors {
			handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
			return
		}
	}

	infoService := live_info_service.LiveInfo{
		Id:      info.Id,
		ApplyId: info.ApplyId,
	}

	count, err := infoService.Count()
	if 0 == count || err != nil {
		handler.SendResponse(c, errno.ErrLiveInfoNotFound, nil)
		return
	}

	model, err := infoService.GetOneLiveInfo()
	if err != nil {
		handler.SendResponse(c, errno.ErrLiveInfoNotFound, nil)
		return
	}

	live_title_record_service := live_title_record_service2.LiveTitleRecord{
		LiveId:       info.Id,
		CurrentTitle: model.Title,
		Status:       0,
		NewTitle:     info.Title,
		PlatformId:   int64(platform_id),
		ApplyName:    model.ApplyName,
	}

	if err := live_title_record_service.Add(); err != nil {
		handler.SendResponse(c, errno.ErrUpdateLiveInfo, nil)
		return
	}

	handler.SendResponse(c, nil, nil)
}

func LiveStatus(c *gin.Context) {

}

func GetLiveCharge(c *gin.Context) {
	platform_id, _ := strconv.Atoi(c.Request.Header.Get("FZM-PLATFORM-ID"))
	valid := validation.Validation{}
	valid.Min(platform_id, 1, "platform_id").Message("平台Id不能为空")
	if valid.HasErrors() {
		handler.SendResponse(c, valid.Errors[0], nil)
		return
	}

	data := make(map[string]interface{})
	data["uint"] = "BTY"
	data["uint_price"] = 1.1

	handler.SendResponse(c, nil, data)
}
