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

import sys
from settings import *

from PyQt6.QtWidgets import QLabel
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QAction, QBrush, QColor, QConicalGradient,
    QCursor, QFont, QFontDatabase, QGradient,
    QIcon, QImage, QKeySequence, QLinearGradient,
    QPainter, QPalette, QPixmap, QRadialGradient,
    QTransform)
from PySide6.QtWidgets import (QApplication, QMainWindow, QMenu, QMenuBar,
    QPushButton, QSizePolicy, QStatusBar, QWidget, QFileDialog, QLabel)

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib as plt


def load_file(file_name):
    try:
        if ".csv" not in file_name:
            raise TypeError
        data = pd.read_csv(file_name, sep=";")
    except:
        print("ERROR: process_file()")
        return 0
    print(data.head())
    return data


class UiMainWindow(object):
    def setup(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(*SIZE_WINDOW)

        ''' Меню -> Файл -> Открыть файл '''
        self.actionOpen_File = QAction(MainWindow)
        self.actionOpen_File.setObjectName(u"actionOpen_File")
        self.actionOpen_File.triggered.connect(self.open_file)

        ''' Меню -> Файл -> Сохранить '''
        self.actionExport = QAction(MainWindow)
        self.actionExport.setObjectName(u"actionExport")

        ''' Меню -> Помощь -> Информация '''
        self.actionInfo = QAction(MainWindow)
        self.actionInfo.setObjectName(u"actionInfo")

        ''' Меню -> Настройки '''
        self.actionsettings = QAction(MainWindow)
        self.actionsettings.setObjectName(u"actionsettings")

        ''' Меню -> Настройки -> Параметры '''
        self.actionProperties = QAction(MainWindow)
        self.actionProperties.setObjectName(u"actionProperties")

        ''' Меню -> Настройки -> Стиль -> Обычный '''
        self.actionDefault = QAction(MainWindow)
        self.actionDefault.setObjectName(u"actionDefault")

        ''' Меню -> Настройки -> Стиль -> Темный '''
        self.actionDark = QAction(MainWindow)
        self.actionDark.setObjectName(u"actionDark")

        ''' Главный виджет - Полотно '''
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")

        '''  Панель инструментов '''
        self.widget_tools = QWidget(self.centralwidget)
        self.widget_tools.setObjectName(u"widget_tools")
        self.widget_tools.setGeometry(QRect(-1, -1, SIZE_WINDOW[0], SIZE_TOOLS_BUTTON+1))
        self.widget_tools.setStyleSheet(u"border-bottom-color: #707070;") # Тут я хочу чтобы цвет нжней границы виджета появился и был определенного цвета, помоги это сделать

        ''' Инструменты -> Открыть файл '''
        self.openFile_tools = QPushButton(self.widget_tools)
        self.openFile_tools.setObjectName(u"openFile_tools")
        self.openFile_tools.setEnabled(True)
        self.openFile_tools.setGeometry(QRect(SPACE, 0, SIZE_TOOLS_BUTTON+1, SIZE_TOOLS_BUTTON+1))
        self.openFile_tools.setAutoFillBackground(True)
        icon = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.DocumentOpen))
        self.openFile_tools.setIcon(icon)
        self.openFile_tools.setIconSize(QSize(*SIZE_TOOLS_ICON))
        self.openFile_tools.setAutoDefault(False)
        self.openFile_tools.setFlat(True)
        self.openFile_tools.clicked.connect(self.open_file)

        ''' Инструменты -> Сохранить '''
        self.save_tools = QPushButton(self.widget_tools)
        self.save_tools.setObjectName(u"save_tools")
        self.save_tools.setEnabled(True)
        self.save_tools.setGeometry(QRect(SIZE_TOOLS_BUTTON+SPACE, 0, SIZE_TOOLS_BUTTON+1, SIZE_TOOLS_BUTTON+1))
        self.save_tools.setAutoFillBackground(True)
        icon1 = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.DocumentSave))
        self.save_tools.setIcon(icon1)
        self.save_tools.setIconSize(QSize(*SIZE_TOOLS_ICON))
        self.save_tools.setAutoDefault(False)
        self.save_tools.setFlat(True)

        ''' Инструменты -> Карандаш '''
        self.dotPut_tools = QPushButton(self.widget_tools)
        self.dotPut_tools.setObjectName(u"dotPut_tools")
        self.dotPut_tools.setEnabled(True)
        self.dotPut_tools.setGeometry(QRect(SIZE_TOOLS_BUTTON*2+SPACE*3, 0, SIZE_TOOLS_BUTTON+1, SIZE_TOOLS_BUTTON+1))
        self.dotPut_tools.setAutoFillBackground(True)
        icon2 = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.MailMessageNew))
        self.dotPut_tools.setIcon(icon2)
        self.dotPut_tools.setIconSize(QSize(*SIZE_TOOLS_ICON))
        self.dotPut_tools.setAutoDefault(False)
        self.dotPut_tools.setFlat(True)
        self.dotPut_tools.setCheckable(True)
        self.dotPut_tools.setStyleSheet(
            "QPushButton:checked {"
            "border: 1px  solid #707070;"
            "background-color: #D0D0D0; }")

        ''' Инструменты -> Переместить '''
        self.movePlot_tools = QPushButton(self.widget_tools)
        self.movePlot_tools.setObjectName(u"movePlot_tools")
        self.movePlot_tools.setEnabled(True)
        self.movePlot_tools.setGeometry(QRect(SIZE_TOOLS_BUTTON*3+SPACE*3, 0, SIZE_TOOLS_BUTTON+1, SIZE_TOOLS_BUTTON+1))
        self.movePlot_tools.setAutoFillBackground(True)
        icon3 = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.DocumentOpenRecent))
        self.movePlot_tools.setIcon(icon3)
        self.movePlot_tools.setIconSize(QSize(*SIZE_TOOLS_ICON))
        self.movePlot_tools.setAutoDefault(False)
        self.movePlot_tools.setFlat(True)
        self.movePlot_tools.setCheckable(True)
        self.movePlot_tools.setStyleSheet(
            "QPushButton:checked {"
            "border: 1px solid #707070;"
            "background-color: #D0D0D0; }")

        ''' Виджет для графиков '''
        self.areaPlot = QWidget(self.centralwidget)
        self.areaPlot.setObjectName(u"areaPlot")
        self.areaPlot.setGeometry(QRect(0, SIZE_TOOLS_BUTTON, SIZE_WINDOW[0], 501))

        MainWindow.setCentralWidget(self.centralwidget)

        ''' Дерево Меню '''
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, SIZE_WINDOW[0], 22))
        self.menuFile = QMenu(self.menubar)
        self.menuFile.setObjectName(u"menuFile")
        self.menuHelp = QMenu(self.menubar)
        self.menuHelp.setObjectName(u"menuHelp")
        self.menuHelp.setGeometry(QRect(301, 125, 134, 72))
        self.menuSettings = QMenu(self.menubar)
        self.menuSettings.setObjectName(u"menuSettings")
        self.menuStyle = QMenu(self.menuSettings)
        self.menuStyle.setObjectName(u"menuStyle")
        MainWindow.setMenuBar(self.menubar)

        ''' Виджет для статусов '''
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)

        ''' Статус отладки '''
        self.feedback_label = QLabel(self.statusbar)
        self.feedback_label.setObjectName(u"feedbackLabel")
        self.feedback_label.setGeometry(QRect(8, 0, 250, 20))

        ''' Добавление кнопок в Меню '''
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuSettings.menuAction())
        self.menubar.addAction(self.menuHelp.menuAction())
        self.menuFile.addAction(self.actionOpen_File)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionExport)
        self.menuHelp.addAction(self.actionInfo)
        self.menuSettings.addAction(self.menuStyle.menuAction())
        self.menuSettings.addAction(self.actionProperties)
        self.menuStyle.addAction(self.actionDefault)
        self.menuStyle.addAction(self.actionDark)

        self.retranslateUi(MainWindow)

        self.openFile_tools.setDefault(False)
        self.save_tools.setDefault(False)
        self.dotPut_tools.setDefault(False)
        self.movePlot_tools.setDefault(False)

        QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        ''' Обзываем элементы приложения '''
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"Графический Анализатор", None))
        self.actionOpen_File.setText(QCoreApplication.translate("MainWindow", u"Открыть файл", None))
        self.actionExport.setText(QCoreApplication.translate("MainWindow", u"Сохранить", None))
        self.actionInfo.setText(QCoreApplication.translate("MainWindow", u"Информация", None))
        self.actionsettings.setText(QCoreApplication.translate("MainWindow", u"Настройки", None))
        self.actionProperties.setText(QCoreApplication.translate("MainWindow", u"Параметры", None))
        self.actionDefault.setText(QCoreApplication.translate("MainWindow", u"Обычный", None))
        self.actionDark.setText(QCoreApplication.translate("MainWindow", u"Темный", None))
        self.openFile_tools.setText("")
        self.save_tools.setText("")
        self.dotPut_tools.setText("")
        self.movePlot_tools.setText("")
        self.feedback_label.setText("")
        self.menuFile.setTitle(QCoreApplication.translate("MainWindow", u"Файл", None))
        self.menuHelp.setTitle(QCoreApplication.translate("MainWindow", u"Помощь", None))
        self.menuSettings.setTitle(QCoreApplication.translate("MainWindow", u"Настройки", None))
        self.menuStyle.setTitle(QCoreApplication.translate("MainWindow", u"Стиль", None))

    def open_file(self):
        file_name, _ = QFileDialog.getOpenFileName(MainWindow, "Открыть файл", "", "Все файлы (*)")
        if file_name:
            self.feedback_label.setText("Открывается...")
            self.data = load_file(file_name)
            if type(self.data) != int:
                self.feedback_label.setText("Успешно открыт")
            else:
                self.feedback_label.setText("Не удалось открыть файл")

    def draw(self):
        pass


if __name__ == "__main__":
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = UiMainWindow()
    ui.setup(MainWindow)
    MainWindow.show()
    sys.exit(app.exec())