package v1

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

func GetWalletRecommendCategory(c *gin.Context) {
	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
	}

	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
	}

	categoryService := wallet_recommend_category.WalletRecommendCategory{Id: id, PlatformId: platform_id}
	exists, err := categoryService.ExistById()

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

	if (!exists) {
		handler.SendResponse(c, errno.ErrWalletNotFound, nil)
		return
	}

	wallet, err := categoryService.Get()
	if err != nil {
		handler.SendResponse(c, errno.ErrWalletNotFound, nil)
		return
	}

	handler.SendResponse(c, nil, wallet)
}

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

	var wallet_type uint8
	if arg := c.Query("type"); arg != "" {
		wallet_type = com.StrTo(c.Query("type")).MustUint8()
	}

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

	categoryService := wallet_recommend_category.WalletRecommendCategory{
		PlatformId: platform_id,
		WalletType: wallet_type,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

	currencies, err := categoryService.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 AddWalletRecommendCategory(c *gin.Context) {
	category := validate_service.RecommendCategory{}
	c.ShouldBindJSON(&category)

	//方法一
	if ok, errors := validate_service.ValidateInputs(category); !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 category.PlatformId != 0 {
			platform_id = category.PlatformId
		}
	}

	categoryService := wallet_recommend_category.WalletRecommendCategory{
		Name:       category.Name,
		PlatformId: platform_id,
		Status:     category.Status,
		Sort:       category.Sort,
		WalletType: category.WalletType,
	}

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

	handler.SendResponse(c, nil, nil)
}

func EditWalletRecommendCategory(c *gin.Context) {
	category := validate_service.EditRecommendCategory{}
	c.ShouldBindJSON(&category)

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

	categoryService := wallet_recommend_category.WalletRecommendCategory{
		Id:         category.Id,
		Name:       category.Name,
		Status:     category.Status,
		Sort:       category.Sort,
		WalletType: category.WalletType,
	}

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

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

	handler.SendResponse(c, nil, nil)
}

func DeleteWalletRecommendCategory(c *gin.Context) {
	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
	}

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

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

	handler.SendResponse(c, nil, nil)
}
