// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Mozilla Public 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
//
//     https://mozilla.org/MPL/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.
//
// SPDX-License-Identifier: MPL-2.0

// Code generated by "gen/generator.go"; DO NOT EDIT.

package provider

// Section below is generated&owned by "gen/generator.go". //template:begin imports
import (
	"context"
	"fmt"
	"reflect"
	"regexp"
	"strconv"
	"strings"

	"github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"github.com/tidwall/gjson"
	"github.com/tidwall/sjson"
)

// End of section. //template:end imports

// Section below is generated&owned by "gen/generator.go". //template:begin types
type NAT struct {
	Device                 types.String                `tfsdk:"device"`
	Id                     types.String                `tfsdk:"id"`
	DeleteMode             types.String                `tfsdk:"delete_mode"`
	InsideSourceInterfaces []NATInsideSourceInterfaces `tfsdk:"inside_source_interfaces"`
}

type NATData struct {
	Device                 types.String                `tfsdk:"device"`
	Id                     types.String                `tfsdk:"id"`
	InsideSourceInterfaces []NATInsideSourceInterfaces `tfsdk:"inside_source_interfaces"`
}
type NATInsideSourceInterfaces struct {
	Id         types.String                          `tfsdk:"id"`
	Interfaces []NATInsideSourceInterfacesInterfaces `tfsdk:"interfaces"`
}
type NATInsideSourceInterfacesInterfaces struct {
	Interface types.String `tfsdk:"interface"`
	Overload  types.Bool   `tfsdk:"overload"`
}

// End of section. //template:end types

// Section below is generated&owned by "gen/generator.go". //template:begin getPath

func (data NAT) getPath() string {
	return "Cisco-IOS-XE-native:native/ip/Cisco-IOS-XE-nat:nat"
}

func (data NATData) getPath() string {
	return "Cisco-IOS-XE-native:native/ip/Cisco-IOS-XE-nat:nat"
}

// if last path element has a key -> remove it
func (data NAT) getPathShort() string {
	path := data.getPath()
	re := regexp.MustCompile(`(.*)=[^\/]*$`)
	matches := re.FindStringSubmatch(path)
	if len(matches) <= 1 {
		return path
	}
	return matches[1]
}

// End of section. //template:end getPath

// Section below is generated&owned by "gen/generator.go". //template:begin toBody

func (data NAT) toBody(ctx context.Context) string {
	body := `{"` + helpers.LastElement(data.getPath()) + `":{}}`
	if len(data.InsideSourceInterfaces) > 0 {
		body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"inside.source.list-interface.list", []interface{}{})
		for index, item := range data.InsideSourceInterfaces {
			if !item.Id.IsNull() && !item.Id.IsUnknown() {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"inside.source.list-interface.list"+"."+strconv.Itoa(index)+"."+"id", item.Id.ValueString())
			}
			if len(item.Interfaces) > 0 {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"inside.source.list-interface.list"+"."+strconv.Itoa(index)+"."+"interface", []interface{}{})
				for cindex, citem := range item.Interfaces {
					if !citem.Interface.IsNull() && !citem.Interface.IsUnknown() {
						body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"inside.source.list-interface.list"+"."+strconv.Itoa(index)+"."+"interface"+"."+strconv.Itoa(cindex)+"."+"name", citem.Interface.ValueString())
					}
					if !citem.Overload.IsNull() && !citem.Overload.IsUnknown() {
						if citem.Overload.ValueBool() {
							body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"inside.source.list-interface.list"+"."+strconv.Itoa(index)+"."+"interface"+"."+strconv.Itoa(cindex)+"."+"overload-new", map[string]string{})
						}
					}
				}
			}
		}
	}
	return body
}

// End of section. //template:end toBody

// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBody

