package backend

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

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

	CoinTickerService := coin_ticker_service.CoinTicker{
		PlatformId: uint8(user.UserInfo.PlatformId),
	}

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

	handler.SendResponse(c, nil, coins_ticker)
}

func AddCoinTicker(c *gin.Context) {
	coin_ticker := validate_service.CoinTicker{}
	c.ShouldBindJSON(&coin_ticker)

	if ok, errors := validate_service.ValidateInputs(coin_ticker); !ok {
		for _, err := range errors {
			handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
			return
		}
	}
	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)

	CoinTickerValidateService := coin_ticker_service.CoinTicker{
		CoinId:     coin_ticker.CoinId,
		PlatformId: uint8(user.UserInfo.PlatformId),
	}
	exist, _ := CoinTickerValidateService.Exist()
	if exist {
		handler.SendResponse(c, errno.ErrExistCoinTicker, nil)
		return
	}

	CoinTickerService := coin_ticker_service.CoinTicker{
		CoinId:     coin_ticker.CoinId,
		CoinName:   coin_ticker.CoinName,
		Ticker:     coin_ticker.Ticker,
		PlatformId: uint8(user.UserInfo.PlatformId),
	}

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

	handler.SendResponse(c, nil, nil)
}

func UpdateCoinTicker(c *gin.Context) {
	coin_ticker := validate_service.CoinTicker{}
	c.ShouldBindJSON(&coin_ticker)

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

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

	CoinTickerValidateService := coin_ticker_service.CoinTicker{
		CoinId:     coin_ticker.CoinId,
		PlatformId: uint8(user.UserInfo.PlatformId),
	}
	exist, _ := CoinTickerValidateService.Exist()
	if !exist {
		handler.SendResponse(c, errno.ErrCoinTickerNotFound, nil)
		return
	}

	CoinTickerService := coin_ticker_service.CoinTicker{
		CoinId:     coin_ticker.CoinId,
		PlatformId: uint8(user.UserInfo.PlatformId),
		Ticker:     coin_ticker.Ticker,
	}

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

	handler.SendResponse(c, nil, nil)
}

func DeleteCoinTicker(c *gin.Context) {
	coin_id := com.StrTo(c.DefaultQuery("coin_id", "0")).MustInt()
	valid := validation.Validation{}
	valid.Min(coin_id, 1, "coin_id").Message("ID必须大于0")
	if valid.HasErrors() {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}
	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)
	CoinTickerService := coin_ticker_service.CoinTicker{
		CoinId:     coin_id,
		PlatformId: uint8(user.UserInfo.PlatformId),
	}
	exists, err := CoinTickerService.Exist()
	if err != nil {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}

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

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