package backend

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"bwallet/service/live_info_service"
	"bwallet/validate_service"
	"github.com/Unknwon/com"
	"github.com/gin-gonic/gin"
	"strings"
)

func GetLiveInfo(c *gin.Context) {
	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)
	group := user.UserInfo.Group

	var platform_id int
	platform_id = user.UserInfo.PlatformId
	if ("administrator" == group) {
		if arg := c.Query("platform_id"); arg != "" {
			platform_id = com.StrTo(c.Query("platform_id")).MustInt()
		}
	}

	infoService := live_info_service.LiveInfo{
		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.GetAll()
	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 AddLiveInfo(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,
		Attendance: info.Attendance,
		Mode:       info.Mode,
		ApplyId:    info.ApplyId,
		Sort:       info.Sort,
	}

	infoService.Add()

	handler.SendResponse(c, nil, nil)
}

func VerifyLiveInfo(c *gin.Context) {
	info := validate_service.VerifyLiveInfo{}
	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,
		Status: info.Status,
	}

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

	handler.SendResponse(c, nil, nil)
}

func EditLiveInfo(c *gin.Context) {
	info := validate_service.EditLiveInfo{}
	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,
		Title:      info.Title,
		Cover:      info.Cover,
		CategoryId: info.CategoryId,
		Duration:   info.Duration,
		Attendance: info.Attendance,
		Mode:       info.Mode,
		ApplyId:    info.ApplyId,
		Sort:       info.Sort,
	}

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

	handler.SendResponse(c, nil, nil)
}

func DeleteLiveInfo(c *gin.Context) {
	//id := com.StrTo(c.DefaultQuery("id", "0")).MustInt()
	//valid := validation.Validation{}
	//valid.Min(id, 1, "id").Message("Id必须大于0")
	//if valid.HasErrors() {
	//	handler.SendResponse(c, valid.Errors[0], nil)
	//	return
	//}
	//
	//infoService := live_info_service.LiveInfo{Id: id}
	//exists, err := infoService.ExistById()
	//if err != nil {
	//	handler.SendResponse(c, errno.ErrLiveInfoNotFound, nil)
	//	return
	//}
	//
	//if !exists {
	//	handler.SendResponse(c, errno.ErrLiveInfoNotFound, nil)
	//	return
	//}
	//
	//token := c.Request.Header.Get("Token")
	//user, _ := util.ParseToken(token)
	//group := user.UserInfo.Group
	//if ("administrator" != group) {
	//	info, _ := infoService.GetOneLiveInfo()
	//	if info.PlatformId != int64(user.UserInfo.PlatformId) {
	//		handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
	//		return
	//	}
	//}
	//err = infoService.Delete()
	//if err != nil {
	//	handler.SendResponse(c, errno.ErrDeleteLiveInfo, nil)
	//	return
	//}
	//
	//handler.SendResponse(c, nil, nil)
}
