package domain

import (
	"errors"
	"fmt"
	"strings"
)

type ResourceType struct {
	resourceType string
}

var (
	ResourceTypeUnd   ResourceType = ResourceType{}        // "und"
	ResourceTypeAudio ResourceType = ResourceType{"audio"} // "audio"
	ResourceTypeImage ResourceType = ResourceType{"image"} // "image"
	ResourceTypePage  ResourceType = ResourceType{"page"}  // "page"
	ResourceTypeText  ResourceType = ResourceType{"text"}  // "text"
	ResourceTypeVideo ResourceType = ResourceType{"video"} // "video"
)

var ErrResourceType error = errors.New("unsupported ResourceType enum")

var stringsResourceTypes = map[string]ResourceType{
	ResourceTypeAudio.resourceType: ResourceTypeAudio,
	ResourceTypeImage.resourceType: ResourceTypeImage,
	ResourceTypePage.resourceType:  ResourceTypePage,
	ResourceTypeText.resourceType:  ResourceTypeText,
	ResourceTypeVideo.resourceType: ResourceTypeVideo,
}

func ParseResourceType(raw string) (ResourceType, error) {
	if rt, ok := stringsResourceTypes[strings.ToLower(raw)]; ok {
		return rt, nil
	}

	return ResourceTypeUnd, fmt.Errorf("%w: got '%s', want %s", ErrResourceType, raw, stringsResourceTypes)
}

func (rt ResourceType) String() string {
	if rt.resourceType != "" {
		return rt.resourceType
	}

	return "und"
}

func (rt ResourceType) GoString() string {
	return "domain.ResourceType(" + rt.String() + ")"
}
