#!/usr/bin/env python3
# This file is placed in the Public Domain.
#
# pylint: disable=C0115,C0116,C0209,C0413,W0201,R0903,W0212


"program"


import inspect
import os
import sys
import termios
import time


sys.path.insert(0, os.getcwd())


from prg.object import Default, Object, fmt, keys
from prg.disk   import Storage
from prg.error  import Censor, Errors
from prg.find   import find
from prg.run    import Cfg, Commands, Errors, Event, Reactor, forever, parse
from prg.run    import scan


"defines"


Cfg.name   = "prg"
Storage.wd = os.path.expanduser("~/.prg")


"modules"


import prg.mods as modules


try:
    import mods
except ModuleNotFoundError:
    mods = None


"cli"


class CLI(Commands):

    def dosay(self, channel, txt):
        print(txt.encode('utf-8', 'replace').decode())
        sys.stdout.flush()


"console"


class Console(Reactor):

    def __init__(self):
        Reactor.__init__(self)
        self.register("command", Commands.dispatch)

    def dispatch(self, evt):
        Commands.dispatch(evt)

    def poll(self) -> Event:
        evt = Event()
        evt.orig = object.__repr__(self)
        evt.txt = input("> ")
        evt.type = "command"
        return evt


"utilities"
        

def wrap(func) -> None:
    old = None
    try:
        old = termios.tcgetattr(sys.stdin.fileno())
    except termios.error:
        pass
    try:
        func()
    except (EOFError, KeyboardInterrupt):
        print("")
        sys.stdout.flush()
    finally:
        if old:
            termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old)


"runtime"


def main():
    Censor.output = print
    parse(Cfg, " ".join(sys.argv[1:]))
    if "wd" in Cfg.sets:
        Storage.wd = Cfg.wd
    scan(modules)
    for mod in scan(mods, Cfg.mod):
        if "i" in Cfg.opts:
            mod.init()
    if "c" in Cfg.opts:
       if "v" in Cfg.opts:
           dte = time.ctime(time.time()).replace("  ", " ")
           print(f"{Cfg.name.upper()} started {dte} {Cfg.opts.upper()}")
       csl = Console()
       csl.start()
       forever()
       return
    cli = CLI()
    evn = Event()
    evn.orig = object.__repr__(cli)
    evn.txt = Cfg.otxt
    parse(evn)
    CLI.dispatch(evn)


if __name__ == "__main__":
    wrap(main)
    Errors.show()
