package image

import (
	"encoding/json"
	"strconv"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/image"
)

func flattenHistory(history []image.History) []map[string]interface{} {
	temp := make([]map[string]interface{}, 0)
	for _, item := range history {
		t := map[string]interface{}{
			"id":        item.ID,
			"guid":      item.GUID,
			"timestamp": item.Timestamp,
		}

		temp = append(temp, t)
	}
	return temp
}

func flattenImage(d *schema.ResourceData, img *image.RecordImage) {
	cdPresentedTo, _ := json.Marshal(img.CdPresentedTo)

	d.Set("unc_path", img.UNCPath)
	d.Set("account_id", img.AccountID)
	d.Set("acl", FlattenACL(img.ACL))
	d.Set("architecture", img.Architecture)
	d.Set("boot_type", img.BootType)
	d.Set("bootable", img.Bootable)
	d.Set("compute_ci_id", img.ComputeCIID)
	d.Set("cd_presented_to", string(cdPresentedTo))
	d.Set("deleted_time", img.DeletedTime)
	d.Set("desc", img.Description)
	d.Set("drivers", img.Drivers)
	d.Set("enabled", img.Enabled)
	d.Set("gid", img.GID)
	d.Set("guid", img.GUID)
	d.Set("history", flattenHistory(img.History))
	d.Set("hot_resize", img.HotResize)
	d.Set("image_id", img.ID)
	d.Set("independent", img.Independent)
	d.Set("last_modified", img.LastModified)
	d.Set("link_to", img.LinkTo)
	d.Set("links_to", img.LinksTo)
	d.Set("milestones", img.Milestones)
	d.Set("image_name", img.Name)
	d.Set("network_interface_naming", img.NetworkInterfaceNaming)
	d.Set("password", img.Password)
	d.Set("pool_name", img.Pool)
	d.Set("provider_name", img.ProviderName)
	d.Set("purge_attempts", img.PurgeAttempts)
	d.Set("present_to", img.PresentTo)
	d.Set("res_id", img.ResID)
	d.Set("rescuecd", img.RescueCD)
	d.Set("sep_id", img.SepID)
	d.Set("shared_with", img.SharedWith)
	d.Set("size", img.Size)
	d.Set("snapshot_id", img.SnapshotID)
	d.Set("status", img.Status)
	d.Set("storage_policy_id", img.StoragePolicyID)
	d.Set("tech_status", img.TechStatus)
	d.Set("to_clean", img.ToClean)
	d.Set("type", img.Type)
	d.Set("username", img.Username)
	d.Set("version", img.Version)
}

func flattenImageList(il *image.ListImages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	for _, img := range il.Data {
		temp := map[string]interface{}{
			"account_id":               img.AccountID,
			"architecture":             img.Architecture,
			"boot_type":                img.BootType,
			"bootable":                 img.Bootable,
			"cdrom":                    img.CDROM,
			"desc":                     img.Description,
			"drivers":                  img.Drivers,
			"hot_resize":               img.HotResize,
			"image_id":                 img.ID,
			"link_to":                  img.LinkTo,
			"image_name":               img.Name,
			"network_interface_naming": img.NetworkInterfaceNaming,
			"pool_name":                img.Pool,
			"sep_id":                   img.SepID,
			"size":                     img.Size,
			"status":                   img.Status,
			"storage_policy_id":        img.StoragePolicyID,
			"type":                     img.Type,
			"username":                 img.Username,
			"virtual":                  img.Virtual,
		}
		res = append(res, temp)
	}
	return res
}

func FlattenACL(acl interface{}) string {
	switch d := acl.(type) {
	case string:
		return d
	case int:
		return strconv.Itoa(d)
	case int64:
		return strconv.FormatInt(d, 10)
	case float64:
		return strconv.FormatInt(int64(d), 10)
	default:
		return ""
	}
}
