package resources

import (
	"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
	"cloud.o-forge.io/core/oc-lib/models/utils"
)

// AbstractResource is the struct containing all of the attributes commons to all ressources

// Resource is the interface to be implemented by all classes inheriting from Resource to have the same behavior

// http://www.inanzzz.com/index.php/post/wqbs/a-basic-usage-of-int-and-string-enum-types-in-golang
type ResourceInterface interface {
	utils.DBObject
	Trim() *resource_model.AbstractResource
	SetResourceModel(model *resource_model.ResourceModel)
}

type ResourceSet struct {
	Datas       []string `bson:"datas,omitempty" json:"datas,omitempty"`
	Storages    []string `bson:"storages,omitempty" json:"storages,omitempty"`
	Processings []string `bson:"processings,omitempty" json:"processings,omitempty"`
	Computes    []string `bson:"computes,omitempty" json:"computes,omitempty"`
	Workflows   []string `bson:"workflows,omitempty" json:"workflows,omitempty"`

	DataResources       []*DataResource       `bson:"-" json:"data_resources,omitempty"`
	StorageResources    []*StorageResource    `bson:"-" json:"storage_resources,omitempty"`
	ProcessingResources []*ProcessingResource `bson:"-" json:"processing_resources,omitempty"`
	ComputeResources    []*ComputeResource    `bson:"-" json:"compute_resources,omitempty"`
	WorkflowResources   []*WorkflowResource   `bson:"-" json:"workflow_resources,omitempty"`
}

func (r *ResourceSet) Clear() {
	r.DataResources = nil
	r.StorageResources = nil
	r.ProcessingResources = nil
	r.ComputeResources = nil
	r.WorkflowResources = nil
}

func (r *ResourceSet) Fill(peerID string, groups []string) {
	for k, v := range map[utils.DBObject][]string{
		(&DataResource{}):       r.Datas,
		(&ComputeResource{}):    r.Computes,
		(&StorageResource{}):    r.Storages,
		(&ProcessingResource{}): r.Processings,
		(&WorkflowResource{}):   r.Workflows,
	} {
		for _, id := range v {
			d, _, e := k.GetAccessor(peerID, groups, nil).LoadOne(id)
			if e == nil {
				switch k.(type) {
				case *DataResource:
					r.DataResources = append(r.DataResources, d.(*DataResource))
				case *ComputeResource:
					r.ComputeResources = append(r.ComputeResources, d.(*ComputeResource))
				case *StorageResource:
					r.StorageResources = append(r.StorageResources, d.(*StorageResource))
				case *ProcessingResource:
					r.ProcessingResources = append(r.ProcessingResources, d.(*ProcessingResource))
				case *WorkflowResource:
					r.WorkflowResources = append(r.WorkflowResources, d.(*WorkflowResource))
				}
			}
		}
	}
}

type ItemResource struct {
	Data       *DataResource       `bson:"data,omitempty" json:"data,omitempty"`
	Processing *ProcessingResource `bson:"processing,omitempty" json:"processing,omitempty"`
	Storage    *StorageResource    `bson:"storage,omitempty" json:"storage,omitempty"`
	Compute    *ComputeResource    `bson:"compute,omitempty" json:"compute,omitempty"`
	Workflow   *WorkflowResource   `bson:"workflow,omitempty" json:"workflow,omitempty"`
}

func (i *ItemResource) GetAbstractRessource() *resource_model.AbstractResource {

	if i.Data != nil {
		return &i.Data.AbstractResource
	}
	if i.Processing != nil {
		return &i.Processing.AbstractResource
	}
	if i.Storage != nil {
		return &i.Storage.AbstractResource
	}
	if i.Compute != nil {
		return &i.Compute.AbstractResource
	}
	if i.Workflow != nil {
		return &i.Workflow.AbstractResource
	}
	return nil
}