func (data *NAT) updateFromBody(ctx context.Context, res gjson.Result) {
	prefix := helpers.LastElement(data.getPath()) + "."
	if res.Get(helpers.LastElement(data.getPath())).IsArray() {
		prefix += "0."
	}
	for i := range data.InsideSourceInterfaces {
		keys := [...]string{"id"}
		keyValues := [...]string{data.InsideSourceInterfaces[i].Id.ValueString()}

		var r gjson.Result
		res.Get(prefix + "inside.source.list-interface.list").ForEach(
			func(_, v gjson.Result) bool {
				found := false
				for ik := range keys {
					if v.Get(keys[ik]).String() == keyValues[ik] {
						found = true
						continue
					}
					found = false
					break
				}
				if found {
					r = v
					return false
				}
				return true
			},
		)
		if value := r.Get("id"); value.Exists() && !data.InsideSourceInterfaces[i].Id.IsNull() {
			data.InsideSourceInterfaces[i].Id = types.StringValue(value.String())
		} else {
			data.InsideSourceInterfaces[i].Id = types.StringNull()
		}
		for ci := range data.InsideSourceInterfaces[i].Interfaces {
			keys := [...]string{"name"}
			keyValues := [...]string{data.InsideSourceInterfaces[i].Interfaces[ci].Interface.ValueString()}

			var cr gjson.Result
			r.Get("interface").ForEach(
				func(_, v gjson.Result) bool {
					found := false
					for ik := range keys {
						if v.Get(keys[ik]).String() == keyValues[ik] {
							found = true
							continue
						}
						found = false
						break
					}
					if found {
						cr = v
						return false
					}
					return true
				},
			)
			if value := cr.Get("name"); value.Exists() && !data.InsideSourceInterfaces[i].Interfaces[ci].Interface.IsNull() {
				data.InsideSourceInterfaces[i].Interfaces[ci].Interface = types.StringValue(value.String())
			} else {
				data.InsideSourceInterfaces[i].Interfaces[ci].Interface = types.StringNull()
			}
			if value := cr.Get("overload-new"); !data.InsideSourceInterfaces[i].Interfaces[ci].Overload.IsNull() {
				if value.Exists() {
					data.InsideSourceInterfaces[i].Interfaces[ci].Overload = types.BoolValue(true)
				} else {
					data.InsideSourceInterfaces[i].Interfaces[ci].Overload = types.BoolValue(false)
				}
			} else {
				data.InsideSourceInterfaces[i].Interfaces[ci].Overload = types.BoolNull()
			}
		}
	}
}

// End of section. //template:end updateFromBody

// Section below is generated&owned by "gen/generator.go". //template:begin fromBody

func (data *NAT) fromBody(ctx context.Context, res gjson.Result) {
	prefix := helpers.LastElement(data.getPath()) + "."
	if res.Get(helpers.LastElement(data.getPath())).IsArray() {
		prefix += "0."
	}
	if value := res.Get(prefix + "inside.source.list-interface.list"); value.Exists() {
		data.InsideSourceInterfaces = make([]NATInsideSourceInterfaces, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := NATInsideSourceInterfaces{}
			if cValue := v.Get("id"); cValue.Exists() {
				item.Id = types.StringValue(cValue.String())
			}
			if cValue := v.Get("interface"); cValue.Exists() {
				item.Interfaces = make([]NATInsideSourceInterfacesInterfaces, 0)
				cValue.ForEach(func(ck, cv gjson.Result) bool {
					cItem := NATInsideSourceInterfacesInterfaces{}
					if ccValue := cv.Get("name"); ccValue.Exists() {
						cItem.Interface = types.StringValue(ccValue.String())
					}
					if ccValue := cv.Get("overload-new"); ccValue.Exists() {
						cItem.Overload = types.BoolValue(true)
					} else {
						cItem.Overload = types.BoolValue(false)
					}
					item.Interfaces = append(item.Interfaces, cItem)
					return true
				})
			}
			data.InsideSourceInterfaces = append(data.InsideSourceInterfaces, item)
			return true
		})
	}
}

// End of section. //template:end fromBody

// Section below is generated&owned by "gen/generator.go". //template:begin fromBodyData

func (data *NATData) fromBody(ctx context.Context, res gjson.Result) {
	prefix := helpers.LastElement(data.getPath()) + "."
	if res.Get(helpers.LastElement(data.getPath())).IsArray() {
		prefix += "0."
	}
	if value := res.Get(prefix + "inside.source.list-interface.list"); value.Exists() {
		data.InsideSourceInterfaces = make([]NATInsideSourceInterfaces, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := NATInsideSourceInterfaces{}
			if cValue := v.Get("id"); cValue.Exists() {
				item.Id = types.StringValue(cValue.String())
			}
			if cValue := v.Get("interface"); cValue.Exists() {
				item.Interfaces = make([]NATInsideSourceInterfacesInterfaces, 0)
				cValue.ForEach(func(ck, cv gjson.Result) bool {
					cItem := NATInsideSourceInterfacesInterfaces{}
					if ccValue := cv.Get("name"); ccValue.Exists() {
						cItem.Interface = types.StringValue(ccValue.String())
					}
					if ccValue := cv.Get("overload-new"); ccValue.Exists() {
						cItem.Overload = types.BoolValue(true)
					} else {
						cItem.Overload = types.BoolValue(false)
					}
					item.Interfaces = append(item.Interfaces, cItem)
					return true
				})
			}
			data.InsideSourceInterfaces = append(data.InsideSourceInterfaces, item)
			return true
		})
	}
}

