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

// simple-store.go
package expr

import (
	"fmt"
	"slices"
	// "strings"
)

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

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

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

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

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

func (ctx *SimpleStore) Clone() ExprContext {
	clone := &SimpleStore{
		varStore:  CloneFilteredMap(ctx.varStore, filterRefName),
		funcStore: 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) ToString(opt FmtOpt) string {
	dict := ctx.ToDict()
	return dict.ToString(opt)
}

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

func (ctx *SimpleStore) funcsToDict(dict *DictType) *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.(Formatter); ok {
			(*dict)[name] = formatter.ToString(0)
		} else {
			(*dict)[name] = fmt.Sprintf("%v", value)
		}
	}
	return dict
}

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

func (ctx *SimpleStore) GetVar(varName string) (v any, exists bool) {
	v, exists = ctx.varStore[varName]
	return
}

func (ctx *SimpleStore) GetLast() (v any) {
	v = ctx.varStore["last"]
	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 := 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 ExprFunc, exists bool) {
	info, exists = ctx.funcStore[name]
	return
}

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

func (ctx *SimpleStore) RegisterFunc(name string, functor Functor, returnType string, params []ExprFuncParam)  (exprFunc ExprFunc, err error) {
	var info *funcInfo
	if info, err = 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 := GetLocalFuncInfo(ctx, name); exists {
		functor := info.Functor()
		result, err = functor.InvokeNamed(ctx, name, args)
	} else {
		err = fmt.Errorf("unknown function %s()", name)
	}
	return
}
