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

// operator-bool.go
package expr

import (
	"fmt"

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

//-------- NOT term

func newNotTerm(tk *scan.Token) (inst *scan.Term) {
	return &scan.Term{
		Tk:       *tk,
		Children: make([]*scan.Term, 0, 1),
		Position: scan.PosPrefix,
		Priority: scan.PriNot,
		EvalFunc: evalNot,
	}
}

func evalNot(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	var rightValue any

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

	if b, ok := boolean.ToBool(rightValue); ok {
		v = !b
	} else {
		err = opTerm.ErrIncompatiblePrefixPostfixType(rightValue)
	}
	return
}

//-------- AND term

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

func evalAnd(ctx kern.ExprContext, self *scan.Term) (v any, err error) {
	if kern.CtrlIsEnabled(ctx, kern.ControlBoolShortcut) {
		v, err = evalAndWithShortcut(ctx, self)
	} else {
		v, err = evalAndWithoutShortcut(ctx, self)
	}
	return
}

func evalAndWithoutShortcut(ctx kern.ExprContext, self *scan.Term) (v any, err error) {
	var leftValue, rightValue any
	var leftBool, rightBool bool
	var lok, rok bool

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

	leftBool, lok = boolean.ToBool(leftValue)
	rightBool, rok = boolean.ToBool(rightValue)

	if lok && rok {
		v = leftBool && rightBool
	} else {
		err = self.ErrIncompatibleTypes(leftValue, rightValue)
	}
	return
}

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

	if err = self.CheckOperands(); err != nil {
		return
	}

	if leftValue, err = self.Children[0].Compute(ctx); err != nil {
		return
	}

	if leftBool, lok := boolean.ToBool(leftValue); !lok {
		// err = fmt.Errorf("got %s as left operand type of 'AND' operator, it must be bool", expr.TypeName(leftValue))
		// return
		err = self.ErrIncompatibleType(leftValue, "left")
	} else if !leftBool {
		v = false
	} else if rightValue, err = self.Children[1].Compute(ctx); err == nil {
		if rightBool, rok := boolean.ToBool(rightValue); rok {
			v = rightBool
		} else {
			err = self.ErrIncompatibleTypes(leftValue, rightValue)
		}
	}
	return
}

//-------- OR term

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

func evalOr(ctx kern.ExprContext, self *scan.Term) (v any, err error) {
	if kern.CtrlIsEnabled(ctx, kern.ControlBoolShortcut) {
		v, err = evalOrWithShortcut(ctx, self)
	} else {
		v, err = evalOrWithoutShortcut(ctx, self)
	}
	return
}

func evalOrWithoutShortcut(ctx kern.ExprContext, self *scan.Term) (v any, err error) {
	var leftValue, rightValue any
	var leftBool, rightBool bool
	var lok, rok bool

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

	leftBool, lok = boolean.ToBool(leftValue)
	rightBool, rok = boolean.ToBool(rightValue)

	if lok && rok {
		v = leftBool || rightBool
	} else {
		err = self.ErrIncompatibleTypes(leftValue, rightValue)
	}
	return
}

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

	if err = self.CheckOperands(); err != nil {
		return
	}

	if leftValue, err = self.Children[0].Compute(ctx); err != nil {
		return
	}

	if leftBool, lok := boolean.ToBool(leftValue); !lok {
		err = fmt.Errorf("got %s as left operand type of 'OR' operator, it must be bool", kern.TypeName(leftValue))
		return
	} else if leftBool {
		v = true
	} else if rightValue, err = self.Children[1].Compute(ctx); err == nil {
		if rightBool, rok := boolean.ToBool(rightValue); rok {
			v = rightBool
		} else {
			err = self.ErrIncompatibleTypes(leftValue, rightValue)
		}
	}
	return
}

// init
func init() {
	scan.RegisterTermConstructor(sym.SymNot, newNotTerm)
	scan.RegisterTermConstructor(sym.SymAnd, newAndTerm)
	scan.RegisterTermConstructor(sym.SymOr, newOrTerm)
}
