import sys
from PyQt5.QtWidgets import QWidget, QFileDialog, QMainWindow, QMenuBar, QToolBar, QAction, QStatusBar, QSizePolicy
from PyQt5.QtGui import QKeySequence
from PyQt5.QtCore import QSize, QSettings, Qt

from nixview.util.file_handler import FileHandler, NodeType
import nixview.constants as cnst
import nixview.communicator as comm
from nixview.ui.centralwidget import CentralWidget
from nixview.ui.about import AboutDialog
from nixview.ui.helpdialog import HelpDialog


class NixView(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(NixView, self).__init__(*args, **kwargs)
        self._current_item = None
        self.setWindowTitle("NixView")
        self.setWindowIcon(cnst.get_icon('nixview'))

        self._file_handler = FileHandler()
        self.setStatusBar(QStatusBar(self))
        self.setMenuBar(QMenuBar(self))
        self.create_actions()

        self._cw = CentralWidget(self)
        self.setCentralWidget(self._cw)

        comm.communicator.open_recent.connect(self.on_open_recent)
        comm.communicator.item_selected.connect(self.on_item_selected)
        comm.communicator.plot_error.connect(self.on_plot_error)
        self.show()

    def on_open_recent(self, event):
        self.open_file(event)

    def on_item_selected(self, current):
        self._current_item = current
        current_item_type = current._node_descriptor.entity_type
        enable = current_item_type == NodeType.MultiTag or current_item_type == NodeType.DataArray or current_item_type == NodeType.Tag
        self._plot_action.setEnabled(enable)
        self._table_action.setEnabled(enable)

    def create_actions(self):
        self._file_open_action = QAction(cnst.get_icon("nixview_open"), "Open", self)
        self._file_open_action.setStatusTip("Open nix file")
        self._file_open_action.setShortcut(QKeySequence("Ctrl+o"))
        self._file_open_action.triggered.connect(self.on_file_open)

        self._file_close_action = QAction(cnst.get_icon("nixview_close"), "Close", self)
        self._file_close_action.setStatusTip("Close current nix file")
        self._file_close_action.setShortcut(QKeySequence("Ctrl+w"))
        self._file_close_action.setEnabled(False)
        self._file_close_action.triggered.connect(self.on_file_close)

        self._quit_action = QAction(cnst.get_icon("nixview_quit"), "Quit", self)
        self._quit_action.setStatusTip("Close current file and quit")
        self._quit_action.setShortcut(QKeySequence("Ctrl+q"))
        self._quit_action.triggered.connect(self.on_quit)

        self._plot_action = QAction(cnst.get_icon("nix_data_array"), "Plot", self)
        self._plot_action.setStatusTip("Plot currently selected entity")
        self._plot_action.setShortcut(QKeySequence("Ctrl+p"))
        self._plot_action.setEnabled(False)
        self._plot_action.triggered.connect(self.on_item_plot)

        self._table_action = QAction(cnst.get_icon("nix_data_frame"), "Show table", self)
        self._table_action.setStatusTip("Show data as table")
        self._table_action.setShortcut(QKeySequence("Ctrl+t"))
        self._table_action.setEnabled(False)
        # self._table_action.triggered.connect(self.on_file_close)

        self._about_action = QAction("about")
        self._about_action.setStatusTip("Show about dialog")
        self._about_action.setEnabled(True)
        self._about_action.triggered.connect(self.on_about)

        self._help_action = QAction(cnst.get_icon("nixview_help"), "help")
        self._help_action.setStatusTip("Show help dialog")
        self._table_action.setShortcut(QKeySequence("F1"))

        self._help_action.setEnabled(True)
        self._help_action.triggered.connect(self.on_help)

        self.create_toolbar()
        self.create_menu() 

    def create_toolbar(self):
        self._toolbar = QToolBar("My main toolbar")
        #self._toolbar.setStyleSheet("QToolButton:!hover {background-color:none}")
        self._toolbar.setAllowedAreas(Qt.LeftToolBarArea | Qt.TopToolBarArea)
        self._toolbar.setIconSize(QSize(32, 32))

        self._toolbar.addAction(self._file_open_action)
        self._toolbar.addAction(self._file_close_action)
        self._toolbar.addSeparator()
        self._toolbar.addAction(self._plot_action)
        self._toolbar.addAction(self._table_action)
        self._toolbar.addAction(self._help_action)
        
        empty = QWidget()
        empty.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self._toolbar.addWidget(empty)
        self._toolbar.addSeparator()
        self._toolbar.addAction(self._quit_action)

        self.addToolBar(Qt.LeftToolBarArea, self._toolbar)

    def create_menu(self):
        menu = self.menuBar()
        file_menu = menu.addMenu("&File")
        file_menu.addAction(self._file_open_action)
        file_menu.addAction(self._file_close_action)
        file_menu.addSeparator()
        file_menu.addAction(self._quit_action)
        
        plot_menu = menu.addMenu("&Plot")
        plot_menu.addAction(self._plot_action)
        plot_menu.addAction(self._table_action)
        
        help_menu = menu.addMenu("&Help")
        help_menu.addAction(self._about_action)
        help_menu.addAction(self._help_action)
        self.setMenuBar(menu)

    def _update_recent_files(self, filename):
        settings = QSettings(cnst.organization, cnst.application)
        recent_file_max_count = settings.value(cnst.settings_recent_file_max_count_key, 10, type=int)
        filenames = settings.value(cnst.settings_recent_files_key, [])
        new_filenames = [filename]
        if filename in filenames:
            del filenames[filenames.index(filename)]
        new_filenames.extend(filenames)
        settings.setValue(cnst.settings_recent_files_key, new_filenames[:recent_file_max_count])
        del settings

    def open_file(self, filename):
        self._current_item = None
        success, msg = self._file_handler.open(filename)
        self.statusBar().showMessage(msg, 5000)
        if success:
            self._file_close_action.setEnabled(success)
            self._cw.show_file_content()
            self._update_recent_files(filename)

    def on_file_open(self, s):
        dlg = QFileDialog(self, 'Open nix data file', '', "NIX files (*.h5 *.nix)")
        dlg.setFileMode(QFileDialog.ExistingFile)
        filenames = None
        if dlg.exec_():
            filenames = dlg.selectedFiles()
            self.open_file(filenames[0])

    def on_file_close(self, s):
        self._file_handler.close()
        self._cw.reset()
        self._current_item = None
        self._file_close_action.setEnabled(False)
        self.statusBar().showMessage("Successfully closed current file", 1000)

    def on_quit(self, s):
        self._file_handler.close()
        sys.exit()

    def on_item_plot(self, s):
        if self._current_item is not None:
            self._cw.plot_item(self._current_item.node_descriptor)

    def on_plot_error(self, s):
        self.statusBar().showMessage(s, 5000)

    def on_about(self, e):
        about = AboutDialog(self)
        about.show()

    def on_help(self, e):
        about = HelpDialog(self)
        about.show()