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

// operator-prod.go
package expr

import (
	"errors"
	"strings"
)

//-------- multiply term

func newMultiplyTerm(tk *Token) (inst *term) {
	return &term{
		tk: *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priProduct,
		evalFunc: evalMultiply,
	}
}

func evalMultiply(ctx ExprContext, prodTerm *term) (v any, err error) {
	var leftValue, rightValue any

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

	if IsString(leftValue) && IsInteger(rightValue) {
		s, _ := leftValue.(string)
		n, _ := rightValue.(int64)
		v = strings.Repeat(s, int(n))
	} else if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
		if IsFloat(leftValue) || IsFloat(rightValue) {
			v = numAsFloat(leftValue) * numAsFloat(rightValue)
		} else if isFraction(leftValue) || isFraction(rightValue) {
			v, err = mulAnyFract(leftValue, rightValue)
		} else {
			leftInt, _ := leftValue.(int64)
			rightInt, _ := rightValue.(int64)
			v = leftInt * rightInt
		}
	} else {
		err = prodTerm.errIncompatibleTypes(leftValue, rightValue)
	}
	return
}

//-------- divide term

func newDivideTerm(tk *Token) (inst *term) {
	return &term{
		tk: *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priProduct,
		evalFunc: evalDivide,
	}
}

func evalDivide(ctx ExprContext, opTerm *term) (v any, err error) {
	var leftValue, rightValue any

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

	if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
		if IsFloat(leftValue) || IsFloat(rightValue) {
			d := numAsFloat(rightValue)
			if d == 0.0 {
				err = errors.New("division by zero")
			} else {
				v = numAsFloat(leftValue) / d
			}
		} else if isFraction(leftValue) || isFraction(rightValue) {
			v, err = divAnyFract(leftValue, rightValue)
		} else {
			leftInt, _ := leftValue.(int64)
			if rightInt, _ := rightValue.(int64); rightInt == 0 {
				err = errors.New("division by zero")
			} else {
				v = leftInt / rightInt
			}
		}
	} else {
		err = opTerm.errIncompatibleTypes(leftValue, rightValue)
	}
	return
}

//-------- divide as float term

func newDivideAsFloatTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priProduct,
		evalFunc: evalDivideAsFloat,
	}
}

func evalDivideAsFloat(ctx ExprContext, floatDivTerm *term) (v any, err error) {
	var leftValue, rightValue any

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

	if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
		d := numAsFloat(rightValue)
		if d == 0.0 {
			err = errors.New("division by zero")
		} else {
			v = numAsFloat(leftValue) / d
		}
	} else {
		err = floatDivTerm.errIncompatibleTypes(leftValue, rightValue)
	}
	return
}

//-------- reminder term

func newRemainderTerm(tk *Token) (inst *term) {
	return &term{
		tk: *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priProduct,
		evalFunc: evalReminder,
	}
}

func evalReminder(ctx ExprContext, ramainderTerm *term) (v any, err error) {
	var leftValue, rightValue any

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

	if IsInteger(leftValue) && IsInteger(rightValue) {
		rightInt, _ := rightValue.(int64)
		if rightInt == 0 {
			err = errors.New("division by zero")
		} else {
			leftInt, _ := leftValue.(int64)
			v = leftInt % rightInt
		}
	} else {
		err = ramainderTerm.errIncompatibleTypes(leftValue, rightValue)
	}
	return
}

// init
func init() {
	registerTermConstructor(SymStar, newMultiplyTerm)
	registerTermConstructor(SymSlash, newDivideTerm)
	registerTermConstructor(SymDotSlash, newDivideAsFloatTerm)
	registerTermConstructor(SymPercent, newRemainderTerm)
}
