from tools import widget_form, widget_menu


class SiteForm:

    def __init__(self, sitename, timingtemplate, description):
        self.menulist = []
        n = self.__class__.__name__
        self.menudata = {
            '_text': '',
            '_backtitle': 'Developed by Bernhard - Module: ' + n,
            '_title': 'Lidskjalv - Network Monitor'
        }
        self.sitename = sitename
        self.timingtemplate = timingtemplate
        self.description = description

    def form_standard(self):
        t = "Add a new site"
        tab = 43
        lj = 18
        e = (
            ("Site name:".ljust(lj), 1, 1, '',  1, tab, 31, 31),
            ("Timing template:".ljust(lj), 3, 1, '', 3, tab, 31, 31),
            ("Description:".ljust(lj), 5, 1, '', 5, tab, 31, 31)
        )
        res = widget_form(t, e)
        print(res)

    def timing_template_form(self, tt):
        n = self.__class__.__name__
        menudata = {
            '_text': '0 - 1: Very hard to detect.\n4 - 5: Detectable scan',
            '_backtitle': 'Developed by Bernhard - Module: ' + n,
            '_title': 'Lidskjalv - Timing Template'
        }
        menulist = []
        menulist.append(["0", "Paranoid"])
        menulist.append(["1", "Sneaky"])
        menulist.append(["2", "Polite"])
        menulist.append(["3", "Normal"])
        menulist.append(["4", "Aggressive"])
        menulist.append(["5", "Insane"])
        code, tag = widget_menu(menulist, menudata)
        tag = int(tag)
        print(tag)
        return tag

    def main(self):
        while True:
            self.buildmenu()
            code, tag = widget_menu(self.menulist, self.menudata)

            if code == "ok":
                if tag == "Site name":
                    self.sitename = self.inputbox(
                        "Add a name for the site:",
                        "Site name:",
                        self.sitename)
                if tag == "Timing template":
                    self.timingtemplate = self.timing_template_form(
                        self.timingtemplate)
                if tag == "Description":
                    self.description = self.inputbox(
                        "Add a description for the site:",
                        "Description:",
                        self.description)
                if tag == "Cancel":
                    return None
                if tag == "Done":
                    return [
                        self.sitename,
                        self.timingtemplate,
                        self.description]

    def inputbox(self, t, label, value):
        lj = len(label) + 4
        e = ((label, 1, 1, value,  1, lj, 50, 50),)
        res = widget_form(t, e)
        print(res[1][0])
        return res[1][0]

    def buildmenu(self):
        self.menulist = []
        self.menulist.append(["Site name", self.sitename])
        self.menulist.append(["Timing template", str(self.timingtemplate)])
        self.menulist.append(["Description", self.description])
        self.menulist.append(["", ""])
        self.menulist.append(["Done", ""])
        self.menulist.append(["Cancel", ""])


if __name__ == '__main__':
    print("This is a module.",
          "Please run the lidskjalvmanager in the future.")

    # o = SiteForm("", "4", "")
    # print(o.main())
    # o.timing_template(2)
