// 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 SLA struct {
	Device    types.String   `tfsdk:"device"`
	Id        types.String   `tfsdk:"id"`
	Entries   []SLAEntries   `tfsdk:"entries"`
	Schedules []SLASchedules `tfsdk:"schedules"`
}

type SLAData struct {
	Device    types.String   `tfsdk:"device"`
	Id        types.String   `tfsdk:"id"`
	Entries   []SLAEntries   `tfsdk:"entries"`
	Schedules []SLASchedules `tfsdk:"schedules"`
}
type SLAEntries struct {
	Number              types.Int64  `tfsdk:"number"`
	IcmpEchoDestination types.String `tfsdk:"icmp_echo_destination"`
	IcmpEchoSourceIp    types.String `tfsdk:"icmp_echo_source_ip"`
}
type SLASchedules struct {
	EntryNumber  types.Int64 `tfsdk:"entry_number"`
	Life         types.Int64 `tfsdk:"life"`
	StartTimeNow types.Bool  `tfsdk:"start_time_now"`
}

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

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

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

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

// if last path element has a key -> remove it
func (data SLA) 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 SLA) toBody(ctx context.Context) string {
	body := `{"` + helpers.LastElement(data.getPath()) + `":{}}`
	if len(data.Entries) > 0 {
		body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"entry", []interface{}{})
		for index, item := range data.Entries {
			if !item.Number.IsNull() && !item.Number.IsUnknown() {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"entry"+"."+strconv.Itoa(index)+"."+"number", strconv.FormatInt(item.Number.ValueInt64(), 10))
			}
			if !item.IcmpEchoDestination.IsNull() && !item.IcmpEchoDestination.IsUnknown() {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"entry"+"."+strconv.Itoa(index)+"."+"icmp-echo.destination", item.IcmpEchoDestination.ValueString())
			}
			if !item.IcmpEchoSourceIp.IsNull() && !item.IcmpEchoSourceIp.IsUnknown() {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"entry"+"."+strconv.Itoa(index)+"."+"icmp-echo.source-ip", item.IcmpEchoSourceIp.ValueString())
			}
		}
	}
	if len(data.Schedules) > 0 {
		body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"schedule", []interface{}{})
		for index, item := range data.Schedules {
			if !item.EntryNumber.IsNull() && !item.EntryNumber.IsUnknown() {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"schedule"+"."+strconv.Itoa(index)+"."+"entry-number", strconv.FormatInt(item.EntryNumber.ValueInt64(), 10))
			}
			if !item.Life.IsNull() && !item.Life.IsUnknown() {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"schedule"+"."+strconv.Itoa(index)+"."+"life", strconv.FormatInt(item.Life.ValueInt64(), 10))
			}
			if !item.StartTimeNow.IsNull() && !item.StartTimeNow.IsUnknown() {
				if item.StartTimeNow.ValueBool() {
					body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"schedule"+"."+strconv.Itoa(index)+"."+"start-time.now-config", 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 *SLA) 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.Entries {
		keys := [...]string{"number"}
		keyValues := [...]string{strconv.FormatInt(data.Entries[i].Number.ValueInt64(), 10)}

		var r gjson.Result
		res.Get(prefix + "entry").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("number"); value.Exists() && !data.Entries[i].Number.IsNull() {
			data.Entries[i].Number = types.Int64Value(value.Int())
		} else {
			data.Entries[i].Number = types.Int64Null()
		}
		if value := r.Get("icmp-echo.destination"); value.Exists() && !data.Entries[i].IcmpEchoDestination.IsNull() {
			data.Entries[i].IcmpEchoDestination = types.StringValue(value.String())
		} else {
			data.Entries[i].IcmpEchoDestination = types.StringNull()
		}
		if value := r.Get("icmp-echo.source-ip"); value.Exists() && !data.Entries[i].IcmpEchoSourceIp.IsNull() {
			data.Entries[i].IcmpEchoSourceIp = types.StringValue(value.String())
		} else {
			data.Entries[i].IcmpEchoSourceIp = types.StringNull()
		}
	}
	for i := range data.Schedules {
		keys := [...]string{"entry-number"}
		keyValues := [...]string{strconv.FormatInt(data.Schedules[i].EntryNumber.ValueInt64(), 10)}

		var r gjson.Result
		res.Get(prefix + "schedule").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("entry-number"); value.Exists() && !data.Schedules[i].EntryNumber.IsNull() {
			data.Schedules[i].EntryNumber = types.Int64Value(value.Int())
		} else {
			data.Schedules[i].EntryNumber = types.Int64Null()
		}
		if value := r.Get("life"); value.Exists() && !data.Schedules[i].Life.IsNull() {
			data.Schedules[i].Life = types.Int64Value(value.Int())
		} else {
			data.Schedules[i].Life = types.Int64Null()
		}
		if value := r.Get("start-time.now-config"); !data.Schedules[i].StartTimeNow.IsNull() {
			if value.Exists() {
				data.Schedules[i].StartTimeNow = types.BoolValue(true)
			} else {
				data.Schedules[i].StartTimeNow = types.BoolValue(false)
			}
		} else {
			data.Schedules[i].StartTimeNow = types.BoolNull()
		}
	}
}

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

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

