package v1

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

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

	currencyService := supported_currency_service.SupportedCurrency{
		ID:         id,
		PlatformId: platform_id,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

	currency, err := currencyService.GetAll()
	if err != nil {
		handler.SendResponse(c, errno.ErrSupportedCurrencyNotFound, nil)
		return
	}

	handler.SendResponse(c, nil, currency)
}

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

	currencyService := supported_currency_service.SupportedCurrency{
		PlatformId: platform_id,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

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

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

	handler.SendResponse(c, nil, data)
}

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

	currency := validate_service.SupportedCurrency{}
	c.ShouldBindJSON(&currency)

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

	currencyService := supported_currency_service.SupportedCurrency{
		PlatformId: currency.PlatformId,
		CurrencyId: currency.CurrencyId,
		Sort:       currency.Sort,
	}

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

	handler.SendResponse(c, nil, nil)
}

func EditSupportedCurrency(c *gin.Context) {
	currency := validate_service.EditSupportedCurrency{}
	c.ShouldBindJSON(&currency)

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

	currencyService := supported_currency_service.SupportedCurrency{
		ID:         currency.Id,
		PlatformId: currency.PlatformId,
		CurrencyId: currency.CurrencyId,
		Sort:       currency.Sort,
	}

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

	if err := currencyService.Edit(); err != nil {
		handler.SendResponse(c, errno.ErrUpdateSupportedCurrency, nil)
		return
	}

	handler.SendResponse(c, nil, nil)
}

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

	currencyService := supported_currency_service.SupportedCurrency{ID: id}
	exists, err := currencyService.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) {
		currency, _ := currencyService.Get()
		if currency.PlatformId != auth.PlatformId {
			handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
			return
		}
	}

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

	handler.SendResponse(c, nil, nil)
}

func GetCurrencies(c *gin.Context) {
	currencies, err := supported_currency_service.GetCurrencies()
	if err != nil {
		handler.SendResponse(c, errno.InternalServerError, nil)
		return
	}

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

	handler.SendResponse(c, nil, data)
}
