package backend

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

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

	CoinGasService := coin_gas_service.CoinGas{}
	coins_gas, err := CoinGasService.GetAll()
	if err != nil {
		handler.SendResponse(c, errno.InternalServerError, nil)
		return
	}

	handler.SendResponse(c, nil, coins_gas)
}

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

	coin_gas := validate_service.CoinGas{}
	c.ShouldBindJSON(&coin_gas)

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

	CoinGasValidateService := coin_gas_service.CoinGas{
		CoinName: coin_gas.CoinName,
	}

	exist, _ := CoinGasValidateService.ExistCoinGasByName()
	if exist {
		handler.SendResponse(c, errno.ErrExistCoinGas, nil)
		return
	}

	CoinGasService := coin_gas_service.CoinGas{
		CoinName: coin_gas.CoinName,
		Low:      coin_gas.Low,
		Average:  coin_gas.Average,
		High:     coin_gas.High,
	}

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

	handler.SendResponse(c, nil, nil)
}

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

	coin_gas := validate_service.CoinGas{}
	c.ShouldBindJSON(&coin_gas)

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

	CoinGasValidateService := coin_gas_service.CoinGas{
		CoinName: coin_gas.CoinName,
	}

	exist, _ := CoinGasValidateService.ExistCoinGasByName()
	if !exist {
		handler.SendResponse(c, errno.ErrCoinGasNotFound, nil)
		return
	}

	CoinGasService := coin_gas_service.CoinGas{
		CoinName: coin_gas.CoinName,
		Low:      coin_gas.Low,
		Average:  coin_gas.Average,
		High:     coin_gas.High,
	}

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

	handler.SendResponse(c, nil, nil)
}

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

	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
	}

	CoinGasService := coin_gas_service.CoinGas{
		Id: id,
	}
	exists, err := CoinGasService.ExistCoinGasById()
	if err != nil {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}

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

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