// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.

// compare.go
package types

import (
	"reflect"

	"git.portale-stac.it/go-pkg/expr/types/array"
	"git.portale-stac.it/go-pkg/expr/types/boolean"
	"git.portale-stac.it/go-pkg/expr/types/dict"
	"git.portale-stac.it/go-pkg/expr/types/float"
	"git.portale-stac.it/go-pkg/expr/types/fract"
	"git.portale-stac.it/go-pkg/expr/types/list"
	"git.portale-stac.it/go-pkg/expr/types/str"
)

func Equal(value1, value2 any) (equal bool) {
	if value1 == nil && value2 == nil {
		equal = true
	} else if value1 == nil || value2 == nil {
		equal = false
	} else if boolean.IsBool(value1) && boolean.IsBool(value2) {
		equal = value1.(bool) == value2.(bool)
	} else if array.IsArray(value1) && array.IsArray(value2) {
		ls1 := value1.(*array.ArrayType)
		ls2 := value2.(*array.ArrayType)
		equal = ls1.Equals(*ls2)
	} else if dict.IsDict(value1) && dict.IsDict(value2) {
		d1 := value1.(*dict.DictType)
		d2 := value2.(*dict.DictType)
		equal = d1.Equals(*d2)
	} else if list.IsLinkedList(value1) && list.IsLinkedList(value2) {
		ll1 := value1.(*list.LinkedList)
		ll2 := value2.(*list.LinkedList)
		equal = ll1.Equals(ll2)
	} else if IsInteger(value1) && IsInteger(value2) {
		equal = value1.(int64) == value2.(int64)
	} else if str.IsString(value1) && str.IsString(value2) {
		equal = value1.(string) == value2.(string)
	} else if float.IsFloat(value1) && float.IsFloat(value2) {
		equal = value1.(float64) == value2.(float64)
	} else if IsNumOrFract(value1) && IsNumOrFract(value2) {
		if eq, err := fract.CmpAnyFract(value1, value2); err == nil {
			equal = eq == 0
		}
	} else if !reflect.DeepEqual(value1, value2) {
		equal = false
	}
	return
}
