package infoblox

/*
Copyright 2024 The external-dns-infoblox-webhook Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"sort"

	ibclient "github.com/infobloxopen/infoblox-go-client/v2"
	"sigs.k8s.io/external-dns/endpoint"
)

type ResponseDetail struct {
	Target string
	TTL    int64
}

type ResponseDetails []ResponseDetail

type ResponseMap struct {
	RecordType string
	Map        map[string]ResponseDetails
}

func ToAResponseMap(res []ibclient.RecordA) *ResponseMap {
	rm := &ResponseMap{
		Map:        make(map[string]ResponseDetails),
		RecordType: ibclient.ARecord,
	}
	for _, record := range res {
		if _, ok := rm.Map[AsString(record.Name)]; !ok {
			rm.Map[AsString(record.Name)] = ResponseDetails{{Target: AsString(record.Ipv4Addr), TTL: AsInt64(record.Ttl)}}
			continue
		}
		rm.Map[AsString(record.Name)] = append(rm.Map[AsString(record.Name)], ResponseDetail{Target: AsString(record.Ipv4Addr), TTL: AsInt64(record.Ttl)})
	}
	return rm
}

func ToCNAMEResponseMap(res []ibclient.RecordCNAME) *ResponseMap {
	rm := &ResponseMap{
		Map:        make(map[string]ResponseDetails),
		RecordType: ibclient.CnameRecord,
	}
	for _, record := range res {
		if _, ok := rm.Map[AsString(record.Name)]; !ok {
			rm.Map[AsString(record.Name)] = ResponseDetails{{Target: AsString(record.Canonical), TTL: AsInt64(record.Ttl)}}
			continue
		}
		rm.Map[AsString(record.Name)] = append(rm.Map[AsString(record.Name)], ResponseDetail{Target: AsString(record.Canonical), TTL: AsInt64(record.Ttl)})
	}
	return rm
}

func ToNSResponseMap(res []ibclient.RecordNS) *ResponseMap {
	rm := &ResponseMap{
		Map:        make(map[string]ResponseDetails),
		RecordType: ibclient.ZoneDelegatedConst,
	}
	for _, record := range res {
		if _, ok := rm.Map[record.Name]; !ok {
			rm.Map[record.Name] = ResponseDetails{{Target: AsString(record.Nameserver)}}
			continue
		}
		rm.Map[record.Name] = append(rm.Map[record.Name], ResponseDetail{Target: AsString(record.Nameserver)})
	}
	return rm
}

func ToTXTResponseMap(res []ibclient.RecordTXT) *ResponseMap {
	rm := &ResponseMap{
		Map:        make(map[string]ResponseDetails),
		RecordType: ibclient.TxtRecord,
	}
	for _, record := range res {
		if _, ok := rm.Map[AsString(record.Name)]; !ok {
			rm.Map[AsString(record.Name)] = ResponseDetails{{Target: AsString(record.Text), TTL: AsInt64(record.Ttl)}}
			continue
		}
		rm.Map[AsString(record.Name)] = append(rm.Map[AsString(record.Name)], ResponseDetail{Target: AsString(record.Text), TTL: AsInt64(record.Ttl)})
	}
	return rm
}

func ToHostResponseMap(res []ibclient.HostRecord) *ResponseMap {
	rm := &ResponseMap{
		Map:        make(map[string]ResponseDetails),
		RecordType: ibclient.ARecord, //.HostRecordConst,
	}
	for _, record := range res {
		rds := ResponseDetails{}
		for _, ip := range record.Ipv4Addrs {
			rds = append(rds, ResponseDetail{Target: AsString(ip.Ipv4Addr), TTL: AsInt64(record.Ttl)})
		}
		if _, ok := rm.Map[AsString(record.Name)]; !ok {
			rm.Map[AsString(record.Name)] = rds
			continue
		}
		rm.Map[AsString(record.Name)] = append(rm.Map[AsString(record.Name)], rds...)
	}
	return rm
}

func ToPTRResponseMap(res []ibclient.RecordPTR) *ResponseMap {
	rm := &ResponseMap{
		Map:        make(map[string]ResponseDetails),
		RecordType: ibclient.PtrRecord,
	}
	for _, record := range res {
		if _, ok := rm.Map[AsString(record.PtrdName)]; !ok {
			rm.Map[AsString(record.PtrdName)] = ResponseDetails{{Target: AsString(record.Ipv4Addr), TTL: AsInt64(record.Ttl)}}
			continue
		}
		rm.Map[AsString(record.PtrdName)] = append(rm.Map[AsString(record.PtrdName)], ResponseDetail{Target: AsString(record.Ipv4Addr), TTL: AsInt64(record.Ttl)})
	}
	return rm
}

func (rd ResponseDetails) ToEndpointDetail() (targets []string, ttl endpoint.TTL) {
	for _, v := range rd {
		targets = append(targets, v.Target)
		ttl = endpoint.TTL(v.TTL)
	}
	return
}

func (rm *ResponseMap) ToEndpoints() []*endpoint.Endpoint {
	var endpoints []*endpoint.Endpoint
	for k, v := range rm.Map {
		targets, ttl := v.ToEndpointDetail()
		ep := endpoint.NewEndpointWithTTL(k, rm.RecordType, ttl, targets...)
		sort.Sort(ep.Targets)
		endpoints = append(endpoints, ep)
	}
	return endpoints
}
