package main

import (
	gc "github.com/rthornton128/goncurses"
)

const URLLogin = "https://auth.adlin.nemunai.re/login"

func goLogin(stdscr *gc.Window, in chan gc.Key) (string, string, bool) {
	username, password := login(stdscr, in)

	validator := make(chan bool)
	validator_err := make(chan error)
	go func(username, password string, progress chan bool, err chan error) {
		st, errm := checkLogin(username, password)
		progress <- st
		err <- errm
	}(username, password, validator, validator_err)

	if connection(stdscr, in, validator, validator_err) {
		e := <- validator_err
		return username, e.Error(), true
	} else {
		return goLogin(stdscr, in)
	}
}

func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	// Set main properties
	gc.Cursor(0)
	gc.StartColor()
	gc.Raw(true)
	gc.Echo(false)
	stdscr.Keypad(true)

	// Define colors
	gc.InitPair(1, gc.C_WHITE, gc.C_BLUE)
	gc.InitPair(2, gc.C_GREEN, gc.C_BLACK)
	gc.InitPair(3, gc.C_CYAN, gc.C_BLACK)
	gc.InitPair(4, gc.C_RED, gc.C_BLACK)
	gc.InitPair(5, gc.C_BLACK, gc.C_WHITE)

	// Register pressed key through channel
	in := make(chan gc.Key)
	go func(w *gc.Window, ch chan<- gc.Key) {
		for {
			ch <- w.GetChar()
		}
	}(stdscr, in)

	// Run!
	if username, ip, ok := goLogin(stdscr, in); ok {
		okreboot(stdscr, username, ip)
	}
}
