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

// operator-sum.go
package expr

import (
	"fmt"
	"slices"

	"git.portale-stac.it/go-pkg/expr/kern"
	"git.portale-stac.it/go-pkg/expr/scan"
	"git.portale-stac.it/go-pkg/expr/sym"
	"git.portale-stac.it/go-pkg/expr/types"
	"git.portale-stac.it/go-pkg/expr/types/array"
	"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"
)

//-------- plus term

func newPlusTerm(tk *scan.Token) (inst *scan.Term) {
	return &scan.Term{
		Tk:       *tk,
		Children: make([]*scan.Term, 0, 2),
		Position: scan.PosInfix,
		Priority: scan.PriSum,
		EvalFunc: evalPlus,
	}
}

func sumValues(plusTerm *scan.Term, leftValue, rightValue any) (v any, err error) {
	if (str.IsString(leftValue) && types.IsNumberString(rightValue)) || (str.IsString(rightValue) && types.IsNumberString(leftValue)) {
		v = fmt.Sprintf("%v%v", leftValue, rightValue)
	} else if types.IsNumber(leftValue) && types.IsNumber(rightValue) {
		if float.IsFloat(leftValue) || float.IsFloat(rightValue) {
			v = types.NumAsFloat(leftValue) + types.NumAsFloat(rightValue)
		} else {
			leftInt, _ := leftValue.(int64)
			rightInt, _ := rightValue.(int64)
			v = leftInt + rightInt
		}
	} else if array.IsArray(leftValue) && array.IsArray(rightValue) {
		var leftList, rightList *array.ArrayType
		leftList, _ = leftValue.(*array.ArrayType)
		rightList, _ = rightValue.(*array.ArrayType)

		sumList := make(array.ArrayType, 0, len(*leftList)+len(*rightList))
		sumList = append(sumList, *leftList...)
		sumList = append(sumList, *rightList...)
		v = &sumList
	} else if list.IsLinkedList(leftValue) && list.IsLinkedList(rightValue) {
		lsLeft := leftValue.(*list.LinkedList)
		lsRight := rightValue.(*list.LinkedList)
		ls := lsLeft.Clone()
		ls.SeqClonePushBack(NewLinkedListIterator(lsRight, nil))
		v = ls
	} else if (fract.IsFraction(leftValue) && types.IsNumber(rightValue)) || (fract.IsFraction(rightValue) && types.IsNumber(leftValue)) {
		if float.IsFloat(leftValue) || float.IsFloat(rightValue) {
			v = types.NumAsFloat(leftValue) + types.NumAsFloat(rightValue)
		} else {
			v, err = fract.SumAnyFract(leftValue, rightValue)
		}
	} else if dict.IsDict(leftValue) && dict.IsDict(rightValue) {
		leftDict, _ := leftValue.(*dict.DictType)
		rightDict, _ := rightValue.(*dict.DictType)
		clone := leftDict.Clone()
		c := clone.(*dict.DictType)
		c.Merge(rightDict)
		v = c
	} else if fract.IsFraction(leftValue) && fract.IsFraction(rightValue) {
		v, err = fract.SumAnyFract(leftValue, rightValue)
	} else {
		err = plusTerm.ErrIncompatibleTypes(leftValue, rightValue)
	}
	return v, err
}

func evalPlus(ctx kern.ExprContext, plusTerm *scan.Term) (v any, err error) {
	var leftValue, rightValue any

	if leftValue, rightValue, err = plusTerm.EvalInfix(ctx); err != nil {
		return
	}

	return sumValues(plusTerm, leftValue, rightValue)
}

//-------- minus term

func newMinusTerm(tk *scan.Token) (inst *scan.Term) {
	return &scan.Term{
		Tk:       *tk,
		Children: make([]*scan.Term, 0, 2),
		Position: scan.PosInfix,
		Priority: scan.PriSum,
		EvalFunc: evalMinus,
	}
}

func diffValues(minusTerm *scan.Term, leftValue, rightValue any) (v any, err error) {
	if types.IsNumOrFract(leftValue) && types.IsNumOrFract(rightValue) {
		if float.IsFloat(leftValue) || float.IsFloat(rightValue) {
			v = types.NumAsFloat(leftValue) - types.NumAsFloat(rightValue)
		} else if fract.IsFraction(leftValue) || fract.IsFraction(rightValue) {
			v, err = fract.SubAnyFract(leftValue, rightValue)
		} else {
			leftInt, _ := leftValue.(int64)
			rightInt, _ := rightValue.(int64)
			v = leftInt - rightInt
		}
	} else if array.IsArray(leftValue) && array.IsArray(rightValue) {
		leftList, _ := leftValue.(*array.ArrayType)
		rightList, _ := rightValue.(*array.ArrayType)
		diffList := make(array.ArrayType, 0, len(*leftList)-len(*rightList))
		for _, item := range *leftList {
			if slices.Index(*rightList, item) < 0 {
				diffList = append(diffList, item)
			}
		}
		v = &diffList
	} else {
		err = minusTerm.ErrIncompatibleTypes(leftValue, rightValue)
	}
	return
}

func evalMinus(ctx kern.ExprContext, minusTerm *scan.Term) (v any, err error) {
	var leftValue, rightValue any

	if leftValue, rightValue, err = minusTerm.EvalInfix(ctx); err != nil {
		return
	}

	return diffValues(minusTerm, leftValue, rightValue)
}

// init
func init() {
	scan.RegisterTermConstructor(sym.SymPlus, newPlusTerm)
	scan.RegisterTermConstructor(sym.SymMinus, newMinusTerm)
}
