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

// function.go
package kern

import (
	"fmt"
	"strconv"
)

// ---- Function templates
type FuncTemplate func(ctx ExprContext, name string, args map[string]any) (result any, err error)

type DeepFuncTemplate func(a, b any) (eq bool, err error)

// ---- Common functor definition
type BaseFunctor struct {
	info ExprFunc
}

func (functor *BaseFunctor) ToString(opt FmtOpt) (s string) {
	if functor.info != nil {
		s = functor.info.ToString(opt)
	} else {
		s = "func(){}"
	}
	return s
}

func (functor *BaseFunctor) GetParams() (params []ExprFuncParam) {
	if functor.info != nil {
		return functor.info.Params()
	} else {
		return []ExprFuncParam{}
	}
}

func (functor *BaseFunctor) SetFunc(info ExprFunc) {
	functor.info = info
}

func (functor *BaseFunctor) GetFunc() ExprFunc {
	return functor.info
}

func (functor *BaseFunctor) GetDefinitionContext() ExprContext {
	return nil
}

// ---- Function Parameters
type FuncParamFlags uint16

const (
	PfDefault FuncParamFlags = 1 << iota
	PfOptional
	PfRepeat
)

type funcParamInfo struct {
	name         string
	flags        FuncParamFlags
	defaultValue any
}

func NewFuncParam(name string) ExprFuncParam {
	return &funcParamInfo{name: name}
}

func NewFuncParamFlag(name string, flags FuncParamFlags) ExprFuncParam {
	return &funcParamInfo{name: name, flags: flags}
}

func NewFuncParamFlagDef(name string, flags FuncParamFlags, defValue any) *funcParamInfo {
	return &funcParamInfo{name: name, flags: flags, defaultValue: defValue}
}

func (param *funcParamInfo) Name() string {
	return param.name
}

func (param *funcParamInfo) Type() string {
	return TypeAny
}

func (param *funcParamInfo) IsDefault() bool {
	return (param.flags & PfDefault) != 0
}

func (param *funcParamInfo) IsOptional() bool {
	return (param.flags & PfOptional) != 0
}

func (param *funcParamInfo) IsRepeat() bool {
	return (param.flags & PfRepeat) != 0
}

func (param *funcParamInfo) DefaultValue() any {
	return param.defaultValue
}

func initActualParams(ctx ExprContext, info ExprFunc, callTerm Term) (actualParams map[string]any, err error) {
	var varArgs []any
	var varName string

	namedParamsStarted := false

	formalParams := info.Params()
	actualParams = make(map[string]any, len(formalParams))
	if callTerm == nil {
		return
	}

	childCount := callTerm.GetChildCount()
	for i := range childCount {
		tree := callTerm.GetChild(i)
		// for i, tree := range callTerm.Children() {
		var paramValue any
		paramCtx := ctx.Clone()
		if paramValue, err = tree.Compute(paramCtx); err != nil {
			break
		}
		if paramName, namedParam := GetAssignVarName(tree); namedParam {
			if info.ParamSpec(paramName) == nil {
				err = fmt.Errorf("%s(): unknown param %q", info.Name(), paramName)
				break
			}
			actualParams[paramName] = paramValue
			namedParamsStarted = true
		} else if !namedParamsStarted {
			if varArgs != nil {
				varArgs = append(varArgs, paramValue)
			} else if i < len(formalParams) {
				spec := formalParams[i]
				if spec.IsRepeat() {
					varArgs = make([]any, 0, childCount-i)
					varArgs = append(varArgs, paramValue)
					varName = spec.Name()
				} else {
					actualParams[spec.Name()] = paramValue
				}
			} else {
				err = ErrTooManyParams(info.Name(), len(formalParams), childCount)
				break
			}
		} else {
			err = fmt.Errorf("%s(): positional param nr %d not allowed after named params", info.Name(), i+1)
			break
		}
	}
	if err == nil {
		if varArgs != nil {
			actualParams[varName] = varArgs
		}
	}
	return
}

