package v1

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"bwallet/service/auth_service"
	"bwallet/service/coin_service"
	"bwallet/service/wallet_coin_relation_service"
	"bwallet/validate_service"
	"github.com/Unknwon/com"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

func GetWalletCoinRelationCoinRelations(c *gin.Context) {

	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
	}
	name := ""
	if arg := c.Query("name"); arg != "" {
		name = c.Query("name")
	}

	platform := ""
	if arg := c.Query("platform"); arg != "" {
		platform = c.Query("platform")
	}

	chain := ""
	if arg := c.Query("chain"); arg != "" {
		chain = c.Query("chain")
	}

	walletCoinRelateionService := wallet_coin_relation_service.WalletCoinRelation{
		PlatformId: platform_id,
		CoinCondition: struct {
			Name     string
			Chain    string
			Platform string
		}{Name: name, Chain: chain, Platform: platform},
		PageNum:  util.GetPage(c),
		PageSize: util.GetLimit(c),
	}

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

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

	handler.SendResponse(c, nil, data)
}

/**
 * 添加币种钱包对应关系
 * @param
 * @return
 */
func AddWalletCoinRelationCoinRelation(c *gin.Context) {
	token := c.Request.Header.Get("Token")
	authService := auth_service.Auth{Token: token}
	auth, _ := authService.GetUserInfo()
	group := auth.Group

	if ("administrator" != group) {
		handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
		return
	}

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

	for _, id := range strings.Split(coin_relation.CoinId, ",") {
		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
		}

		relationService := wallet_coin_relation_service.WalletCoinRelation{
			CoinId:     coin_id,
			PlatformId: coin_relation.PlatformId,
		}
		total, err := relationService.Count()
		if err != nil || total > 0 {
			continue
		}

		relationService.Add()
	}

	handler.SendResponse(c, nil, nil)
	/*
		coinService := coin_service.Coin{
			Id: coin_relation.CoinId,
		}
		exists, err := coinService.ExistById()
		if err != nil {
			handler.SendResponse(c, errno.ErrCoinNotFound, nil)
			return
		}
		if !exists {
			handler.SendResponse(c, errno.ErrCoinNotFound, nil)
			return
		}

		relationService := wallet_coin_relation_service.WalletCoinRelation{
			CoinId:     coin_relation.CoinId,
			PlatformId: coin_relation.PlatformId,
			PageNum:    util.GetPage(c),
			PageSize:   util.GetLimit(c),
		}

		coins, err := relationService.GetAll()
		if err != nil {
			handler.SendResponse(c, errno.InternalServerError, nil)
			return
		}
		if 0 != len(coins) {
			handler.SendResponse(c, errno.ErrExistCoin, nil)
			return
		}

		if err := relationService.Add(); err != nil {
			handler.SendResponse(c, errno.ErrAddCoin, nil)
			return
		}
		handler.SendResponse(c, nil, nil)
	*/
}

/**
 * 删除币种钱包对应关系
 * @param
 * @return
 */
func DeleteWalletCoinRelationCoinRelation(c *gin.Context) {
	var platform_id, coin_id int
	if arg := c.Query("platform_id"); arg != "" {
		platform_id = com.StrTo(c.Query("platform_id")).MustInt()
	}
	if arg := c.Query("coin_id"); arg != "" {
		coin_id = com.StrTo(c.Query("coin_id")).MustInt()
	}

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

	if (0 == platform_id || 0 == coin_id) {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}

	relationService := wallet_coin_relation_service.WalletCoinRelation{
		PlatformId: platform_id,
		CoinId:     coin_id,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}
	coins, err := relationService.GetAll()
	if err != nil {
		handler.SendResponse(c, errno.InternalServerError, nil)
		return
	}
	if 0 == len(coins) {
		handler.SendResponse(c, errno.ErrNotExistCoin, nil)
		return
	}

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

	handler.SendResponse(c, nil, nil)
}