func (data *SLA) 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 + "entry"); value.Exists() {
		data.Entries = make([]SLAEntries, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := SLAEntries{}
			if cValue := v.Get("number"); cValue.Exists() {
				item.Number = types.Int64Value(cValue.Int())
			}
			if cValue := v.Get("icmp-echo.destination"); cValue.Exists() {
				item.IcmpEchoDestination = types.StringValue(cValue.String())
			}
			if cValue := v.Get("icmp-echo.source-ip"); cValue.Exists() {
				item.IcmpEchoSourceIp = types.StringValue(cValue.String())
			}
			data.Entries = append(data.Entries, item)
			return true
		})
	}
	if value := res.Get(prefix + "schedule"); value.Exists() {
		data.Schedules = make([]SLASchedules, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := SLASchedules{}
			if cValue := v.Get("entry-number"); cValue.Exists() {
				item.EntryNumber = types.Int64Value(cValue.Int())
			}
			if cValue := v.Get("life"); cValue.Exists() {
				item.Life = types.Int64Value(cValue.Int())
			}
			if cValue := v.Get("start-time.now-config"); cValue.Exists() {
				item.StartTimeNow = types.BoolValue(true)
			} else {
				item.StartTimeNow = types.BoolValue(false)
			}
			data.Schedules = append(data.Schedules, item)
			return true
		})
	}
}

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

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

func (data *SLAData) 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 + "entry"); value.Exists() {
		data.Entries = make([]SLAEntries, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := SLAEntries{}
			if cValue := v.Get("number"); cValue.Exists() {
				item.Number = types.Int64Value(cValue.Int())
			}
			if cValue := v.Get("icmp-echo.destination"); cValue.Exists() {
				item.IcmpEchoDestination = types.StringValue(cValue.String())
			}
			if cValue := v.Get("icmp-echo.source-ip"); cValue.Exists() {
				item.IcmpEchoSourceIp = types.StringValue(cValue.String())
			}
			data.Entries = append(data.Entries, item)
			return true
		})
	}
	if value := res.Get(prefix + "schedule"); value.Exists() {
		data.Schedules = make([]SLASchedules, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := SLASchedules{}
			if cValue := v.Get("entry-number"); cValue.Exists() {
				item.EntryNumber = types.Int64Value(cValue.Int())
			}
			if cValue := v.Get("life"); cValue.Exists() {
				item.Life = types.Int64Value(cValue.Int())
			}
			if cValue := v.Get("start-time.now-config"); cValue.Exists() {
				item.StartTimeNow = types.BoolValue(true)
			} else {
				item.StartTimeNow = types.BoolValue(false)
			}
			data.Schedules = append(data.Schedules, item)
			return true
		})
	}
}

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

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

