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

// simple-store.go
package expr

import (
	"fmt"

	"git.portale-stac.it/go-pkg/expr/kern"
	"git.portale-stac.it/go-pkg/expr/types"
	"git.portale-stac.it/go-pkg/expr/util"
	// "strings"
)

type SimpleStore struct {
	global    kern.ExprContext
	parent    kern.ExprContext
	varStore  map[string]any
	funcStore map[string]kern.ExprFunc
}

func NewSimpleStore() *SimpleStore {
	global := InitGlobal()
	ctx := &SimpleStore{
		global:    global,
		varStore:  make(map[string]any),
		funcStore: make(map[string]kern.ExprFunc),
	}
	return ctx
}

func NewSimpleStoreWithoutGlobalContext() *SimpleStore {
	ctx := &SimpleStore{
		varStore:  make(map[string]any),
		funcStore: make(map[string]kern.ExprFunc),
	}
	return ctx
}

func (ss *SimpleStore) Init() {
	ss.varStore = make(map[string]any)
	ss.funcStore = make(map[string]kern.ExprFunc)
}

func filterRefName(name string) bool { return name[0] != '@' }

//func filterPrivName(name string) bool { return name[0] != '_' }

func (ctx *SimpleStore) SetParent(parentCtx kern.ExprContext) {
	ctx.parent = parentCtx
}

func (ctx *SimpleStore) GetParent() kern.ExprContext {
	return ctx.parent
}

func (ctx *SimpleStore) GetGlobal() (globalCtx kern.ExprContext) {
	return ctx.global
}

func (ctx *SimpleStore) Clone() kern.ExprContext {
	clone := &SimpleStore{
		global:    ctx.global,
		varStore:  util.CloneFilteredMap(ctx.varStore, filterRefName),
		funcStore: util.CloneFilteredMap(ctx.funcStore, filterRefName),
	}
	return clone
}

// func (ctx *SimpleStore) Merge(src ExprContext) {
// 	for _, name := range src.EnumVars(filterRefName) {
// 		ctx.varStore[name], _ = src.GetVar(name)
// 	}
// 	for _, name := range src.EnumFuncs(filterRefName) {
// 		ctx.funcStore[name], _ = src.GetFuncInfo(name)
// 	}
// }

func (ctx *SimpleStore) ToDict() any {
	return types.ContextToDict(ctx)
}

func (ctx *SimpleStore) ToString(opt kern.FmtOpt) string {
	return types.ContextToString(ctx, opt)
}

// func (ctx *SimpleStore) ToString(opt kern.FmtOpt) string {
// 	dict := ctx.ToDict()
// 	return dict.ToString(opt)
// }

// func (ctx *SimpleStore) varsToDict(dict *kern.DictType) *kern.DictType {
// 	names := ctx.EnumVars(nil)
// 	slices.Sort(names)
// 	for _, name := range ctx.EnumVars(nil) {
// 		value, _ := ctx.GetVar(name)
// 		if f, ok := value.(kern.Formatter); ok {
// 			(*dict)[name] = f.ToString(0)
// 		} else if _, ok = value.(kern.Functor); ok {
// 			(*dict)[name] = "func(){}"
// 		} else {
// 			(*dict)[name] = fmt.Sprintf("%v", value)
// 		}
// 	}
// 	return dict
// }

// func (ctx *SimpleStore) funcsToDict(dict *kern.DictType) *kern.DictType {
// 	names := ctx.EnumFuncs(func(name string) bool { return true })
// 	slices.Sort(names)
// 	for _, name := range names {
// 		value, _ := ctx.GetFuncInfo(name)
// 		if formatter, ok := value.(kern.Formatter); ok {
// 			(*dict)[name] = formatter.ToString(0)
// 		} else {
// 			(*dict)[name] = fmt.Sprintf("%v", value)
// 		}
// 	}
// 	return dict
// }

// func (ctx *SimpleStore) ToDict() (dict *kern.DictType) {
// 	dict = kern.MakeDict()
// 	(*dict)["variables"] = ctx.varsToDict(kern.MakeDict())
// 	(*dict)["functions"] = ctx.funcsToDict(kern.MakeDict())
// 	return
// }

