package segments

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

func flattenSegment(d *schema.ResourceData, segmentRecord *segments.SegmentResponse) {
	d.Set("access_group_id", segmentRecord.AccessGroupID)
	d.Set("access_group_name", segmentRecord.AccessGroupName)
	d.Set("created_at", segmentRecord.CreatedAt.String())
	d.Set("description", segmentRecord.Description)
	d.Set("dhcp_v4", flattenDHCPv4(segmentRecord.DHCPv4))
	d.Set("dhcp_v6", flattenDHCPv6(segmentRecord.DHCPv6))
	d.Set("display_name", segmentRecord.DisplayName)
	d.Set("enabled", segmentRecord.Enabled)
	d.Set("logical_ports_info", flattenEntity(segmentRecord.LogicalPortsInfo))
	d.Set("routers_info", flattenEntity(segmentRecord.RoutersInfo))
	d.Set("status", flattenStatus(segmentRecord.Status))
	d.Set("subnet_v4", segmentRecord.SubnetV4)
	d.Set("subnet_v6", segmentRecord.SubnetV6)
	d.Set("updated_at", segmentRecord.UpdatedAt.String())
	d.Set("version_id", segmentRecord.VersionID)
}

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

func flattenHypervisors(hv []segments.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.String(),
		}
		res = append(res, temp)
	}
	return res
}

func flattenEntity(ei []segments.EntityInfo) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(ei))
	for _, v := range ei {
		temp := map[string]interface{}{
			"display_name": v.DisplayName,
			"id":           v.ID,
		}
		res = append(res, temp)
	}
	return res
}

func flattenDHCPv4(dchp segments.DHCPv4Config) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"dns":                     dchp.DNS,
		"excluded_address_ranges": dchp.ExcludedAddressRanges,
		"gateway":                 dchp.Gateway,
		"id":                      dchp.ID,
		"lease_time":              dchp.LeaseTime,
		"server_ip":               dchp.ServerIP,
		"server_mac":              dchp.ServerMAC,
		"enabled":                 dchp.Enabled,
	}
	res = append(res, temp)
	return res
}

func flattenDHCPv6(dchp segments.DHCPv6Config) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"dns":            dchp.DNS,
		"address_prefix": dchp.AddressPrefix,
		"id":             dchp.ID,
		"lease_time":     dchp.LeaseTime,
		"server_mac":     dchp.ServerMAC,
		"enabled":        dchp.Enabled,
	}
	res = append(res, temp)
	return res
}

func flattenSegmentList(sl *segments.ListSegment) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(*sl))
	for _, v := range *sl {
		temp := map[string]interface{}{
			"access_group_id":    v.AccessGroupID,
			"access_group_name":  v.AccessGroupName,
			"created_at":         v.CreatedAt.String(),
			"description":        v.Description,
			"dhcp_v4":            flattenDHCPv4(v.DHCPv4),
			"dhcp_v6":            flattenDHCPv6(v.DHCPv6),
			"display_name":       v.DisplayName,
			"enabled":            v.Enabled,
			"logical_ports_info": flattenEntity(v.LogicalPortsInfo),
			"routers_info":       flattenEntity(v.RoutersInfo),
			"status":             flattenStatus(v.Status),
			"id":                 v.ID,
			"subnet_v4":          v.SubnetV4,
			"subnet_v6":          v.SubnetV6,
			"updated_at":         v.UpdatedAt.String(),
			"version_id":         v.VersionID,
		}
		res = append(res, temp)
	}
	return res
}
