package validators

import (
	"sync"

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

var (
	once            sync.Once
	decortValidator = validator.New()
)

// getDecortValidator returns singleton instance of DecortValidator.
func getDecortValidator() *validator.Validate {
	once.Do(func() {
		err := registerAllValidators(decortValidator)
		if err != nil {
			panic(err)
		}
	})

	return decortValidator
}

// registerAllValidators registers all custom validators in DecortValidator.
func registerAllValidators(validate *validator.Validate) error {

	err := validate.RegisterValidation("proto", protoValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("apiGroup", apiGroupValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("accessType", accessTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("resTypes", resTypesValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("imageBootType", imageBootTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("imageType", imageTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("imageArchitecture", imageArchitectureValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("accountCUType", accountCUTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("bserviceMode", bserviceModeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("computeTopology", computeTopologyValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("computePolicy", computePolicyValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("computeMode", computeModeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("computeNetType", computeNetTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("computex86NetType", computex86NetTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("computeOrder", computeOrderValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("computeDataDisks", computeDataDisksValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("flipgroupClientType", flipgroupClientTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("massCreateNetType", massCreateTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("lbAlgorithm", lbAlgorithmValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("rgDefNet", rgDefNetValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("rgNetType", rgNetTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("vinsType", vinsTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("sepFieldType", sepFieldTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("hwPath", hwPathValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("networkPlugin", networkPluginValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("networkPlugins", networkPluginsValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("strict_loose", strictLooseValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("interfaceState", interfaceStateValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("interfaceTXModel", interfaceTXModelValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("interfaceIOEventFD", interfaceIOEventFDValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("interfaceEventIDx", interfaceEventIDxValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("workerGroupName", workerGroupNameValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("sortBy", sortByValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("action", actionValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("vmaction", vmActionValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("mtu", mtuValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("computeFeatures", computeFeaturesValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("networkInterfaceNaming", networkInterfaceNamingValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("numaAffinity", numaAffinityValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("kvmx86NetType", kvmx86NetTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("isBool", isBoolTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("isString", isStringTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("url", urlValidartor)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("chipset", chipsetValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("preferredCPU", preferredCPUValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("loaderType", loaderTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("language", languageValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("userProvider", userProviderValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("sepType", sepTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("device", deviceValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("trunkTags", trunkTagsValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("securityGroupDirection", securityGroupDirectionValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("securityGroupEthertype", securityGroupEthertypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("securityGroupProtocol", securityGroupProtocolValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("addressPoolNetTypeValidator", addressPoolNetTypeValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("ipTypes", ipTypesValidator)
	if err != nil {
		return err
	}

	err = validate.RegisterValidation("sepTechStatus", sepTechStatusValidator)
	if err != nil {
		return err
	}

	return nil
}