// func (info *funcInfo) PrepareCall(name string, actualParams map[string]any) (err error) {
// 	passedCount := len(actualParams)
// 	if info.MinArgs() > passedCount {
// 		err = ErrTooFewParams(name, info.MinArgs(), info.MaxArgs(), passedCount)
// 		return
// 	}

// 	if passedCount < len(info.formalParams) {
// 		for _, p := range info.formalParams {
// 			if _, exists := actualParams[p.Name()]; !exists {
// 				if !p.IsDefault() {
// 					break
// 				}
// 				if p.IsRepeat() {
// 					varArgs := make([]any, 1)
// 					varArgs[0] = p.DefaultValue()
// 					actualParams[p.Name()] = varArgs
// 				} else {
// 					actualParams[p.Name()] = p.DefaultValue()
// 				}
// 			}
// 		}
// 	}

// 	if info.MaxArgs() >= 0 && info.MaxArgs() < len(actualParams) {
// 		err = ErrTooManyParams(name, info.MaxArgs(), len(actualParams))
// 	}
// 	return
// }

// ----- Call a function ---

// func getAssignVarName(t *term) (name string, ok bool) {
// 	if ok = t.symbol() == SymEqual; ok {
// 		name = t.children[0].source()
// 	}
// 	return
// }

func GetAssignVarName(t Term) (name string, ok bool) {
	if ok = t.IsAssign(); ok {
		name = t.GetChildSource(0)
	}
	return
}

func CallFunctionByTerm(parentCtx ExprContext, name string, callTerm Term) (result any, err error) {
	var actualParams map[string]any
	if info, exists := parentCtx.GetFuncInfo(name); exists {
		if actualParams, err = initActualParams(parentCtx, info, callTerm); err == nil {
			ctx := info.AllocContext(parentCtx)
			if err = info.PrepareCall(name, actualParams); err == nil {
				functor := info.Functor()
				result, err = functor.InvokeNamed(ctx, name, actualParams)
				exportObjectsToParent(ctx)
			}
		}
	} else {
		err = fmt.Errorf("unknown function %s()", name)
	}
	return
}

func CallFunctionByArgs(parentCtx ExprContext, name string, args []any) (result any, err error) {
	var actualParams map[string]any
	if info, exists := parentCtx.GetFuncInfo(name); exists {
		functor := info.Functor()
		actualParams = BindActualParams(functor, args)
		ctx := info.AllocContext(parentCtx)
		if err = info.PrepareCall(name, actualParams); err == nil {
			result, err = functor.InvokeNamed(ctx, name, actualParams)
			exportObjectsToParent(ctx)
		}
	} else {
		err = fmt.Errorf("unknown function %s()", name)
	}
	return
}

func CallFunctionByParams(parentCtx ExprContext, name string, actualParams map[string]any) (result any, err error) {
	//var actualParams map[string]any
	if info, exists := parentCtx.GetFuncInfo(name); exists {
		functor := info.Functor()
		ctx := info.AllocContext(parentCtx)
		if err = info.PrepareCall(name, actualParams); err == nil {
			result, err = functor.InvokeNamed(ctx, name, actualParams)
			exportObjectsToParent(ctx)
		}
	} else {
		err = fmt.Errorf("unknown function %s()", name)
	}
	return
}

func GetParam(args map[string]any, paramName string, paramNum int) (value any, exists bool) {
	if value, exists = args[paramName]; !exists {
		if paramNum > 0 && paramNum <= len(args) {
			value, exists = args["arg"+strconv.Itoa(paramNum)]
		}
	}
	return
}

func BindActualParams(functor Functor, args []any) (actualParams map[string]any) {
	formalParams := functor.GetParams()
	actualParams = make(map[string]any, len(args))
	for i, arg := range args {
		if i < len(formalParams) {
			actualParams[formalParams[i].Name()] = arg
		} else {
			actualParams["arg"+strconv.Itoa(i+1)] = arg
		}
	}
	return
}
