package extnet

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/extnet"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/flattens"
)

func flattenExtnet(d *schema.ResourceData, e *extnet.RecordExtNet) {
	d.Set("ckey", e.CKey)
	d.Set("meta", flattens.FlattenMeta(e.Meta))
	d.Set("check_ips", e.CheckIPs)
	d.Set("default", e.Default)
	d.Set("default_qos", flattenExtnetDefaultQos(e.DefaultQOS))
	d.Set("desc", e.Description)
	d.Set("dns", e.DNS)
	d.Set("excluded", flattenExcluded(e.Excluded))
	d.Set("free_ips", e.FreeIPs)
	d.Set("gateway", e.Gateway)
	d.Set("gid", e.GID)
	d.Set("guid", e.GUID)
	d.Set("ipcidr", e.IPCIDR)
	d.Set("milestones", e.Milestones)
	d.Set("net_name", e.Name)
	d.Set("network", e.Network)
	d.Set("network_ids", flattenNetworkIDs(e.NetworkIDs))
	d.Set("ntp", e.NTP)
	d.Set("pre_reservations_num", e.PreReservationsNum)
	d.Set("prefix", e.Prefix)
	d.Set("pri_vnf_dev_id", e.PriVNFDevID)
	d.Set("reservations", flattenExtnetReservations(e.Reservations))
	d.Set("shared_with", e.SharedWith)
	d.Set("status", e.Status)
	d.Set("vlan_id", e.VLANID)
	d.Set("vnfs", flattenExtnetVNFS(e.VNFs))
	d.Set("zone_id", e.ZoneID)
	d.Set("pre_reservations", flattenExtnetReservations(e.PreReservations))
	d.Set("sec_vnfdev_id", e.SecVNFDevID)
	d.Set("redundant", e.Redundant)
	d.Set("mtu", e.MTU)
}

func flattenExcluded(ex []extnet.Excluded) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(ex))
	for _, item := range ex {
		temp := map[string]interface{}{
			"client_type": item.ClientType,
			"mac":         item.MAC,
			"ip":          item.IP,
			"type":        item.Type,
			"vm_id":       item.VMID,
		}
		res = append(res, temp)
	}

	return res
}

func flattenExtnetReservations(ers extnet.ListReservations) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(ers))
	for _, er := range ers {
		temp := map[string]interface{}{
			"account_id":  er.AccountID,
			"client_type": er.ClientType,
			"domainname":  er.DomainName,
			"hostname":    er.Hostname,
			"desc":        er.Description,
			"ip":          er.IP,
			"mac":         er.MAC,
			"type":        er.Type,
			"vm_id":       er.VMID,
		}
		res = append(res, temp)
	}

	return res
}

func flattenExtnetDefaultQos(edqos extnet.QOS) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"e_rate":   edqos.ERate,
		"e_burst":  edqos.EBurst,
		"guid":     edqos.GUID,
		"in_burst": edqos.InBurst,
		"in_rate":  edqos.InRate,
	}
	res = append(res, temp)
	return res
}

func flattenExtnetVNFS(evnfs extnet.VNFs) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"dhcp": evnfs.DHCP,
	}
	res = append(res, temp)
	return res
}

func flattenExtnetsComputes(ecs extnet.ListExtNetExtends) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(ecs))
	for _, ec := range ecs {
		temp := map[string]interface{}{
			"net_id": ec.ID,
			"ipaddr": ec.IPAddr,
			"ipcidr": ec.IPCIDR,
			"name":   ec.Name,
		}
		res = append(res, temp)
	}
	return res
}

func flattenExtnetComputesList(ecl *extnet.ListExtNetComputes) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(ecl.Data))
	for _, ec := range ecl.Data {
		temp := map[string]interface{}{
			"account_id":   ec.AccountID,
			"account_name": ec.AccountName,
			"extnets":      flattenExtnetsComputes(ec.ExtNets),
			"id":           ec.ID,
			"name":         ec.Name,
			"rg_id":        ec.RGID,
			"rg_name":      ec.RGName,
		}
		res = append(res, temp)
	}
	return res
}

func flattenExtnetList(el *extnet.ListExtNets) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(el.Data))
	for _, e := range el.Data {
		temp := map[string]interface{}{
			"net_id": e.ID,
			"ipcidr": e.IPCIDR,
			"name":   e.Name,
			"status": e.Status,
		}
		res = append(res, temp)
	}
	return res
}

func flattenExtnetReservedIp(el []extnet.RecordReservedIP) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(el))
	for _, e := range el {
		reservations := make([]map[string]interface{}, 0, len(e.Reservations))
		for _, r := range e.Reservations {
			temp := map[string]interface{}{
				"account_id":  r.AccountID,
				"client_type": r.ClientType,
				"domain_name": r.DomainName,
				"hostname":    r.Hostname,
				"ip":          r.IP,
				"mac":         r.Mac,
				"type":        r.Type,
				"vm_id":       r.VMID,
			}
			reservations = append(reservations, temp)
		}
		item := map[string]interface{}{
			"extnet_id":    e.ExtnetID,
			"reservations": reservations,
		}
		res = append(res, item)
	}
	return res
}

func flattenNetworkIDs(ex extnet.NetworkIDs) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, 1)
	temp := map[string]interface{}{
		"primary":   ex.Primary,
		"secondary": ex.Secondary,
	}
	res = append(res, temp)

	return res
}
