package rpcclient

import (
	"context"

	"git.marketally.com/tutus-one/tutus-chain/pkg/tutusrpc"
)

// InternalHook is a function signature that is required to create a local client
// (see NewInternal). It performs registration of local client's event channel
// and returns a request handler function.
type InternalHook func(context.Context, chan<- tutusrpc.Notification) func(*tutusrpc.Request) (*tutusrpc.Response, error)

// Internal is an experimental "local" client that does not connect to RPC via
// network. It's made for deeply integrated applications like NeoFS that have
// blockchain running in the same process, so use it only if you know what you're
// doing. It provides the same interface WSClient does.
type Internal struct {
	WSClient

	events chan tutusrpc.Notification
}

// NewInternal creates an instance of internal client. It accepts a method
// provided by RPC server.
func NewInternal(ctx context.Context, register InternalHook) (*Internal, error) {
	c := &Internal{
		WSClient: WSClient{
			Client: Client{},

			shutdown:      make(chan struct{}),
			readerDone:    make(chan struct{}),
			writerDone:    make(chan struct{}),
			subscriptions: make(map[string]notificationReceiver),
			receivers:     make(map[any][]string),
		},
		events: make(chan tutusrpc.Notification),
	}

	err := initClient(ctx, &c.Client, "localhost:0", Options{})
	if err != nil {
		return nil, err // Can't really happen for internal client.
	}
	c.cli = nil
	go c.eventLoop()
	// c.ctx is inherited from ctx in fact (see initClient).
	c.requestF = register(c.ctx, c.events) //nolint:contextcheck // Non-inherited new context, use function like `context.WithXXX` instead
	return c, nil
}

func (c *Internal) eventLoop() {
eventloop:
	for {
		select {
		case <-c.ctx.Done():
			break eventloop
		case <-c.shutdown:
			break eventloop
		case ev := <-c.events:
			ntf := Notification{Type: ev.Event}
			if len(ev.Payload) > 0 {
				ntf.Value = ev.Payload[0]
			}
			c.notifySubscribers(ntf)
		}
	}
	close(c.readerDone)
	c.ctxCancel()
	// ctx is cancelled, server is notified and will close c.events soon.
	for {
		_, ok := <-c.events
		if !ok {
			break
		}
	}
}
