package backend

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

func GetLiveCategory(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()
		}
	}

	categoryService := live_category_service.LiveCategory{
		PlatformId: int64(platform_id),
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

	category, err := categoryService.GetAll()
	if err != nil {
		handler.SendResponse(c, errno.InternalServerError, nil)
		return
	}

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

	handler.SendResponse(c, nil, data)
}

func AddLiveCategory(c *gin.Context) {
	category := validate_service.LiveCategory{}
	c.ShouldBindJSON(&category)

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

	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 category.PlatformId != 0 {
			platform_id = category.PlatformId
		}
	}

	categoryService := live_category_service.LiveCategory{
		Name:       category.Name,
		Sort:       category.Sort,
		Logo:       category.Logo,
		PlatformId: int64(platform_id),
	}

	categoryService.Add()

	handler.SendResponse(c, nil, nil)
}

func EditLiveCategory(c *gin.Context) {
	category := validate_service.EditLiveCategory{}
	c.ShouldBindJSON(&category)

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

	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)
	group := user.UserInfo.Group
	platform_id := user.UserInfo.PlatformId

	if ("administrator" != group && category.PlatformId != platform_id) {
		handler.SendResponse(c, errno.ErrUserTokenIncorrect, nil)
		return
	}

	if ("administrator" == group) {
		if category.PlatformId != 0 {
			platform_id = category.PlatformId
		}
	}

	categoryService := live_category_service.LiveCategory{
		Id:         category.Id,
		Name:       category.Name,
		Sort:       category.Sort,
		Logo:       category.Logo,
		PlatformId: int64(platform_id),
	}

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

	handler.SendResponse(c, nil, nil)
}

func DeleteLiveCategory(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
	}

	categoryService := live_category_service.LiveCategory{Id: id}
	exists, err := categoryService.ExistById()
	if err != nil {
		handler.SendResponse(c, errno.ErrLiveCategoryNotFound, nil)
		return
	}

	if !exists {
		handler.SendResponse(c, errno.ErrLiveCategoryNotFound, nil)
		return
	}

	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)
	group := user.UserInfo.Group
	if ("administrator" != group) {
		category, _ := categoryService.GetOneLiveCategory()
		if category.PlatformId != int64(user.UserInfo.PlatformId) {
			handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
			return
		}
	}
	err = categoryService.Delete()
	if err != nil {
		handler.SendResponse(c, errno.ErrDeleteLiveCategory, nil)
		return
	}

	handler.SendResponse(c, nil, nil)
}
