package v1

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

func GetCoin(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("ID必须大于0")

	if valid.HasErrors() {
		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
	var platform_id int
	if ("administrator" == group) {
		platform_id = com.StrTo(c.DefaultQuery("platform_id", "1")).MustInt()
	} else {
		platform_id = auth.PlatformId
	}
	coinService := coin_service.Coin{
		Id:         id,
		PlatformId: platform_id,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

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

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

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

	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 arg := c.Query("platform_id"); arg != "" {
			platform_id = com.StrTo(c.Query("platform_id")).MustInt()
		}
	}

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

	coinService := coin_service.Coin{
		Name:       name,
		Chain:      chain,
		Platform:   platform,
		PlatformId: platform_id,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

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

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

	handler.SendResponse(c, nil, data)
}

func AddCoin(c *gin.Context) {
	coin := validate_service.Coin{}
	c.ShouldBindJSON(&coin)
	//方法一
	if ok, errors := validate_service.ValidateInputs(coin); !ok {
		for _, err := range errors {
			handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
			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 coin.PlatformId != 0 {
			platform_id = coin.PlatformId
		}
	}

	coinService := coin_service.Coin{
		Sid:            coin.Sid,
		Name:           coin.Name,
		Nickname:       coin.Nickname,
		Icon:           coin.Icon,
		Introduce:      coin.Introduce,
		Official:       coin.Official,
		Paper:          coin.Paper,
		Platform:       coin.Platform,
		Chain:          coin.Chain,
		Release:        coin.Release,
		AreaSearch:     coin.AreaSearch,
		PublishCount:   coin.PublishCount,
		CirculateCount: coin.CirculateCount,
		Decimals:       coin.Decimals,
		Address:        coin.Address,
		Treaty:         coin.Treaty,
		PlatformId:     platform_id,
	}

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

func EditCoin(c *gin.Context) {
	coin := validate_service.EditCoin{}
	c.ShouldBindJSON(&coin)

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

	coinService := coin_service.Coin{
		Id:             coin.Id,
		Sid:            coin.Sid,
		Name:           coin.Name,
		Nickname:       coin.Nickname,
		Icon:           coin.Icon,
		Introduce:      coin.Introduce,
		Official:       coin.Official,
		Paper:          coin.Paper,
		Platform:       coin.Platform,
		Chain:          coin.Chain,
		Release:        coin.Release,
		AreaSearch:     coin.AreaSearch,
		PublishCount:   coin.PublishCount,
		CirculateCount: coin.CirculateCount,
		Decimals:       coin.Decimals,
		Address:        coin.Address,
		PlatformId:     coin.PlatformId,
		Treaty:         coin.Treaty,
	}

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

	token := c.Request.Header.Get("Token")
	authService := auth_service.Auth{Token: token}
	auth, _ := authService.GetUserInfo()
	group := auth.Group
	if ("administrator" != group) {
		coin_model := coin_service.Coin{Id: coin.Id}
		coin, _ := coin_model.Get()
		if coin.PlatformId != auth.PlatformId {
			handler.SendResponse(c, errno.ErrUserTokenIncorrect, nil)
			return
		}
	}
	if err := coinService.Edit(); err != nil {
		handler.SendResponse(c, errno.ErrUpdateCoin, nil)
		return
	}

	handler.SendResponse(c, nil, nil)
}

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

	coinService := coin_service.Coin{Id: id}
	exists, err := coinService.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) {
		coin, _ := coinService.Get()
		if coin.PlatformId != auth.PlatformId {
			handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
			return
		}
	}

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

	handler.SendResponse(c, nil, nil)
}

/*
func GetCoinsPlatform(c *gin.Context) {
	appG := app.Gin{c}
	coinService := coin_service.Coin{}
	coins, err := coinService.GetCoinsPlatform()
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_GET_COIN_FAIL, nil)
		return
	}

	data := make(map[string]interface{})
	data["lists"] = coins

	appG.Response(http.StatusOK, e.SUCCESS, data)
}
*/

func GetPrimaryChains(c *gin.Context) {
	data := make(map[string]interface{})
	data["items"] = [] string{"BTC", "ETH", "BTY", "DCR"}

	handler.SendResponse(c, nil, data)
}

func GetTypes(c *gin.Context) {
	data := make(map[string]interface{})
	data["items"] = [] string{"coins", "er c20/token"}

	handler.SendResponse(c, nil, data)
}
