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

// function.go
package expr

import (
	"fmt"
	"strings"
)

// ---- Function template
type FuncTemplate func(ctx ExprContext, name string, args []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 newUnnamedFuncInfo(functor Functor, returnType string, params []ExprFuncParam) (info *funcInfo, err error) {
// 	return newFuncInfo("unnamed", functor, returnType, params)
// }

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) PrepareCall(parentCtx ExprContext, name string, varActualParams *[]any) (ctx ExprContext, err error) {
	passedCount := len(*varActualParams)
	if info.MinArgs() > passedCount {
		err = ErrTooFewParams(name, info.MinArgs(), info.MaxArgs(), passedCount)
	}

	for i := passedCount; i < len(info.formalParams); i++ {
		p := info.formalParams[i]
		if !p.IsDefault() {
			break
		}
		*varActualParams = append(*varActualParams, p.DefaultValue())
	}
	if err == nil && info.MaxArgs() >= 0 && info.MaxArgs() < len(*varActualParams) {
		err = ErrTooMuchParams(name, info.MaxArgs(), len(*varActualParams))
	}

	if err == nil {
		ctx = info.AllocContext(parentCtx)
	}
	return
}

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

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