package logicalports

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/sdn/logicalports"
)

func flattenLogicalPortResource(d *schema.ResourceData, logicalPort *logicalports.LogicalPort) {
	d.Set("id", logicalPort.ID)
	d.Set("access_group_id", logicalPort.AccessGroupID)
	d.Set("access_group_name", logicalPort.AccessGroupName)
	d.Set("adapter_mac", logicalPort.AdapterMAC)
	d.Set("address_detection", logicalPort.AddressDetection)
	d.Set("description", logicalPort.Description)
	d.Set("display_name", logicalPort.DisplayName)
	d.Set("enabled", logicalPort.Enabled)
	d.Set("hypervisor", logicalPort.Hypervisor)
	d.Set("hypervisor_display_name", logicalPort.HypervisorDisplayName)
	d.Set("live_migration_target_hv", logicalPort.LiveMigrationTargetHV)
	d.Set("status", flattenStatus(logicalPort.Status))
	d.Set("unique_identifier", logicalPort.UniqueIdentifier)
	d.Set("version_id", logicalPort.VersionID)
	d.Set("bindings", flattenBindings(logicalPort.Bindings))
	d.Set("created_at", logicalPort.CreatedAt)
}

func flattenLogicalPortList(lpl *logicalports.LogicalPortsList) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(lpl.Ports))
	for _, v := range lpl.Ports {
		temp := map[string]interface{}{
			"id":                       v.ID,
			"access_group_id":          v.AccessGroupID,
			"access_group_name":        v.AccessGroupName,
			"adapter_mac":              v.AdapterMAC,
			"address_detection":        v.AddressDetection,
			"description":              v.Description,
			"display_name":             v.DisplayName,
			"enabled":                  v.Enabled,
			"hypervisor":               v.Hypervisor,
			"hypervisor_display_name":  v.HypervisorDisplayName,
			"live_migration_target_hv": v.LiveMigrationTargetHV,
			"status":                   flattenStatus(v.Status),
			"unique_identifier":        v.UniqueIdentifier,
			"version_id":               v.VersionID,
			"bindings":                 flattenBindings(v.Bindings),
			"created_at":               v.CreatedAt,
			"updated_at":               v.UpdatedAt,
		}
		res = append(res, temp)
	}
	return res
}

func flattenLogicalPort(d *schema.ResourceData, logicalPort *logicalports.LogicalPort) {
	d.Set("id", logicalPort.ID)
	d.Set("access_group_id", logicalPort.AccessGroupID)
	d.Set("access_group_name", logicalPort.AccessGroupName)
	d.Set("adapter_mac", logicalPort.AdapterMAC)
	d.Set("address_detection", logicalPort.AddressDetection)
	d.Set("description", logicalPort.Description)
	d.Set("display_name", logicalPort.DisplayName)
	d.Set("enabled", logicalPort.Enabled)
	d.Set("hypervisor", logicalPort.Hypervisor)
	d.Set("hypervisor_display_name", logicalPort.HypervisorDisplayName)
	d.Set("live_migration_target_hv", logicalPort.LiveMigrationTargetHV)
	d.Set("status", flattenStatus(logicalPort.Status))
	d.Set("unique_identifier", logicalPort.UniqueIdentifier)
	d.Set("version_id", logicalPort.VersionID)
	d.Set("bindings", flattenBindings(logicalPort.Bindings))
	d.Set("created_at", logicalPort.CreatedAt)
	d.Set("updated_at", logicalPort.UpdatedAt)
}

func flattenBindings(bindings logicalports.Bindings) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"id":                        bindings.ID,
		"segment_id":                bindings.SegmentID,
		"segment_display_name":      bindings.SegmentDisplayName,
		"port_security":             bindings.PortSecurity,
		"address_detection":         bindings.AddressDetection,
		"is_excluded_from_firewall": bindings.IsExcludedFromFirewall,
		"version_id":                bindings.VersionID,
		"created_at":                bindings.CreatedAt,
		"updated_at":                bindings.UpdatedAt,
	}
	res = append(res, temp)
	return res
}

func flattenStatus(status logicalports.Status) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"common":      status.Common,
		"hypervisors": flattenHypervisors(status.Hypervisors),
	}
	res = append(res, temp)
	return res
}

func flattenHypervisors(hv []logicalports.HypervisorStatus) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(hv))
	for _, v := range hv {
		temp := map[string]interface{}{
			"status":            v.Status,
			"name":              v.Name,
			"display_name":      v.DisplayName,
			"hypervisor_status": v.HypervisorStatus,
			"synced_at":         v.SyncedAt,
		}
		res = append(res, temp)
	}
	return res
}
