// 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"
)

//-------- 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 (kern.IsString(leftValue) && kern.IsNumberString(rightValue)) || (kern.IsString(rightValue) && kern.IsNumberString(leftValue)) {
		v = fmt.Sprintf("%v%v", leftValue, rightValue)
	} else if kern.IsNumber(leftValue) && kern.IsNumber(rightValue) {
		if kern.IsFloat(leftValue) || kern.IsFloat(rightValue) {
			v = kern.NumAsFloat(leftValue) + kern.NumAsFloat(rightValue)
		} else {
			leftInt, _ := leftValue.(int64)
			rightInt, _ := rightValue.(int64)
			v = leftInt + rightInt
		}
	} else if kern.IsList(leftValue) && kern.IsList(rightValue) {
		var leftList, rightList *kern.ListType
		leftList, _ = leftValue.(*kern.ListType)
		rightList, _ = rightValue.(*kern.ListType)

		sumList := make(kern.ListType, 0, len(*leftList)+len(*rightList))
		sumList = append(sumList, *leftList...)
		sumList = append(sumList, *rightList...)
		v = &sumList
	} else if (kern.IsFraction(leftValue) && kern.IsNumber(rightValue)) || (kern.IsFraction(rightValue) && kern.IsNumber(leftValue)) {
		if kern.IsFloat(leftValue) || kern.IsFloat(rightValue) {
			v = kern.NumAsFloat(leftValue) + kern.NumAsFloat(rightValue)
		} else {
			v, err = kern.SumAnyFract(leftValue, rightValue)
		}
	} else if kern.IsDict(leftValue) && kern.IsDict(rightValue) {
		leftDict, _ := leftValue.(*kern.DictType)
		rightDict, _ := rightValue.(*kern.DictType)
		c := leftDict.Clone()
		c.Merge(rightDict)
		v = c
	} else if kern.IsFraction(leftValue) && kern.IsFraction(rightValue) {
		v, err = kern.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 kern.IsNumOrFract(leftValue) && kern.IsNumOrFract(rightValue) {
		if kern.IsFloat(leftValue) || kern.IsFloat(rightValue) {
			v = kern.NumAsFloat(leftValue) - kern.NumAsFloat(rightValue)
		} else if kern.IsFraction(leftValue) || kern.IsFraction(rightValue) {
			v, err = kern.SubAnyFract(leftValue, rightValue)
		} else {
			leftInt, _ := leftValue.(int64)
			rightInt, _ := rightValue.(int64)
			v = leftInt - rightInt
		}
	} else if kern.IsList(leftValue) && kern.IsList(rightValue) {
		leftList, _ := leftValue.(*kern.ListType)
		rightList, _ := rightValue.(*kern.ListType)
		diffList := make(kern.ListType, 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(scan.SymPlus, newPlusTerm)
	scan.RegisterTermConstructor(scan.SymMinus, newMinusTerm)
}
