package backend

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

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

	token := c.Request.Header.Get("Token")
	user, _ := util.ParseToken(token)
	group := user.UserInfo.Group
	var platform_id int
	if ("administrator" == group) {
		platform_id = com.StrTo(c.DefaultQuery("platform_id", "1")).MustInt()
	} else {
		platform_id = user.UserInfo.PlatformId
	}

	if valid.HasErrors() {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}

	chainService := supported_chain_service.SupportedChain{
		PlatformId: platform_id,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

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

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

	handler.SendResponse(c, nil, data)
}

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

	chain := validate_service.SupportedChain{}
	c.ShouldBindJSON(&chain)

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

	coinService := coin_service.Coin{
		Name:  chain.CoinName,
		Chain: strings.ToUpper(chain.CoinName),
	}
	coin, err := coinService.Find()

	if err != nil {
		handler.SendResponse(c, errno.ErrCoinNotFound, nil)
		return
	}
	if "" == coin.Name {
		coinService := coin_service.Coin{
			Name:  chain.CoinName,
			Chain: "BTY",
		}
		coin, err = coinService.Find()
		if err != nil {
			handler.SendResponse(c, errno.ErrCoinNotFound, nil)
			return
		}
	}
	if "" == coin.Name {
		handler.SendResponse(c, errno.ErrCoinNotFound, nil)
		return
	}

	chainService := supported_chain_service.SupportedChain{
		PlatformId: chain.PlatformId,
		CoinId:     coin.ID,
	}

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

	if 0 != total {
		handler.SendResponse(c, errno.ErrExistCoin, nil)
		return
	}

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

	handler.SendResponse(c, nil, nil)
}

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

	chainService := supported_chain_service.SupportedChain{ID: id}
	exists, err := chainService.ExistById()
	if err != nil {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}

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

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

	handler.SendResponse(c, nil, nil)
}
