package v1

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"bwallet/service/auth_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")
	authService := auth_service.Auth{Token: token}
	auth, _ := authService.GetUserInfo()
	group := auth.Group
	var platform_id int
	if ("administrator" == group) {
		platform_id = com.StrTo(c.DefaultQuery("platform_id", "1")).MustInt()
	} else {
		platform_id = auth.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) {
	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
		}
	}
	return
	//自定义验证
	supportedCurrencyValidate := supported_chain_service.SupportedChain{
		PlatformId: chain.PlatformId,
		CoinId:     11111,
	}
	total, err := supportedCurrencyValidate.Count()
	if err != nil {
		handler.SendResponse(c, errno.ErrCountSupportedChain, nil)
		return
	}
	if (total > 0) {
		handler.SendResponse(c, errno.ErrExistSupportedChain, nil)
		return
	}

	token := c.Request.Header.Get("Token")
	authService := auth_service.Auth{Token: token}
	auth, _ := authService.GetUserInfo()
	group := auth.Group

	var platform_id int
	platform_id = auth.PlatformId
	if ("administrator" == group) {
		if chain.PlatformId != 0 {
			platform_id = chain.PlatformId
		}
	}

	chainService := supported_chain_service.SupportedChain{
		PlatformId: platform_id,
		CoinId:     11111,
	}

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

	handler.SendResponse(c, nil, nil)
}

func DeleteSupportedChain(c *gin.Context) {

	valid := validation.Validation{}
	id := com.StrTo(c.Param("id")).MustInt()
	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
	}

	token := c.Request.Header.Get("Token")
	authService := auth_service.Auth{Token: token}
	auth, _ := authService.GetUserInfo()
	group := auth.Group
	if ("administrator" != group) {
		chain, _ := chainService.Get()
		if chain.PlatformId != auth.PlatformId {
			handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
			return
		}
	}

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

	handler.SendResponse(c, nil, nil)
}
