package views

import (
	OCCatalog_cli "oc-search/api-client/oc-catalog"

	"github.com/beego/beego/v2/core/logs"

	beego "github.com/beego/beego/v2/server/web"
)

func init() {
	beego.AddFuncMap("getID", getID)
	beego.AddFuncMap("getShortDescription", getShortDescription)
	beego.AddFuncMap("getName", getName)
	beego.AddFuncMap("getType", getType)
	beego.AddFuncMap("getLogo", getLogo)
	beego.AddFuncMap("getRtype", getRtype)
	beego.AddFuncMap("searchResourceToList", searchResourceToList)
}

func getRtype(in interface{}) (out string) {
	switch in.(type) {
	case OCCatalog_cli.ModelsDataModel:
		out = "data"
	case OCCatalog_cli.ModelsComputingModel:
		out = "computing"
	case OCCatalog_cli.ModelsDatacenterModel:
		out = "datacenter"
	case OCCatalog_cli.ModelsStorageModel:
		out = "storage"
	}
	return
}

func getID(in interface{}) (out string) {
	switch in.(type) {
	case OCCatalog_cli.ModelsDataModel:
		out = in.(OCCatalog_cli.ModelsDataModel).ID
	case OCCatalog_cli.ModelsComputingModel:
		out = in.(OCCatalog_cli.ModelsComputingModel).ID
	case OCCatalog_cli.ModelsDatacenterModel:
		out = in.(OCCatalog_cli.ModelsDatacenterModel).ID
	case OCCatalog_cli.ModelsStorageModel:
		out = in.(OCCatalog_cli.ModelsStorageModel).ID
	}
	return
}

func getLogo(in interface{}) (out string) {
	switch in.(type) {
	case OCCatalog_cli.ModelsDataModel:
		out = in.(OCCatalog_cli.ModelsDataModel).Logo
	case OCCatalog_cli.ModelsComputingModel:
		out = in.(OCCatalog_cli.ModelsComputingModel).Logo
	case OCCatalog_cli.ModelsDatacenterModel:
		out = in.(OCCatalog_cli.ModelsDatacenterModel).Logo
	case OCCatalog_cli.ModelsStorageModel:
		out = in.(OCCatalog_cli.ModelsStorageModel).Logo
	default:
		logs.Warn("Unable to parse Logo")
	}

	//TODO: Find a hybrid aproach for images as src and base64

	return
}

func getName(in interface{}) (out string) {
	switch in.(type) {
	case OCCatalog_cli.ModelsDataModel:
		out = in.(OCCatalog_cli.ModelsDataModel).Name
	case OCCatalog_cli.ModelsComputingModel:
		out = in.(OCCatalog_cli.ModelsComputingModel).Name
	case OCCatalog_cli.ModelsDatacenterModel:
		out = in.(OCCatalog_cli.ModelsDatacenterModel).Name
	case OCCatalog_cli.ModelsStorageModel:
		out = in.(OCCatalog_cli.ModelsStorageModel).Name
	}
	return
}

func getShortDescription(in interface{}) (out string) {
	switch in.(type) {
	case OCCatalog_cli.ModelsDataModel:
		out = in.(OCCatalog_cli.ModelsDataModel).ShortDescription
	case OCCatalog_cli.ModelsComputingModel:
		out = in.(OCCatalog_cli.ModelsComputingModel).ShortDescription
	case OCCatalog_cli.ModelsDatacenterModel:
		out = in.(OCCatalog_cli.ModelsDatacenterModel).ShortDescription
	case OCCatalog_cli.ModelsStorageModel:
		out = in.(OCCatalog_cli.ModelsStorageModel).ShortDescription
	}
	return
}

func searchResourceToList(in interface{}) (out []interface{}) {
	out = []interface{}{}

	obj := in.(OCCatalog_cli.ModelsSearchResult)

	// Data
	if len(obj.Data) > 0 {
		ArrInterface := make([]interface{}, len(obj.Data))
		for i := range obj.Data {
			ArrInterface[i] = obj.Data[i]
		}
		out = append(out, ArrInterface...)
	}

	// Computing
	if len(obj.Computing) > 0 {
		ArrInterface := make([]interface{}, len(obj.Computing))
		for i := range obj.Computing {
			ArrInterface[i] = obj.Computing[i]
		}
		out = append(out, ArrInterface...)
	}

	// Storage
	if len(obj.Storage) > 0 {
		ArrInterface := make([]interface{}, len(obj.Storage))
		for i := range obj.Storage {
			ArrInterface[i] = obj.Storage[i]
		}
		out = append(out, ArrInterface...)
	}

	// Datacenter
	if len(obj.Datacenter) > 0 {
		ArrInterface := make([]interface{}, len(obj.Datacenter))
		for i := range obj.Datacenter {
			ArrInterface[i] = obj.Datacenter[i]
		}
		out = append(out, ArrInterface...)
	}

	return

}

func getType(in interface{}) (out string) {
	//FIXME: We should use types
	switch in.(type) {
	case OCCatalog_cli.ModelsDataModel:
		out = "data"
	case OCCatalog_cli.ModelsComputingModel:
		out = "computing"
	case OCCatalog_cli.ModelsDatacenterModel:
		out = "datacenter"
	case OCCatalog_cli.ModelsStorageModel:
		out = "storage"
	}
	return
}