func (ctx *SimpleStore) GetVar(varName string) (value any, exists bool) {
	if value, exists = ctx.varStore[varName]; !exists && ctx.global != nil {
		value, exists = ctx.global.GetVar(varName)
	}

	return
}

func (ctx *SimpleStore) GetLast() (v any) {
	v = ctx.varStore[kern.ControlLastResult]
	return
}

func (ctx *SimpleStore) UnsafeSetVar(varName string, value any) {
	// fmt.Printf("[%p] setVar(%v, %v)\n", ctx, varName, value)
	ctx.varStore[varName] = value
}

func (ctx *SimpleStore) SetVar(varName string, value any) {
	// fmt.Printf("[%p] SetVar(%v, %v)\n", ctx, varName, value)
	if allowedValue, ok := util.FromGenericAny(value); ok {
		ctx.varStore[varName] = allowedValue
	} else {
		panic(fmt.Errorf("unsupported type %T of value %v", value, value))
	}
}

func (ctx *SimpleStore) EnumVars(acceptor func(name string) (accept bool)) (varNames []string) {
	varNames = make([]string, 0)
	for name := range ctx.varStore {
		if acceptor != nil {
			if acceptor(name) {
				varNames = append(varNames, name)
			}
		} else {
			varNames = append(varNames, name)
		}
	}
	return
}

func (ctx *SimpleStore) VarCount() int {
	return len(ctx.varStore)
}

func (ctx *SimpleStore) DeleteVar(varName string) {
	delete(ctx.varStore, varName)
}

func (ctx *SimpleStore) GetFuncInfo(name string) (info kern.ExprFunc, exists bool) {
	info, exists, _ = ctx.GetFuncInfoAndOwner(name)
	return
}

func (ctx *SimpleStore) GetFuncInfoAndOwner(name string) (info kern.ExprFunc, exists bool, ownerCtx kern.ExprContext) {
	if len(name) > 0 {
		if info, exists = ctx.GetLocalFuncInfo(name); exists {
			ownerCtx = ctx
		} else if globalCtx := ctx.GetGlobal(); globalCtx != nil {
			if info, exists = globalCtx.GetFuncInfo(name); exists {
				ownerCtx = globalCtx
			}
		}
	}
	return
}

func (ctx *SimpleStore) GetLocalFuncInfo(name string) (info kern.ExprFunc, exists bool) {
	var v any
	if len(name) > 0 {
		if v, exists = ctx.GetVar(name); exists && kern.IsFunctor(v) {
			f, _ := v.(kern.Functor)
			info = f.GetFunc()
		} else {
			info, exists = ctx.funcStore[name]
		}
	}
	return
}

func (ctx *SimpleStore) RegisterFuncInfo(info kern.ExprFunc) {
	ctx.funcStore[info.Name()], _ = info.(*kern.FuncInfo)
}

func (ctx *SimpleStore) RegisterFunc(name string, functor kern.Functor, returnType string, params []kern.ExprFuncParam) (exprFunc kern.ExprFunc, err error) {
	var info *kern.FuncInfo
	if info, err = kern.NewFuncInfo(name, functor, returnType, params); err == nil {
		ctx.funcStore[name] = info
		exprFunc = info
	}
	return
}

func (ctx *SimpleStore) EnumFuncs(acceptor func(name string) (accept bool)) (funcNames []string) {
	funcNames = make([]string, 0)
	for name := range ctx.funcStore {
		if acceptor != nil {
			if acceptor(name) {
				funcNames = append(funcNames, name)
			}
		} else {
			funcNames = append(funcNames, name)
		}
	}
	return
}

func (ctx *SimpleStore) FuncCount() int {
	return len(ctx.funcStore)
}

func (ctx *SimpleStore) DeleteFunc(funcName string) {
	delete(ctx.funcStore, funcName)
}

func (ctx *SimpleStore) Call(name string, args map[string]any) (result any, err error) {
	if info, exists := ctx.GetLocalFuncInfo(name); exists {
		functor := info.Functor()
		result, err = functor.InvokeNamed(ctx, name, args)
	} else {
		err = fmt.Errorf("unknown function %s()", name)
	}
	return
}
