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

package provider

import (
	"context"
	"fmt"

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

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

type SLAClassEntries struct {
	AppProbeClassId            types.String `tfsdk:"app_probe_class_id"`
	Jitter                     types.Int64  `tfsdk:"jitter"`
	Latency                    types.Int64  `tfsdk:"latency"`
	Loss                       types.Int64  `tfsdk:"loss"`
	FallbackBestTunnelCriteria types.String `tfsdk:"fallback_best_tunnel_criteria"`
	FallbackBestTunnelJitter   types.Int64  `tfsdk:"fallback_best_tunnel_jitter"`
	FallbackBestTunnelLatency  types.Int64  `tfsdk:"fallback_best_tunnel_latency"`
	FallbackBestTunnelLoss     types.Int64  `tfsdk:"fallback_best_tunnel_loss"`
}

func (data SLAClass) getType() string {
	return "sla"
}

func (data SLAClass) 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", "sla")
	if len(data.Entries) > 0 {
		body, _ = sjson.Set(body, "entries", []interface{}{})
		for _, item := range data.Entries {
			itemBody := ""
			if !item.AppProbeClassId.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "appProbeClass", item.AppProbeClassId.ValueString())
			}
			if !item.Jitter.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "jitter", fmt.Sprint(item.Jitter.ValueInt64()))
			}
			if !item.Latency.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "latency", fmt.Sprint(item.Latency.ValueInt64()))
			}
			if !item.Loss.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "loss", fmt.Sprint(item.Loss.ValueInt64()))
			}
			if !item.FallbackBestTunnelCriteria.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "fallbackBestTunnel.criteria", item.FallbackBestTunnelCriteria.ValueString())
			}
			if !item.FallbackBestTunnelJitter.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "fallbackBestTunnel.jitterVariance", fmt.Sprint(item.FallbackBestTunnelJitter.ValueInt64()))
			}
			if !item.FallbackBestTunnelLatency.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "fallbackBestTunnel.latencyVariance", fmt.Sprint(item.FallbackBestTunnelLatency.ValueInt64()))
			}
			if !item.FallbackBestTunnelLoss.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "fallbackBestTunnel.lossVariance", fmt.Sprint(item.FallbackBestTunnelLoss.ValueInt64()))
			}
			body, _ = sjson.SetRaw(body, "entries.-1", itemBody)
		}
	}
	return body
}

func (data *SLAClass) 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([]SLAClassEntries, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := SLAClassEntries{}
			if cValue := v.Get("appProbeClass"); cValue.Exists() {
				item.AppProbeClassId = types.StringValue(cValue.String())
			} else {
				item.AppProbeClassId = types.StringNull()
			}
			if cValue := v.Get("jitter"); cValue.Exists() {
				item.Jitter = types.Int64Value(cValue.Int())
			} else {
				item.Jitter = types.Int64Null()
			}
			if cValue := v.Get("latency"); cValue.Exists() {
				item.Latency = types.Int64Value(cValue.Int())
			} else {
				item.Latency = types.Int64Null()
			}
			if cValue := v.Get("loss"); cValue.Exists() {
				item.Loss = types.Int64Value(cValue.Int())
			} else {
				item.Loss = types.Int64Null()
			}
			if cValue := v.Get("fallbackBestTunnel.criteria"); cValue.Exists() {
				item.FallbackBestTunnelCriteria = types.StringValue(cValue.String())
			} else {
				item.FallbackBestTunnelCriteria = types.StringNull()
			}
			if cValue := v.Get("fallbackBestTunnel.jitterVariance"); cValue.Exists() {
				item.FallbackBestTunnelJitter = types.Int64Value(cValue.Int())
			} else {
				item.FallbackBestTunnelJitter = types.Int64Null()
			}
			if cValue := v.Get("fallbackBestTunnel.latencyVariance"); cValue.Exists() {
				item.FallbackBestTunnelLatency = types.Int64Value(cValue.Int())
			} else {
				item.FallbackBestTunnelLatency = types.Int64Null()
			}
			if cValue := v.Get("fallbackBestTunnel.lossVariance"); cValue.Exists() {
				item.FallbackBestTunnelLoss = types.Int64Value(cValue.Int())
			} else {
				item.FallbackBestTunnelLoss = types.Int64Null()
			}
			data.Entries = append(data.Entries, item)
			return true
		})
	}
}
