package controllers

import (
	"github.com/beego/beego/v2/core/logs"
	beego "github.com/beego/beego/v2/server/web"
	"golang.org/x/net/context"
)

// WorkspaceController is the controller in chrage of solr
type WorkspaceController struct {
	beego.Controller
}

// @Title GetWorkspace
// @Description Return Workspace
// @router / [get]
func (c *WorkspaceController) GetWorkspace() {

	//TODO: Use fullModel api call
	workspaceObj, _, err := OCCatalogAPI.WorkspaceApi.WorkspaceControllerGetWorkspace(context.Background())
	if err != nil {
		logs.Warn(err.Error())
		return
	}

	outputArray := []interface{}{}

	if len(workspaceObj.Data) > 0 {
		dataArr, _, err := OCCatalogAPI.DataApi.DataControllerGetMultipleDataByIDs(context.Background(), workspaceObj.Data)
		if err != nil {
			logs.Warn(err.Error())
		}

		//convert array of strings to interface{}
		ArrInterface := make([]interface{}, len(dataArr))
		for i := range dataArr {
			ArrInterface[i] = dataArr[i]
		}

		outputArray = append(outputArray, ArrInterface...)
	}

	if len(workspaceObj.Computing) > 0 {
		computingArr, _, err := OCCatalogAPI.ComputingApi.ComputingControllerGetMultipleComputingByIDs(context.Background(), workspaceObj.Computing)
		if err != nil {
			logs.Warn(err.Error())
		}

		//convert array of strings to interface{}
		ArrInterface := make([]interface{}, len(computingArr))
		for i := range computingArr {
			ArrInterface[i] = computingArr[i]
		}

		outputArray = append(outputArray, ArrInterface...)
	}

	if len(workspaceObj.Datacenter) > 0 {
		datacenterArr, _, err := OCCatalogAPI.DatacenterApi.DatacenterControllerGetMultipleDatacentersByIDs(context.Background(), workspaceObj.Datacenter)
		if err != nil {
			logs.Warn(err.Error())
		}

		//convert array of strings to interface{}
		ArrInterface := make([]interface{}, len(datacenterArr))
		for i := range datacenterArr {
			ArrInterface[i] = datacenterArr[i]
		}

		outputArray = append(outputArray, ArrInterface...)
	}

	if len(workspaceObj.Storage) > 0 {
		storageArr, _, err := OCCatalogAPI.StorageApi.StorageControllerGetMultipleStoragesByIDs(context.Background(), workspaceObj.Storage)
		if err != nil {
			logs.Warn(err.Error())
		}

		//convert array of strings to interface{}
		ArrInterface := make([]interface{}, len(storageArr))
		for i := range storageArr {
			ArrInterface[i] = storageArr[i]
		}

		outputArray = append(outputArray, ArrInterface...)
	}

	c.Data["list"] = outputArray
	c.TplName = "workspace.tpl"
}
