#!/usr/bin/python
# Code for the modal that displays data per sensor
# 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: 20 March 2019

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

class ShowData(wx.Dialog):
    def __init__(self, parent, sensor):
        super(ShowData, self).__init__(parent)
        # Check to see which data is being asked for
        if sensor == "BMP180/280":
            path = "bmp/"
        elif sensor == "HTU21D":
            path = "htu21d/"
        elif sensor == "MCP9008":
            path = "mcp9808/"
        elif sensor == "SI1145":
            path = "si1145/"
        elif sensor == "Tipping Bucket":
            path = "rain/"
        elif sensor == "Wind Direction":
            path = "wind_direction/"
        elif sensor == "Wind Speed":
            path = "wind_speed/"
        elif sensor == "Remote Stations":
            path = "remote_stations/"
        self.sensor = sensor
        # Grab the most recent data file and read its lines into a list
        list_of_files = glob.glob(root + 'data/' + path + '*.dat')
        if len(list_of_files) > 0:
            latest_file = max(list_of_files, key=os.path.getctime)
            with open(latest_file) as f:
                self.content = f.readlines()
            # Only use the 5 most recent lines of data
            if len(self.content) > 5:
                self.content = self.content[-5:]
        # If there are no files found
        else:
            self.content = ["(There are no data files yet)\n"]
        # Initialize the window
        self.InitUI()
        self.SetTitle(self.sensor + " Sensor Data")


    def InitUI(self):
        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        if self.sensor == "Remote Stations":
            sentence = "remote stations."
        else:
            sentence = self.sensor.lower() + " sensor."
        vbox.Add(wx.StaticText(panel, label="These are the most recent data entries from the " + sentence), flag=wx.ALL, border=15)
        # Make a horizontal line
        line = wx.StaticLine(panel)
        vbox.Add(line, flag=wx.LEFT|wx.BOTTOM|wx.RIGHT|wx.EXPAND, border=7)
        # Show data
        for line in self.content:
            line = line.replace("\n","")
            vbox.Add(wx.StaticText(panel, label=line), flag=wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT, border=20)
        # Make a horizontal line
        line = wx.StaticLine(panel)
        vbox.Add(line, flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=7)
        # Add close button
        self.closeButton = wx.Button(panel, label='Close')
        self.closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
        vbox.Add(self.closeButton, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=15)
        # Adjust window size to fit content
        panel.SetSizer(vbox)
        vbox.Fit(self)


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