package telnet

import (
	"context"
	"log"

	"github.com/ThreeDotsLabs/watermill"
	"github.com/ThreeDotsLabs/watermill/message"
)

type TELNETParser struct {
	s      message.Subscriber
	c      context.Context
	logger watermill.LoggerAdapter
	to     TELNETOptions
}

func (tp *TELNETParser) Handle() {
	messages, err := tp.s.Subscribe(tp.c, "telnet.raw")
	if err != nil {
		log.Fatalln("couldn't subscribe to telnet.raw")
	}

	for message := range messages {
		fields := make(watermill.LogFields)
		fields["body"] = string(message.Payload)
		fields["correlation_id"] = string(message.Metadata["correlation_id"])
		tp.logger.Trace("received raw TCP line", fields)
		message.Ack()
	}

}

func NewTELNETParser(c context.Context, s message.Subscriber, wml watermill.LoggerAdapter) *TELNETParser {
	return &TELNETParser{
		c: c,
		s: s,
		logger: wml.With(
			watermill.LogFields{
				"name": "TELNETParser",
			},
		),
	}
}