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

package provider

import (
	"context"

	"github.com/hashicorp/terraform-plugin-framework/types"
	"github.com/tidwall/gjson"
	"github.com/tidwall/sjson"
)

type AppProbeClass struct {
	Id      types.String           `tfsdk:"id"`
	Version types.Int64            `tfsdk:"version"`
	Name    types.String           `tfsdk:"name"`
	Entries []AppProbeClassEntries `tfsdk:"entries"`
}

type AppProbeClassEntries struct {
	ForwardingClass types.String                   `tfsdk:"forwarding_class"`
	Mappings        []AppProbeClassEntriesMappings `tfsdk:"mappings"`
}

type AppProbeClassEntriesMappings struct {
	Color types.String `tfsdk:"color"`
	Dscp  types.Int64  `tfsdk:"dscp"`
}

func (data AppProbeClass) getType() string {
	return "appProbe"
}

func (data AppProbeClass) toBody(ctx context.Context) string {
	body, _ := sjson.Set("", "description", "Desc Not Required")
	body, _ = sjson.Set(body, "name", data.Name.ValueString())
	body, _ = sjson.Set(body, "type", "appProbe")
	if len(data.Entries) > 0 {
		body, _ = sjson.Set(body, "entries", []interface{}{})
		for _, item := range data.Entries {
			itemBody := ""
			if !item.ForwardingClass.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "forwardingClass", item.ForwardingClass.ValueString())
			}
			if len(item.Mappings) > 0 {
				itemBody, _ = sjson.Set(itemBody, "map", []interface{}{})
				for _, childItem := range item.Mappings {
					itemChildBody := ""
					if !childItem.Color.IsNull() {
						itemChildBody, _ = sjson.Set(itemChildBody, "color", childItem.Color.ValueString())
					}
					if !childItem.Dscp.IsNull() {
						itemChildBody, _ = sjson.Set(itemChildBody, "dscp", childItem.Dscp.ValueInt64())
					}
					itemBody, _ = sjson.SetRaw(itemBody, "map.-1", itemChildBody)
				}
			}
			body, _ = sjson.SetRaw(body, "entries.-1", itemBody)
		}
	}
	return body
}

func (data *AppProbeClass) fromBody(ctx context.Context, res gjson.Result) {
	if value := res.Get("name"); value.Exists() {
		data.Name = types.StringValue(value.String())
	} else {
		data.Name = types.StringNull()
	}
	if value := res.Get("entries"); value.Exists() {
		data.Entries = make([]AppProbeClassEntries, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := AppProbeClassEntries{}
			if cValue := v.Get("forwardingClass"); cValue.Exists() {
				item.ForwardingClass = types.StringValue(cValue.String())
			} else {
				item.ForwardingClass = types.StringNull()
			}
			if cValue := v.Get("map"); cValue.Exists() {
				item.Mappings = make([]AppProbeClassEntriesMappings, 0)
				cValue.ForEach(func(ck, cv gjson.Result) bool {
					cItem := AppProbeClassEntriesMappings{}
					if ccValue := cv.Get("color"); ccValue.Exists() {
						cItem.Color = types.StringValue(ccValue.String())
					} else {
						cItem.Color = types.StringNull()
					}
					if ccValue := cv.Get("dscp"); ccValue.Exists() {
						cItem.Dscp = types.Int64Value(ccValue.Int())
					} else {
						cItem.Dscp = types.Int64Null()
					}
					item.Mappings = append(item.Mappings, cItem)
					return true
				})
			}
			data.Entries = append(data.Entries, item)
			return true
		})
	}
}
