package backend

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

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

	var platform_id int
	platform_id = user.UserInfo.PlatformId
	group := user.UserInfo.Group

	if ("administrator" == group) {
		if arg := c.Query("platform_id"); arg != "" {
			platform_id = com.StrTo(c.DefaultQuery("platform_id", strconv.Itoa(platform_id))).MustInt()
		}
	}
	status := com.StrTo(c.DefaultQuery("status", "1")).MustUint8()

	bannerService := live_banner_service.LiveBanner{
		Status:     status,
		PlatformId: platform_id,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

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

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

	handler.SendResponse(c, nil, data)
}

func GetLiveBanner(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, errno.ErrValidation, nil)
		return
	}

	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)
	group := user.UserInfo.Group
	var platform_id int
	platform_id = id
	if ("administrator" != group && id != user.UserInfo.PlatformId) {
		handler.SendResponse(c, errno.ErrUserTokenIncorrect, nil)
		return
	}

	bannerService := live_banner_service.LiveBanner{Id: platform_id}
	exists, err := bannerService.ExistById()

	if err != nil {
		handler.SendResponse(c, errno.ErrLiveBannerNotFound, nil)
		return
	}

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

	banner, err := bannerService.Get()
	if err != nil {
		handler.SendResponse(c, errno.ErrLiveBannerNotFound, nil)
		return
	}

	handler.SendResponse(c, nil, banner)
}

func AddLiveBanner(c *gin.Context) {
	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)
	group := user.UserInfo.Group
	if ("administrator" != group) {
		handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
		return
	}

	banner := validate_service.LiveBanner{}
	c.ShouldBindJSON(&banner)

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

	bannerService := live_banner_service.LiveBanner{
		ImageUrl:   banner.ImageUrl,
		TargetLink: banner.TargetLink,
		Title:      banner.Title,
		PlatformId: banner.PlatformId,
		Status:     banner.Status,
		Sort:       banner.Sort,
	}

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

	handler.SendResponse(c, nil, nil)
}

func EditLiveBanner(c *gin.Context) {
	banner := validate_service.EditLiveBanner{}
	c.ShouldBindJSON(&banner)

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

	bannerService := live_banner_service.LiveBanner{
		Id:         banner.Id,
		ImageUrl:   banner.ImageUrl,
		TargetLink: banner.TargetLink,
		Title:      banner.Title,
		PlatformId: banner.PlatformId,
		Status:     banner.Status,
		Sort:       banner.Sort,
	}

	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)
	group := user.UserInfo.Group
	if ("administrator" != group && user.UserInfo.PlatformId != banner.PlatformId) {
		handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
		return
	}

	exists, err := bannerService.ExistById()
	if err != nil || !exists {
		handler.SendResponse(c, errno.ErrLiveBannerNotFound, nil)
		return
	}

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

	handler.SendResponse(c, nil, nil)
}

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

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

	bannerService := live_banner_service.LiveBanner{Id: id}

	exists, err := bannerService.ExistById()
	if err != nil || !exists {
		handler.SendResponse(c, errno.ErrLiveBannerNotFound, nil)
		return
	}

	banner, err := bannerService.Get()
	if err != nil {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}

	if ("administrator" != group && user.UserInfo.PlatformId != banner.PlatformId) {
		handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
		return
	}

	err = bannerService.Delete()
	if err != nil {
		handler.SendResponse(c, errno.ErrDeleteLiveBanner, nil)
		return
	}

	handler.SendResponse(c, nil, nil)
}
