package backend

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

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

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

	chargeService := live_charge_service.LiveCharge{
		PlatformId: platform_id,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

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

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

	handler.SendResponse(c, nil, data)
}

func AddLiveCharge(c *gin.Context) {
	charge := validate_service.LiveCharge{}
	c.ShouldBindJSON(&charge)

	//方法一
	if ok, errors := validate_service.ValidateInputs(charge); !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 int64
	platform_id = int64(user.UserInfo.PlatformId)

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

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

	chargeService := live_charge_service.LiveCharge{
		Unit:       charge.Unit,
		UnitPrice:  charge.UnitPrice,
		PlatformId: platform_id,
	}

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

	handler.SendResponse(c, nil, nil)
}

func EditLiveCharge(c *gin.Context) {
	charge := validate_service.EditLiveCharge{}
	c.ShouldBindJSON(&charge)

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

	chargeService := live_charge_service.LiveCharge{
		Id: charge.Id,
	}
	exists, err := chargeService.ExistById()
	if err != nil || !exists {
		handler.SendResponse(c, errno.ErrLiveChargeNotFound, nil)
		return
	}

	temp, err := chargeService.GetOneLiveInfo()
	if err != nil {
		handler.SendResponse(c, errno.ErrLiveChargeNotFound, nil)
		return
	}

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

	chargeService.UnitPrice = charge.UnitPrice
	chargeService.Unit = charge.Unit

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

	handler.SendResponse(c, nil, nil)
}
