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

// function.go
package expr

import (
	"fmt"
	"strconv"
	"strings"
)

// ---- Function template
type FuncTemplate func(ctx ExprContext, name string, args map[string]any) (result any, 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 paramFlags uint16

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

type funcParamInfo struct {
	name         string
	flags        paramFlags
	defaultValue any
}

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

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

func NewFuncParamFlagDef(name string, flags paramFlags, 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
}

// --- Functions

// funcInfo implements ExprFunc
type funcInfo struct {
	name         string
	minArgs      int
	maxArgs      int
	functor      Functor
	formalParams []ExprFuncParam
	returnType   string
}

func newFuncInfo(name string, functor Functor, returnType string, params []ExprFuncParam) (info *funcInfo, err error) {
	var minArgs = 0
	var maxArgs = 0
	for _, p := range params {
		if maxArgs == -1 {
			return nil, fmt.Errorf("no more params can be specified after the ellipsis symbol: %q", p.Name())
		}
		if p.IsDefault() || p.IsOptional() {
			maxArgs++
		} else if maxArgs == minArgs {
			minArgs++
			maxArgs++
		} else {
			return nil, fmt.Errorf("can't specify non-optional param after optional ones: %q", p.Name())
		}
		if p.IsRepeat() {
			minArgs--
			maxArgs = -1
		}
	}

	info = &funcInfo{
		name: name, minArgs: minArgs, maxArgs: maxArgs, functor: functor, returnType: returnType, formalParams: params,
	}
	functor.SetFunc(info)
	return info, nil
}

func (info *funcInfo) Params() []ExprFuncParam {
	return info.formalParams
}

func (info *funcInfo) ReturnType() string {
	return info.returnType
}

func (info *funcInfo) ToString(opt FmtOpt) string {
	var sb strings.Builder
	if len(info.Name()) == 0 {
		sb.WriteString("func")
	} else {
		sb.WriteString(info.Name())
	}
	sb.WriteByte('(')
	if info.formalParams != nil {
		for i, p := range info.formalParams {
			if i > 0 {
				sb.WriteString(", ")
			}
			sb.WriteString(p.Name())

			if p.IsDefault() {
				sb.WriteByte('=')
				if s, ok := p.DefaultValue().(string); ok {
					sb.WriteByte('"')
					sb.WriteString(s)
					sb.WriteByte('"')
				} else {
					sb.WriteString(fmt.Sprintf("%v", p.DefaultValue()))
				}
			}
		}
	}
	if info.maxArgs < 0 {
		sb.WriteString(" ...")
	}
	sb.WriteString("):")
	if len(info.returnType) > 0 {
		sb.WriteString(info.returnType)
	} else {
		sb.WriteString(TypeAny)
	}
	sb.WriteString("{}")
	return sb.String()
}

func (info *funcInfo) Name() string {
	return info.name
}

func (info *funcInfo) MinArgs() int {
	return info.minArgs
}

func (info *funcInfo) MaxArgs() int {
	return info.maxArgs
}

func (info *funcInfo) Functor() Functor {
	return info.functor
}

func (info *funcInfo) AllocContext(parentCtx ExprContext) (ctx ExprContext) {
	if defCtx := info.functor.GetDefinitionContext(); defCtx != nil {
		ctx = defCtx.Clone()
		ctx.SetParent(defCtx)
	} else {
		ctx = parentCtx.Clone()
		ctx.SetParent(parentCtx)
	}
	return
}

func (info *funcInfo) ParamSpec(paramName string) ExprFuncParam {
	for _, spec := range info.formalParams {
		if spec.Name() == paramName {
			return spec
		}
	}
	return nil
}

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
	}

	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, len(callTerm.children)-i)
					varArgs = append(varArgs, paramValue)
					varName = spec.Name()
				} else {
					actualParams[spec.Name()] = paramValue
				}
			} else {
				err = ErrTooManyParams(info.Name(), len(formalParams), len(callTerm.children))
				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 CallFunctionByTerm(parentCtx ExprContext, name string, callTerm *term) (result any, err error) {
	var actualParams map[string]any
	if info, exists := GetFuncInfo(parentCtx, 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 := GetFuncInfo(parentCtx, 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 := GetFuncInfo(parentCtx, 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
}
