package views

import (
	"code.fbi.h-da.de/cocsn/gosdn/cmd/gosdn-tview/app"
	commands "code.fbi.h-da.de/cocsn/gosdn/cmd/gosdn-tview/grpc"
	"github.com/rivo/tview"
	"google.golang.org/grpc"
)

//ConsoleLogView is an application view to create a view for goSDN log messages
type ConsoleLogView struct {
	title          string
	consoleLogView *tview.TextView
}

//NewConsoleLogView creates a new ConsoleLogView
func NewConsoleLogView(title string, app *app.App, conn *grpc.ClientConn) *ConsoleLogView {
	clv := &ConsoleLogView{
		consoleLogView: tview.NewTextView(),
		title:          title,
	}
	clv.consoleLogView.
		SetDynamicColors(true).
		SetTextAlign(tview.AlignLeft).
		SetScrollable(true).
		SetRegions(true).
		SetBorder(true).
		SetTitle(clv.title)

	commands.GoSDNLogStream(app, conn, clv.consoleLogView)

	return clv
}

//GetContent returns the tview.Primitive belonging to the ConsoleLogView
func (clv *ConsoleLogView) GetContent() tview.Primitive {
	return clv.consoleLogView
}

//Write implements the io.Writer interface via tview.textView.Write().
//Gets a string and converts it to a byte slice and writes it to the textView
//buffer
func (clv *ConsoleLogView) Write(s string) (n int, err error) {
	b := []byte(s)
	return clv.consoleLogView.Write(b)
}

//GetTitle returns the title of the specific view
func (clv *ConsoleLogView) GetTitle() string {
	return clv.title
}