// End of section. //template:end fromBodyData

// Section below is generated&owned by "gen/generator.go". //template:begin getDeletedItems

func (data *NAT) getDeletedItems(ctx context.Context, state NAT) []string {
	deletedItems := make([]string, 0)
	for i := range state.InsideSourceInterfaces {
		stateKeyValues := [...]string{state.InsideSourceInterfaces[i].Id.ValueString()}

		emptyKeys := true
		if !reflect.ValueOf(state.InsideSourceInterfaces[i].Id.ValueString()).IsZero() {
			emptyKeys = false
		}
		if emptyKeys {
			continue
		}

		found := false
		for j := range data.InsideSourceInterfaces {
			found = true
			if state.InsideSourceInterfaces[i].Id.ValueString() != data.InsideSourceInterfaces[j].Id.ValueString() {
				found = false
			}
			if found {
				for ci := range state.InsideSourceInterfaces[i].Interfaces {
					cstateKeyValues := [...]string{state.InsideSourceInterfaces[i].Interfaces[ci].Interface.ValueString()}

					cemptyKeys := true
					if !reflect.ValueOf(state.InsideSourceInterfaces[i].Interfaces[ci].Interface.ValueString()).IsZero() {
						cemptyKeys = false
					}
					if cemptyKeys {
						continue
					}

					found := false
					for cj := range data.InsideSourceInterfaces[j].Interfaces {
						found = true
						if state.InsideSourceInterfaces[i].Interfaces[ci].Interface.ValueString() != data.InsideSourceInterfaces[j].Interfaces[cj].Interface.ValueString() {
							found = false
						}
						if found {
							if !state.InsideSourceInterfaces[i].Interfaces[ci].Overload.IsNull() && data.InsideSourceInterfaces[j].Interfaces[cj].Overload.IsNull() {
								deletedItems = append(deletedItems, fmt.Sprintf("%v/inside/source/list-interface/list=%v/interface=%v/overload-new", state.getPath(), strings.Join(stateKeyValues[:], ","), strings.Join(cstateKeyValues[:], ",")))
							}
							break
						}
					}
					if !found {
						deletedItems = append(deletedItems, fmt.Sprintf("%v/inside/source/list-interface/list=%v/interface=%v", state.getPath(), strings.Join(stateKeyValues[:], ","), strings.Join(cstateKeyValues[:], ",")))
					}
				}
				break
			}
		}
		if !found {
			deletedItems = append(deletedItems, fmt.Sprintf("%v/inside/source/list-interface/list=%v", state.getPath(), strings.Join(stateKeyValues[:], ",")))
		}
	}

	return deletedItems
}

// End of section. //template:end getDeletedItems

// Section below is generated&owned by "gen/generator.go". //template:begin getEmptyLeafsDelete

func (data *NAT) getEmptyLeafsDelete(ctx context.Context) []string {
	emptyLeafsDelete := make([]string, 0)

	for i := range data.InsideSourceInterfaces {
		keyValues := [...]string{data.InsideSourceInterfaces[i].Id.ValueString()}

		for ci := range data.InsideSourceInterfaces[i].Interfaces {
			ckeyValues := [...]string{data.InsideSourceInterfaces[i].Interfaces[ci].Interface.ValueString()}
			if !data.InsideSourceInterfaces[i].Interfaces[ci].Overload.IsNull() && !data.InsideSourceInterfaces[i].Interfaces[ci].Overload.ValueBool() {
				emptyLeafsDelete = append(emptyLeafsDelete, fmt.Sprintf("%v/inside/source/list-interface/list=%v/interface=%v/overload-new", data.getPath(), strings.Join(keyValues[:], ","), strings.Join(ckeyValues[:], ",")))
			}
		}
	}

	return emptyLeafsDelete
}

// End of section. //template:end getEmptyLeafsDelete

// Section below is generated&owned by "gen/generator.go". //template:begin getDeletePaths

func (data *NAT) getDeletePaths(ctx context.Context) []string {
	var deletePaths []string
	for i := range data.InsideSourceInterfaces {
		keyValues := [...]string{data.InsideSourceInterfaces[i].Id.ValueString()}

		deletePaths = append(deletePaths, fmt.Sprintf("%v/inside/source/list-interface/list=%v", data.getPath(), strings.Join(keyValues[:], ",")))
	}

	return deletePaths
}

// End of section. //template:end getDeletePaths
