// 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"
	"net/url"
	"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 CommunityListExpanded struct {
	Device  types.String                   `tfsdk:"device"`
	Id      types.String                   `tfsdk:"id"`
	Name    types.String                   `tfsdk:"name"`
	Entries []CommunityListExpandedEntries `tfsdk:"entries"`
}

type CommunityListExpandedData struct {
	Device  types.String                   `tfsdk:"device"`
	Id      types.String                   `tfsdk:"id"`
	Name    types.String                   `tfsdk:"name"`
	Entries []CommunityListExpandedEntries `tfsdk:"entries"`
}
type CommunityListExpandedEntries struct {
	Action types.String `tfsdk:"action"`
	Regex  types.String `tfsdk:"regex"`
}

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

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

func (data CommunityListExpanded) getPath() string {
	return fmt.Sprintf("Cisco-IOS-XE-native:native/ip/Cisco-IOS-XE-bgp:community-list/expanded=%v", url.QueryEscape(fmt.Sprintf("%v", data.Name.ValueString())))
}

func (data CommunityListExpandedData) getPath() string {
	return fmt.Sprintf("Cisco-IOS-XE-native:native/ip/Cisco-IOS-XE-bgp:community-list/expanded=%v", url.QueryEscape(fmt.Sprintf("%v", data.Name.ValueString())))
}

// if last path element has a key -> remove it
func (data CommunityListExpanded) 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 CommunityListExpanded) toBody(ctx context.Context) string {
	body := `{"` + helpers.LastElement(data.getPath()) + `":{}}`
	if !data.Name.IsNull() && !data.Name.IsUnknown() {
		body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"name", data.Name.ValueString())
	}
	if len(data.Entries) > 0 {
		body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"extended-grouping.extended_grouping", []interface{}{})
		for index, item := range data.Entries {
			if !item.Action.IsNull() && !item.Action.IsUnknown() {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"extended-grouping.extended_grouping"+"."+strconv.Itoa(index)+"."+"action", item.Action.ValueString())
			}
			if !item.Regex.IsNull() && !item.Regex.IsUnknown() {
				body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"extended-grouping.extended_grouping"+"."+strconv.Itoa(index)+"."+"string", item.Regex.ValueString())
			}
		}
	}
	return body
}

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

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

func (data *CommunityListExpanded) updateFromBody(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 + "name"); value.Exists() && !data.Name.IsNull() {
		data.Name = types.StringValue(value.String())
	} else {
		data.Name = types.StringNull()
	}
	for i := range data.Entries {
		keys := [...]string{"action", "string"}
		keyValues := [...]string{data.Entries[i].Action.ValueString(), data.Entries[i].Regex.ValueString()}

		var r gjson.Result
		res.Get(prefix + "extended-grouping.extended_grouping").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("action"); value.Exists() && !data.Entries[i].Action.IsNull() {
			data.Entries[i].Action = types.StringValue(value.String())
		} else {
			data.Entries[i].Action = types.StringNull()
		}
		if value := r.Get("string"); value.Exists() && !data.Entries[i].Regex.IsNull() {
			data.Entries[i].Regex = types.StringValue(value.String())
		} else {
			data.Entries[i].Regex = types.StringNull()
		}
	}
}

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

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

func (data *CommunityListExpanded) 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 + "extended-grouping.extended_grouping"); value.Exists() {
		data.Entries = make([]CommunityListExpandedEntries, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := CommunityListExpandedEntries{}
			if cValue := v.Get("action"); cValue.Exists() {
				item.Action = types.StringValue(cValue.String())
			}
			if cValue := v.Get("string"); cValue.Exists() {
				item.Regex = types.StringValue(cValue.String())
			}
			data.Entries = append(data.Entries, item)
			return true
		})
	}
}

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

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

func (data *CommunityListExpandedData) 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 + "extended-grouping.extended_grouping"); value.Exists() {
		data.Entries = make([]CommunityListExpandedEntries, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := CommunityListExpandedEntries{}
			if cValue := v.Get("action"); cValue.Exists() {
				item.Action = types.StringValue(cValue.String())
			}
			if cValue := v.Get("string"); cValue.Exists() {
				item.Regex = types.StringValue(cValue.String())
			}
			data.Entries = append(data.Entries, item)
			return true
		})
	}
}

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

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

func (data *CommunityListExpanded) getDeletedItems(ctx context.Context, state CommunityListExpanded) []string {
	deletedItems := make([]string, 0)
	for i := range state.Entries {
		stateKeyValues := [...]string{state.Entries[i].Action.ValueString(), state.Entries[i].Regex.ValueString()}

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

		found := false
		for j := range data.Entries {
			found = true
			if state.Entries[i].Action.ValueString() != data.Entries[j].Action.ValueString() {
				found = false
			}
			if state.Entries[i].Regex.ValueString() != data.Entries[j].Regex.ValueString() {
				found = false
			}
			if found {
				break
			}
		}
		if !found {
			deletedItems = append(deletedItems, fmt.Sprintf("%v/extended-grouping/extended_grouping=%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 *CommunityListExpanded) getEmptyLeafsDelete(ctx context.Context) []string {
	emptyLeafsDelete := make([]string, 0)

	return emptyLeafsDelete
}

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

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

func (data *CommunityListExpanded) getDeletePaths(ctx context.Context) []string {
	var deletePaths []string
	for i := range data.Entries {
		keyValues := [...]string{data.Entries[i].Action.ValueString(), data.Entries[i].Regex.ValueString()}

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

	return deletePaths
}

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