#!/usr/bin/python
# Code for the chords interval 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 # pylint: disable=import-error
import re, helper_functions
from helper_functions import root

class ChangeInterval(wx.Dialog):
    def __init__(self, parent):
        super(ChangeInterval, 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 Interval Menu")


    def InitUI(self):
        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        # Add explanation text
        vbox.Add(wx.StaticText(panel, label="By default, the sensors send data to CHORDS each"), flag=wx.LEFT|wx.TOP|wx.RIGHT, border=15)
        vbox.Add(wx.StaticText(panel, label="minute. You can change that interval here."), flag=wx.LEFT|wx.RIGHT, border=15)
        vbox.Add(wx.StaticText(panel, label="(Should be between 1 and 60 minutes)"), 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)
        # Create input
        self.interval_input = wx.SpinCtrl(panel, value=self.interval, min=1, max=60)
        vbox.Add(self.interval_input, 0, wx.ALL|wx.CENTER, 5)
        # Make a horizontal line
        line = wx.StaticLine(panel)
        vbox.Add(line, flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=7)
        # Add save and cancel buttons
        button_area = wx.BoxSizer(wx.HORIZONTAL)
        self.save_button = wx.Button(panel, label='Save')
        self.save_button.Bind(wx.EVT_BUTTON, self.OnSave)
        button_area.Add(self.save_button, flag=wx.RIGHT, border=5)
        self.cancel_button = wx.Button(panel, label='Cancel')
        self.cancel_button.Bind(wx.EVT_BUTTON, self.OnCancel)
        button_area.Add(self.cancel_button)
        vbox.Add(button_area, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=15)
        # Adjust window size to fit content
        panel.SetSizer(vbox)
        vbox.Fit(self)


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


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