# -*- coding: utf-8 -*-

# Copyright (c) 2016 - 2017 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to edit a list of history entries.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import QDialog, QInputDialog, QLineEdit

from E5Gui import E5MessageBox

from .Ui_StartHistoryEditDialog import Ui_StartHistoryEditDialog


class StartHistoryEditDialog(QDialog, Ui_StartHistoryEditDialog):
    """
    Class implementing a dialog to edit a list of history entries.
    """
    def __init__(self, history, parent=None):
        """
        Constructor
        
        @param history list of history entries to be edited
        @type list of str
        @param parent reference to the parent widget
        @type QWidget
        """
        super(StartHistoryEditDialog, self).__init__(parent)
        self.setupUi(self)
        
        self.historyList.addItems(history)
        for row in range(self.historyList.count()):
            itm = self.historyList.item(row)
            flags = itm.flags() | Qt.ItemIsEditable
            itm.setFlags(flags)
        
        self.__updateEditButtons()
    
    def __updateEditButtons(self):
        """
        Private method to set the state of the edit buttons.
        """
        selectedCount = len(self.historyList.selectedItems())
        self.editButton.setEnabled(selectedCount == 1)
        self.deleteButton.setEnabled(selectedCount > 0)
        self.deleteAllButton.setEnabled(self.historyList.count() > 0)
    
    @pyqtSlot()
    def on_historyList_itemSelectionChanged(self):
        """
        Private slot to handle the selection of entries.
        """
        self.__updateEditButtons()
    
    @pyqtSlot()
    def on_editButton_clicked(self):
        """
        Private slot to edit the selected entry.
        """
        itm = self.historyList.selectedItems()[0]
        historyText, ok = QInputDialog.getText(
            self,
            self.tr("Edit History Entry"),
            self.tr("Enter the new text:"),
            QLineEdit.Normal,
            itm.text())
        if ok:
            itm.setText(historyText)
    
    @pyqtSlot()
    def on_deleteButton_clicked(self):
        """
        Private slot to delete the selected entries.
        """
        yes = E5MessageBox.yesNo(
            self,
            self.tr("Delete Selected Entries"),
            self.tr("""Do you really want to delete the selected"""
                    """ history entries?"""))
        if yes:
            for itm in self.historyList.selectedItems():
                row = self.historyList.row(itm)
                self.historyList.takeItem(row)
                del itm
    
    @pyqtSlot()
    def on_deleteAllButton_clicked(self):
        """
        Private slot to delete all entries.
        """
        yes = E5MessageBox.yesNo(
            self,
            self.tr("Delete All Entries"),
            self.tr("""Do you really want to delete the shown history?"""))
        if yes:
            self.historyList.clear()
    
    def getHistory(self):
        """
        Public method to get the new list of history entries.
        
        @return list of history entries
        @rtype list of str
        """
        history = []
        for row in range(self.historyList.count()):
            entry = self.historyList.item(row).text()
            history.append(entry)
        
        return history
