package router

import (
	"strings"

	"galaxy/util"

	"github.com/go-playground/validator/v10"
)

var notBlankStringValidator validator.Func = func(fl validator.FieldLevel) bool {
	s, ok := fl.Field().Interface().(string)
	if ok {
		if len(strings.TrimSpace(s)) == 0 {
			return false
		}
	}
	return true
}

var entityNameStringValidator validator.Func = func(fl validator.FieldLevel) bool {
	s, ok := fl.Field().Interface().(string)
	if ok {
		if _, ok := util.ValidateTypeName(s); !ok {
			return false
		}
	}
	return true
}

var productionTypeStringValidator validator.Func = func(fl validator.FieldLevel) bool {
	v, ok := fl.Field().Interface().(string)
	if ok {
		f := fl.Parent().FieldByName(fl.Param())
		if f.String() == "SHIP" || f.String() == "SCIENCE" {
			if _, ok := util.ValidateTypeName(v); !ok {
				return false
			}
		}
	}
	return true
}

var armamentWithWeaponsValidator validator.Func = func(fl validator.FieldLevel) bool {
	var v, compareTo float64

	f := fl.Parent().FieldByName(fl.Param())

	if f.CanFloat() {
		compareTo = f.Float()
	} else if f.CanInt() {
		compareTo = float64(f.Int())
	} else {
		return false
	}

	if fl.Field().CanFloat() {
		v = fl.Field().Float()
	} else if fl.Field().CanInt() {
		v = float64(fl.Field().Int())
	} else {
		return false
	}

	return (v == 0 && compareTo == 0) || (v >= 1 && compareTo >= 1)
}
