from PyQt5.QtWidgets import (
    QApplication, QWidget, QVBoxLayout, QHBoxLayout,
    QTextEdit, QPushButton, QSpacerItem, QSpinBox,
    QSizePolicy, QFontComboBox, QColorDialog
)
from PyQt5.QtGui import QPixmap, QIcon, QFont
from PyQt5.QtCore import Qt

import settings as stg

class NoteTab(QWidget):

    ''' This class generates a enhanced notepad in Note Tab '''

    def __init__(self, widget_tab):
        super().__init__()

        path_icon = "./icons/"

        self.verticalLayout_main_note_tab = QVBoxLayout(widget_tab)

        self.horizontalLayout_toolbar = QHBoxLayout()
        self.verticalLayout_main_note_tab.addLayout(self.horizontalLayout_toolbar)

        self.textEdit = QTextEdit()

        self.filename_text = ""

        self.pushbutton_new_txt = QPushButton()
        self.icon_new_txt = QPixmap(path_icon + "new_text.png")
        self.pushbutton_new_txt.setIcon(QIcon(self.icon_new_txt))
        # self.pushbutton_new_txt.clicked.connect(self.new_text)
        self.horizontalLayout_toolbar.addWidget(self.pushbutton_new_txt)

        self.pushbutton_save_txt = QPushButton()
        self.icon_save_txt = QPixmap(path_icon + "save_txt.png")
        self.pushbutton_save_txt.setIcon(QIcon(self.icon_save_txt))
        # self.pushbutton_save_txt.clicked.connect(self.save_text)
        self.horizontalLayout_toolbar.addWidget(self.pushbutton_save_txt)

        self.pushbutton_open_txt = QPushButton()
        self.icon_open_txt = QPixmap(path_icon + "open_txt.png")
        self.pushbutton_open_txt.setIcon(QIcon(self.icon_open_txt))
        # self.pushbutton_open_txt.clicked.connect(self.open_text)
        self.horizontalLayout_toolbar.addWidget(self.pushbutton_open_txt)

        self.fontbox = QFontComboBox()
        self.fontbox.currentFontChanged.connect(lambda font : self.textEdit.setCurrentFont(font))
        self.horizontalLayout_toolbar.addWidget(self.fontbox)

        self.fontSize = QSpinBox()
        self.fontSize.setSuffix("pt")
        self.fontSize.valueChanged.connect(lambda size: self.textEdit.setFontPointSize(size))
        self.fontSize.setValue(14)
        self.horizontalLayout_toolbar.addWidget(self.fontSize)

        self.fontColor = QPushButton()
        self.icon_fontColor = QPixmap(path_icon + "font_color.png")
        self.fontColor.setIcon(QIcon(self.icon_fontColor))
        # self.QAction(QIcon("couleur_police.png"), "Changer la couleur du texte", self)couleur_fond.png
        self.fontColor.clicked.connect(self.fontColorChanged)
        self.horizontalLayout_toolbar.addWidget(self.fontColor)

        self.backColor = QPushButton()
        self.icon_backColor = QPixmap(path_icon + "background_color.png")
        self.backColor.setIcon(QIcon(self.icon_backColor))
        self.backColor.clicked.connect(self.highlight)
        self.horizontalLayout_toolbar.addWidget(self.backColor)

        self.boldAction = QPushButton()
        self.icon_boldAction = QPixmap(path_icon + "bold.png")
        self.boldAction.setIcon(QIcon(self.icon_boldAction))
        self.boldAction.clicked.connect(self.bold)
        self.horizontalLayout_toolbar.addWidget(self.boldAction)

        self.italicAction = QPushButton()
        self.icon_italicAction = QPixmap(path_icon + "italic.png")
        self.italicAction.setIcon(QIcon(self.icon_italicAction))
        self.italicAction.clicked.connect(self.italic)
        self.horizontalLayout_toolbar.addWidget(self.italicAction)

        self.underlineAction = QPushButton()
        self.icon_underlineAction = QPixmap(path_icon + "underline.png")
        self.underlineAction.setIcon(QIcon(self.icon_underlineAction))
        self.underlineAction.clicked.connect(self.underline)
        self.horizontalLayout_toolbar.addWidget(self.underlineAction)

        self.strikeAction = QPushButton()
        self.icon_strikeAction = QPixmap(path_icon + "strike.png")
        self.strikeAction.setIcon(QIcon(self.icon_strikeAction))
        self.strikeAction.clicked.connect(self.strike)
        self.horizontalLayout_toolbar.addWidget(self.strikeAction)

        self.alignLeftAction = QPushButton()
        self.icon_alignLeftAction = QPixmap(path_icon + "left.png")
        self.alignLeftAction.setIcon(QIcon(self.icon_alignLeftAction))
        self.alignLeftAction.clicked.connect(self.alignLeft)
        self.horizontalLayout_toolbar.addWidget(self.alignLeftAction)

        self.alignCenterAction = QPushButton()
        self.icon_alignCenterAction = QPixmap(path_icon + "centre.png")
        self.alignCenterAction.setIcon(QIcon(self.icon_alignCenterAction))
        self.alignCenterAction.clicked.connect(self.alignCenter)
        self.horizontalLayout_toolbar.addWidget(self.alignCenterAction)

        self.alignRightAction = QPushButton()
        self.icon_alignRightAction = QPixmap(path_icon + "right.png")
        self.alignRightAction.setIcon(QIcon(self.icon_alignRightAction))
        self.alignRightAction.clicked.connect(self.alignRight)
        self.horizontalLayout_toolbar.addWidget(self.alignRightAction)

        self.alignJustifyAction = QPushButton()
        self.icon_alignJustifyAction = QPixmap(path_icon + "justify.png")
        self.alignJustifyAction.setIcon(QIcon(self.icon_alignJustifyAction))
        self.alignJustifyAction.clicked.connect(self.alignJustify)
        self.horizontalLayout_toolbar.addWidget(self.alignJustifyAction)

        self.horizontalSpacerItem_toolbar_note_tab = QSpacerItem(500, 10, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.horizontalLayout_toolbar.addItem(self.horizontalSpacerItem_toolbar_note_tab)

        self.pushbutton_print_settings = QPushButton()
        self.pushbutton_print_settings.setText("EDIT SETTINGS")
        self.pushbutton_print_settings.clicked.connect(self.print_settings)
        self.verticalLayout_main_note_tab.addWidget(self.pushbutton_print_settings)

        self.verticalLayout_main_note_tab.addWidget(self.textEdit)

    ## -------------------------------
    ## ---------- Functions ----------
    ## -------------------------------


    # def new_text(self):
    #     window = self.ui_mainwindow.tab5
    #     window.show()

    # def open_text(self):
    #     self.filename_text = QFileDialog.getOpenFileName(self, 'Open File', ".", "(*.txt")
    #     if self.filename_text:
    #         with open(self.filename_text, 'txt') as file:
    #             self.text.setText(file.read())

    # def save_text(self):
    #     if not self.filename_text:
    #         self.filename_text = QFileDialog.getSaveFileName(self, 'Save File')
    #
    #     if not self.filename_text.endswith(".txt"):
    #         self.filename_text += ".txt"
    #
    #         with open(self.filename_text, "txt") as file:
    #             file.write(self.text.toHtml())

    def fontColorChanged(self):
        color = QColorDialog.getColor()
        self.textEdit.setTextColor(color)

    def highlight(self):
        color = QColorDialog.getColor()
        self.textEdit.setTextBackgroundColor(color)

    def bold(self):
        if self.textEdit.fontWeight() == QFont.Bold:
            self.textEdit.setFontWeight(QFont.Normal)
        else:
            self.textEdit.setFontWeight(QFont.Bold)

    def italic(self):
        state = self.textEdit.fontItalic()
        self.textEdit.setFontItalic(not state)

    def underline(self):
        state = self.textEdit.fontUnderline()
        self.textEdit.setFontUnderline(not state)

    def strike(self):
        fmt = self.textEdit.currentCharFormat()
        fmt.setFontStrikeOut(not fmt.fontStrikeOut())
        self.textEdit.setCurrentCharFormat(fmt)

    def alignLeft(self):
        self.textEdit.setAlignment(Qt.AlignLeft)

    def alignRight(self):
        self.textEdit.setAlignment(Qt.AlignRight)

    def alignCenter(self):
        self.textEdit.setAlignment(Qt.AlignCenter)

    def alignJustify(self):
        self.textEdit.setAlignment(Qt.AlignJustify)

    def print_settings(self):
        self.textEdit.setText(
            "Acoustic data: \n\n"
            f"   ABS raw data file: {stg.path_BS_raw_data}/{stg.filename_BS_raw_data} \n"
            f"   ABS noise data file: {stg.path_BS_noise_data}/{stg.filename_BS_noise_data} \n"
            "\n\n"
            "------------------------------------------------------------------------- \n\n\n"

            "Particle size data: \n"
            f"   Fine sediments data file: {stg.path_fine}/{stg.filename_fine} \n"
            f"   Sand sediments data file: {stg.path_sand}/{stg.filename_sand} \n"
            "\n\n"
            "------------------------------------------------------------------------- \n\n\n"
        )

        # "Acoustic Inversion parameters: \n"
        # f"   frequencies to compute VBI: {stg.freq_text[int(stg.frequencies_to_compute_VBI[0, 0])]}, "
        # f"{stg.freq_text[int(stg.frequencies_to_compute_VBI[1, 0])]} \n"
        # f"   frequency to compute SSC: {stg.freq_text[int(stg.frequency_to_compute_SSC[0])]}")
