#==
Main server, DEPS
==#
module Jockey
using Crayons
using Genie, Logging, LoggingExtras
using Sockets
using Base.Threads: @spawn
using TOML
include("ServerController/Session.jl")
include("ServerController/Settings.jl")
"""
Developer API
## main()
Loads a virtual environment, starts Genie, and boots the server. Not meant to be
called directly, instead it should be
```
main()
```
"""
function main()
  Core.eval(Main, :(const UserApp = $(@__MODULE__)))

  Genie.genie(; context = @__MODULE__)

  Core.eval(Main, :(const Genie = UserApp.Genie))
  Core.eval(Main, :(using Genie))
end
#==
Polling
==#
"""
Developer API
## JockeyPoll(s::string)
Other calls:
- ?(JockeyPoll(s::String, status::Symbol))
JockeyPoll is Jockey's internal communication to the REPL via logging and
terminal output. This is of course vital to working server-side, logs are
important. Providing a string will provide appropriate Jockey return.
```
JockeyPoll("Have a nice day")
[Jockey] Have a nice day!
```
"""
function JockeyPoll(s::String)
  print(Crayon(foreground = :black, bold = true), "[")
  print(Crayon(foreground = :cyan, bold = true), "Jockey")
  print(Crayon(foreground = :black, bold = true), "]  ")
  print(Crayon(foreground = :cyan, bold = false), string(s, "\n"))
end
"""
Developer API
## JockeyPoll(s::string, status::Symbol)
Other calls:
- ?(JockeyPoll(s::String))
JockeyPoll is Jockey's internal communication to the REPL via logging and
terminal output. This is of course vital to working server-side, logs are
important. Providing a string will provide appropriate Jockey return.
The status symbol can be any of the following
- :critical
- :warn
- :request
```
JockeyPoll("Have a nice day")
[Jockey] Have a nice day!
In the future, these will also be **logged.**
```
"""
function JockeyPoll(s::String, status::Symbol)
  # warn, critical,  Request
  status_colours = Dict(:warn => [:red, :magenta],
   :critical => [:black, :red],
    :request => [:cyan, :green])
    status = status_colours[status]
  print(Crayon(foreground = :black, bold = true), "[")
  print(Crayon(foreground = status[1], bold = true), "Jockey")
  print(Crayon(foreground = :black, bold = true), "]  ")
  print(Crayon(foreground = status[2], bold = false), string(s, "\n"))
end
#==
Main run function (runjy)
==#
"""
Usage Functions
### runjy()
The runjy() server begins a Genie server hosting a new Jockey notebook server.
Ultimately, this is the foundation where one will configure server settings and
decide what sort of system they want to run jockey with. The core server settings
are controllable through the key-word arguments to this function.
##### Key-word arguments
- host = 127.0.0.1
IP address one wishes to host this connection on.
- port = 8000
Port which the server should run on.
- mode = :user
Available mode options are :user, :admin, :headless. Admin mode will grant access
 to a user account system, and Jockey authentication systems. Headless mode will
 begin a worker process, which can be further managed using the management
   interface.
- pwd = 123
Password for use at administrator login screen. **You can ignore this if not in
  admin mode!**
- admin = "admin"
"""
function runjy(;port = 8000, host = "127.0.0.1",
  mode = :user,
  pwd = "123",
  admin = "admin")
  HI = HostInfo(port, host, admin, pwd)
  global settings = JSettings(mode, HI)
  setting_dict = Dict("port" => string(port), "host" => host,
  "admin" => admin, "mode" => string(mode))
  open("config/env/settings.toml", "w") do io
           TOML.print(io, setting_dict)
         end

  JockeyPoll(string("New Jockey Server starting on port ", port))
  run(`./bin/server`)
end
#==
Session ASCII
==#
print(Crayon(foreground = :blue, bold = true), """
        .###
       ##########
    ###*    #######            #######
            #####################  ###
          ######################%  ####
          ,#####################
         #       ###/    ##   ###
         #(      #    ###       #*
        (#      ##
""")
print(Crayon(foreground = :cyan, bold = true), """
    &&&                        &&&
             *&&        #&&.   &&&          &&&
    &&&    &&%  &&&  &&&   ,   &&&*&&,   &&&   &&   &&   &&
    &&&   &&&   &&&  &&&       &&&&&&,   &&&&&&&&    && &&
    &&&     &&&&&&    &&&&&&&  &&&  %&&   &&&&&&&     &&&
&&&&&                                              &&&&

""")
println(Crayon(foreground = :light_gray, bold = false), "v. 0.0.2\n\n")
JockeyPoll("Use runjy() to start a new Jockey server, or use ?(runjy) for more information.")
export runjy
end
