#!/usr/bin/python
# Code for the chords menu
# Joseph E. Rener
# UCAR
# Boulder, CO USA
# Email: jrener@ucar.edu
# Copyright (c) 2018 UCAR
# Developed at COMET at University Corporation for Atmospheric Research and the Research Applications Laboratory at the National Center for Atmospheric Research (NCAR)
# Updated: 28 September 2018

import wx, re, helper_functions # pylint: disable=import-error
from helper_functions import root

class ChangeChords(wx.Dialog):
    def __init__(self, parent):
        super(ChangeChords, self).__init__(parent)
        inputs = helper_functions.getArguments()
        self.interval = str(inputs[0])
        self.chords_toggle = str(inputs[1])
        self.chords_id = str(inputs[2])
        self.pressure_level = str(inputs[3])
        self.InitUI()
        self.SetTitle("CHORDS Menu")


    def InitUI(self):
        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        # Initialize alert and save button (but don't place them yet; this is so they can be referenced when id_input is made)
        self.alert = wx.StaticText(panel, label="")
        self.alert.SetForegroundColour(wx.Colour(255,0,0))
        self.saveButton = wx.Button(panel, label='Save')
        # Add explanation text
        vbox.Add(wx.StaticText(panel, label="By turning this on, you'll send data to the"), flag=wx.LEFT|wx.TOP|wx.RIGHT, border=15)
        vbox.Add(wx.StaticText(panel, label="Chords website if there is a connection."), flag=wx.LEFT|wx.RIGHT, border=15)
        vbox.Add(wx.StaticText(panel, label="Please email Paul Kucera (pkucera@ucar.edu)"), flag=wx.LEFT|wx.RIGHT, border=15)
        vbox.Add(wx.StaticText(panel, label="to obtain an ID"), flag=wx.LEFT|wx.BOTTOM|wx.RIGHT, border=15)
        # Make a horizontal line
        line = wx.StaticLine(panel)
        vbox.Add(line, flag=wx.LEFT|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=7)
        # Add toggle
        toggle_section = wx.BoxSizer(wx.HORIZONTAL)
        toggle_section.Add(wx.StaticText(panel, label="CHORDS"), flag=wx.ALL, border=10)
        toggle_section.Add(wx.StaticText(panel, label=""), flag=wx.RIGHT, border=16)
        self.toggle = wx.ToggleButton(panel, label="")
        if self.chords_toggle.lower() == "true":
            self.toggle.SetLabel("On")
            self.toggle.SetValue(True)
        else:
            self.toggle.SetLabel("Off")
        toggle_section.Add(self.toggle, flag=wx.TOP, border=3)
        self.toggle.Bind(wx.EVT_TOGGLEBUTTON, self.OnChordsToggle)
        vbox.Add(toggle_section, flag=wx.LEFT, border=75)
        # Add ID input
        id_section = wx.BoxSizer(wx.HORIZONTAL)
        id_section.Add(wx.StaticText(panel, label="CHORDS ID"), flag=wx.ALIGN_CENTER|wx.ALL, border=10)
        toggle_section.Add(wx.StaticText(panel, label=""), flag=wx.RIGHT, border=10)
        self.id_input = wx.TextCtrl(panel, -1, style=wx.ALIGN_LEFT)
        self.Bind(wx.EVT_TEXT, self.OnEdit, self.id_input)
        self.id_input.SetValue(self.chords_id)
        id_section.Add(self.id_input, flag=wx.TOP, border=2)
        vbox.Add(id_section, flag=wx.LEFT, border=75)
        # Make a horizontal line
        line = wx.StaticLine(panel)
        vbox.Add(line, flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=7)
        # Place alert
        vbox.Add(self.alert, flag=wx.ALIGN_CENTER)
        # Add save and cancel buttons
        button_area = wx.BoxSizer(wx.HORIZONTAL)
        self.saveButton.Bind(wx.EVT_BUTTON, self.OnSave)
        button_area.Add(self.saveButton, flag=wx.RIGHT, border=5)
        self.cancelButton = wx.Button(panel, label='Cancel')
        self.cancelButton.Bind(wx.EVT_BUTTON, self.OnCancel)
        button_area.Add(self.cancelButton)
        vbox.Add(button_area, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=10)
        # Adjust window size to fit content
        panel.SetSizer(vbox)
        vbox.Fit(self)


    def OnChordsToggle(self, e):
        if self.chords_toggle == "false":
            self.chords_toggle = "true"
            self.toggle.SetLabel("On")
        else:
            self.chords_toggle = "false"
            self.toggle.SetLabel("Off")


    def OnEdit(self, e):
        value = self.id_input.GetValue()
        try:
            int(value)
            self.chords_id = value
            self.alert.SetLabel("")
            self.saveButton.Enable(True)
        except (ValueError, TypeError):
            self.alert.SetLabel("ID must be an integer")
            self.saveButton.Enable(False)


    def OnSave(self, e):
        with open(root + 'scripts/input.txt', 'w') as file:
            file.write(self.interval + "," + self.chords_toggle + "," + str(int(self.chords_id)) + "," + self.pressure_level)
        self.Destroy()


    def OnCancel(self, e):
        self.Destroy()
