package app

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

func GetTransactionFee(c *gin.Context) {
	platform_id := com.StrTo(c.DefaultQuery("platform_id", "0")).MustInt()
	valid := validation.Validation{}
	valid.Min(platform_id, 1, "platform_id").Message("平台Id必须大于0")

	name := c.DefaultQuery("name", "")
	valid.Required(name, "name").Message("币种不能为空")

	transfer_type := com.StrTo(c.DefaultQuery("transfer_type", "1")).MustUint8()

	if valid.HasErrors() {
		handler.SendResponse(c, valid.Errors[0], nil)
		return
	}

	feeService := fee_service.Fee{
		PlatformId:   platform_id,
		CoinName:     name,
		TransferType: transfer_type,
		PageNum:      util.GetPage(c),
		PageSize:     util.GetLimit(c),
	}

	total_fee, err_fee := feeService.Count()
	if err_fee != nil {
		handler.SendResponse(c, errno.ErrCountCoin, nil)
		return
	}

	if 0 == total_fee {
		handler.SendResponse(c, errno.ErrCoinNotFound, nil)
		return
	}

	fee, err := feeService.Search()
	if err != nil {
		handler.SendResponse(c, errno.ErrCountCoin, nil)
		return
	}

	handler.SendResponse(c, nil, fee)
	return
}

func GetTransactionGas(c *gin.Context) {
	valid := validation.Validation{}

	name := c.DefaultQuery("name", "")
	valid.Required(name, "name").Message("币种不能为空")

	if valid.HasErrors() {
		handler.SendResponse(c, valid.Errors[0], nil)
		return
	}

	gas, err := coin_gas_service.GetTransactionGas(name)

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

	handler.SendResponse(c, nil, gas)
	return
}
