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

// list-iterator.go
package expr

import (
	"fmt"
	"io"
	"slices"

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

type ArrayIterator struct {
	a     *array.ArrayType
	count int64
	index int64
	start int64
	stop  int64
	step  int64
}

func NewArrayIteratorFromArray(a *array.ArrayType, args []any) (it *ArrayIterator) {
	var argc int = 0
	listLen := int64(len(([]any)(*a)))
	if args != nil {
		argc = len(args)
	}
	it = &ArrayIterator{a: a, count: 0, index: -1, start: 0, stop: listLen - 1, step: 1}
	if argc >= 1 {
		if i, err := types.ToGoInt64(args[0], "start index"); err == nil {
			if i < 0 {
				i = listLen + i
			}
			it.start = i
		}
		if argc >= 2 {
			if i, err := types.ToGoInt64(args[1], "stop index"); err == nil {
				if i < 0 {
					i = listLen + i
				}
				it.stop = i
			}
			if argc >= 3 {
				if i, err := types.ToGoInt64(args[2], "step"); err == nil {
					if i < 0 {
						i = -i
					}
					if it.start > it.stop {
						it.step = -i
					} else {
						it.step = i
					}
				}
			}
		}
	}
	it.index = it.start - it.step
	return
}

func NewArrayIterator(a []any) (it *ArrayIterator) {
	it = &ArrayIterator{a: (*array.ArrayType)(&a), count: 0, index: -1, start: 0, stop: int64(len(a)) - 1, step: 1}
	return
}

func (it *ArrayIterator) String() string {
	var l = int64(0)
	if it.a != nil {
		l = int64(len(*it.a))
	}
	return fmt.Sprintf("$([#%d])", l)
}

func (it *ArrayIterator) TypeName() string {
	return "ArrayIterator"
}

func (it *ArrayIterator) HasOperation(name string) bool {
	//yes := name == expr.NextName || name == expr.ResetName || name == expr.IndexName || name == expr.CountName || name == expr.CurrentName
	yes := slices.Contains([]string{kern.NextName, kern.ResetName, kern.IndexName, kern.CountName, kern.CurrentName}, name)
	return yes
}

func (it *ArrayIterator) CallOperation(name string, args map[string]any) (v any, err error) {
	switch name {
	case kern.NextName:
		v, err = it.Next()
	case kern.ResetName:
		err = it.Reset()
	case kern.CleanName:
		err = it.Clean()
	case kern.IndexName:
		v = int64(it.Index())
	case kern.CurrentName:
		v, err = it.Current()
	case kern.CountName:
		v = it.count
	default:
		err = kern.ErrNoOperation(name)
	}
	return
}

func (it *ArrayIterator) Current() (item any, err error) {
	a := *(it.a)
	if it.start <= it.stop {
		if it.stop < int64(len(a)) && it.index >= it.start && it.index <= it.stop {
			item = a[it.index]
		} else {
			err = io.EOF
		}
	} else {
		if it.start < int64(len(a)) && it.index >= it.stop && it.index <= it.start {
			item = a[it.index]
		} else {
			err = io.EOF
		}

	}
	return
}

func (it *ArrayIterator) Next() (item any, err error) {
	it.index += it.step
	if item, err = it.Current(); err != io.EOF {
		it.count++
	}
	return
}

func (it *ArrayIterator) Index() int64 {
	return it.index
}

func (it *ArrayIterator) Count() int64 {
	return it.count
}

func (it *ArrayIterator) Reset() error {
	it.index = it.start - it.step
	it.count = 0
	return nil
}

func (it *ArrayIterator) Clean() error {
	return nil
}
