// 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 RewriteRule struct {
	Id          types.String       `tfsdk:"id"`
	Version     types.Int64        `tfsdk:"version"`
	Type        types.String       `tfsdk:"type"`
	Name        types.String       `tfsdk:"name"`
	Description types.String       `tfsdk:"description"`
	Rules       []RewriteRuleRules `tfsdk:"rules"`
}

type RewriteRuleRules struct {
	ClassMapId      types.String `tfsdk:"class_map_id"`
	ClassMapVersion types.Int64  `tfsdk:"class_map_version"`
	Priority        types.String `tfsdk:"priority"`
	Dscp            types.Int64  `tfsdk:"dscp"`
	Layer2cos       types.Int64  `tfsdk:"layer2cos"`
}

func (data RewriteRule) getType() string {
	return "rewriteRule"
}

func (data RewriteRule) toBody(ctx context.Context) string {
	body, _ := sjson.Set("", "name", data.Name.ValueString())
	body, _ = sjson.Set(body, "description", data.Description.ValueString())
	body, _ = sjson.Set(body, "type", "rewriteRule")
	path := "definition."
	if len(data.Rules) > 0 {
		body, _ = sjson.Set(body, path+"rules", []interface{}{})
		for _, item := range data.Rules {
			itemBody := ""
			if !item.ClassMapId.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "class", item.ClassMapId.ValueString())
			}
			if !item.Priority.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "plp", item.Priority.ValueString())
			}
			if !item.Dscp.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "dscp", fmt.Sprint(item.Dscp.ValueInt64()))
			}
			if !item.Layer2cos.IsNull() {
				itemBody, _ = sjson.Set(itemBody, "layer2Cos", fmt.Sprint(item.Layer2cos.ValueInt64()))
			}
			body, _ = sjson.SetRaw(body, path+"rules.-1", itemBody)
		}
	}
	return body
}

func (data *RewriteRule) 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("description"); value.Exists() {
		data.Description = types.StringValue(value.String())
	} else {
		data.Description = types.StringNull()
	}
	if value := res.Get("type"); value.Exists() {
		data.Type = types.StringValue(value.String())
	} else {
		data.Type = types.StringNull()
	}
	path := "definition."
	if value := res.Get(path + "rules"); value.Exists() {
		data.Rules = make([]RewriteRuleRules, 0)
		value.ForEach(func(k, v gjson.Result) bool {
			item := RewriteRuleRules{}
			if cValue := v.Get("class"); cValue.Exists() {
				item.ClassMapId = types.StringValue(cValue.String())
			} else {
				item.ClassMapId = types.StringNull()
			}
			if cValue := v.Get("plp"); cValue.Exists() {
				item.Priority = types.StringValue(cValue.String())
			} else {
				item.Priority = types.StringNull()
			}
			if cValue := v.Get("dscp"); cValue.Exists() {
				item.Dscp = types.Int64Value(cValue.Int())
			} else {
				item.Dscp = types.Int64Null()
			}
			if cValue := v.Get("layer2Cos"); cValue.Exists() {
				item.Layer2cos = types.Int64Value(cValue.Int())
			} else {
				item.Layer2cos = types.Int64Null()
			}
			data.Rules = append(data.Rules, item)
			return true
		})
	}
}

func (data *RewriteRule) hasChanges(ctx context.Context, state *RewriteRule) bool {
	hasChanges := false
	if !data.Name.Equal(state.Name) {
		hasChanges = true
	}
	if !data.Description.Equal(state.Description) {
		hasChanges = true
	}
	if len(data.Rules) != len(state.Rules) {
		hasChanges = true
	} else {
		for i := range data.Rules {
			if !data.Rules[i].ClassMapId.Equal(state.Rules[i].ClassMapId) {
				hasChanges = true
			}
			if !data.Rules[i].Priority.Equal(state.Rules[i].Priority) {
				hasChanges = true
			}
			if !data.Rules[i].Dscp.Equal(state.Rules[i].Dscp) {
				hasChanges = true
			}
			if !data.Rules[i].Layer2cos.Equal(state.Rules[i].Layer2cos) {
				hasChanges = true
			}
		}
	}
	return hasChanges
}

func (data *RewriteRule) getClassMapVersion(ctx context.Context, id string) types.Int64 {
	for _, item := range data.Rules {
		if item.ClassMapId.ValueString() == id {
			return item.ClassMapVersion
		}
	}
	return types.Int64Null()
}

func (data *RewriteRule) updateVersions(ctx context.Context, state RewriteRule) {
	for r := range data.Rules {
		id := data.Rules[r].ClassMapId.ValueString()
		data.Rules[r].ClassMapVersion = state.getClassMapVersion(ctx, id)
	}
}