func (data *SLA) getDeletedItems(ctx context.Context, state SLA) []string {
	deletedItems := make([]string, 0)
	for i := range state.Schedules {
		stateKeyValues := [...]string{strconv.FormatInt(state.Schedules[i].EntryNumber.ValueInt64(), 10)}

		emptyKeys := true
		if !reflect.ValueOf(state.Schedules[i].EntryNumber.ValueInt64()).IsZero() {
			emptyKeys = false
		}
		if emptyKeys {
			continue
		}

		found := false
		for j := range data.Schedules {
			found = true
			if state.Schedules[i].EntryNumber.ValueInt64() != data.Schedules[j].EntryNumber.ValueInt64() {
				found = false
			}
			if found {
				if !state.Schedules[i].StartTimeNow.IsNull() && data.Schedules[j].StartTimeNow.IsNull() {
					deletedItems = append(deletedItems, fmt.Sprintf("%v/schedule=%v/start-time/now-config", state.getPath(), strings.Join(stateKeyValues[:], ",")))
				}
				if !state.Schedules[i].Life.IsNull() && data.Schedules[j].Life.IsNull() {
					deletedItems = append(deletedItems, fmt.Sprintf("%v/schedule=%v/life", state.getPath(), strings.Join(stateKeyValues[:], ",")))
				}
				break
			}
		}
		if !found {
			deletedItems = append(deletedItems, fmt.Sprintf("%v/schedule=%v", state.getPath(), strings.Join(stateKeyValues[:], ",")))
		}
	}
	for i := range state.Entries {
		stateKeyValues := [...]string{strconv.FormatInt(state.Entries[i].Number.ValueInt64(), 10)}

		emptyKeys := true
		if !reflect.ValueOf(state.Entries[i].Number.ValueInt64()).IsZero() {
			emptyKeys = false
		}
		if emptyKeys {
			continue
		}

		found := false
		for j := range data.Entries {
			found = true
			if state.Entries[i].Number.ValueInt64() != data.Entries[j].Number.ValueInt64() {
				found = false
			}
			if found {
				if !state.Entries[i].IcmpEchoSourceIp.IsNull() && data.Entries[j].IcmpEchoSourceIp.IsNull() {
					deletedItems = append(deletedItems, fmt.Sprintf("%v/entry=%v/icmp-echo/source-ip", state.getPath(), strings.Join(stateKeyValues[:], ",")))
				}
				if !state.Entries[i].IcmpEchoDestination.IsNull() && data.Entries[j].IcmpEchoDestination.IsNull() {
					deletedItems = append(deletedItems, fmt.Sprintf("%v/entry=%v/icmp-echo/destination", state.getPath(), strings.Join(stateKeyValues[:], ",")))
				}
				break
			}
		}
		if !found {
			deletedItems = append(deletedItems, fmt.Sprintf("%v/entry=%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 *SLA) getEmptyLeafsDelete(ctx context.Context) []string {
	emptyLeafsDelete := make([]string, 0)

	for i := range data.Schedules {
		keyValues := [...]string{strconv.FormatInt(data.Schedules[i].EntryNumber.ValueInt64(), 10)}
		if !data.Schedules[i].StartTimeNow.IsNull() && !data.Schedules[i].StartTimeNow.ValueBool() {
			emptyLeafsDelete = append(emptyLeafsDelete, fmt.Sprintf("%v/schedule=%v/start-time/now-config", data.getPath(), strings.Join(keyValues[:], ",")))
		}
	}

	return emptyLeafsDelete
}

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

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

func (data *SLA) getDeletePaths(ctx context.Context) []string {
	var deletePaths []string
	for i := range data.Schedules {
		keyValues := [...]string{strconv.FormatInt(data.Schedules[i].EntryNumber.ValueInt64(), 10)}

		deletePaths = append(deletePaths, fmt.Sprintf("%v/schedule=%v", data.getPath(), strings.Join(keyValues[:], ",")))
	}
	for i := range data.Entries {
		keyValues := [...]string{strconv.FormatInt(data.Entries[i].Number.ValueInt64(), 10)}

		deletePaths = append(deletePaths, fmt.Sprintf("%v/entry=%v", data.getPath(), strings.Join(keyValues[:], ",")))
	}

	return deletePaths
}

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