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"
	"strconv"
	"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
		}
	}

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

	for _, id := range strings.Split(chain.CoinIds, ",") {
		if id == "" {
			continue
		}
		coin_id, err := strconv.Atoi(id)
		if (nil != err) {
			continue
		}
		coinService := coin_service.Coin{
			Id: coin_id,
		}
		exists, err := coinService.ExistById()
		if err != nil || !exists {
			continue
		}

		supportedChainValidate := supported_chain_service.SupportedChain{
			CoinId:     coin_id,
			PlatformId: platform_id,
		}
		total, err := supportedChainValidate.Count()
		if err != nil || total > 0 {
			continue
		}

		supportedChainValidate.Add()
	}

	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)
}
