import sys
import ntpath
from PySide2.QtWidgets import (
    QApplication,
    QDialog,
    QFileDialog,
    QMessageBox
    )
from PySide2.QtCore import Qt

from uifilechose import Ui_Dialog

from sql.base import Session
from sql.competitive import Competitive
from sql.data import Data
from sql.filterf import FilterF
from sql.compatitive_filter import CompativeFilterf
from sql.category import Category

from sqlalchemy import exc


class FileChoser(QDialog):
    def __init__(self, parent=None):
        super(FileChoser, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.setWindowModality(Qt.ApplicationModal)
        self.main = parent

        #buttons
        self.pbtechedge = self.ui.pushButton_techedge
        self.pbadexpert = self.ui.pushButton_adexpert
        self.pbaddall = self.ui.pushButton_addall
        self.pbaddrapo = self.ui.pushButton_addraports
        self.pb_rap_remo = self.ui.pushButton_remove_raports

        #lineedit
        self.letechedge = self.ui.lineEdit_techedge
        self.leadexpert = self.ui.lineEdit_adexpert
        self.leaddraport = self.ui.lineEdit_addraport

        #combobox
        self.cbraports = self.ui.comboBox_raports
        self.cbrapo_remove = self.ui.comboBox_raports_remove

        #connects
        self.pbtechedge.clicked.connect(lambda: self.pic_file(self.letechedge, 0))
        self.pbadexpert.clicked.connect(lambda: self.pic_file(self.leadexpert, 1))
        self.pbaddrapo.clicked.connect(self.add_new_competitive)
        self.pbaddall.clicked.connect(self.send_values)
        self.pb_rap_remo.clicked.connect(self.remove_competitive)

        self.paths = {
                      0: "",
                      1: "",
                      2: "",
                    }

        """
        read records from database
        """
        self.populate_row_competitive()

    def clead_data(self):
        """
        clean data in form
        :return:
        """
        self.paths = {
            0: "",
            1: "",
            2: "",
        }

        self.letechedge.setText("")
        self.leadexpert.setText("")


    def pic_file(self, lename, nr):
        """
        open file dialog to open file
        :param lename: lineedit where will show picked file
        :return: path to file
        """
        fpath = QFileDialog.getOpenFileName(self, 'Otwórz plik',
                                            'c:\\', "Pliki Excel (*.xlsx *.xlsm)")
        lename.setText(ntpath.basename(fpath[0]))
        self.paths[nr] = fpath[0]

    def send_values(self):
        self.close()
        self.main.get_data(self.paths, str(self.cbraports.currentText()))



    def add_new_competitive(self):
        """
        add new competitive name to data base
        :return:
        """

        new_name = self.leaddraport.text()
        if self.check_is_not_empty(new_name):
            """
            add new name to data base
            """
            try:
                session = Session()
                name = Competitive(new_name)
                session.add(name)
                session.commit()
                session.close()

            except exc.IntegrityError:
                QMessageBox.critical(self, "Bład", "Błąd zapisu danych do bazy.\n Podana nazwa już istnieje")
                return

            """
            add value to combo box and clear line edit
            """
            self.leaddraport.setText('')
            self.cbraports.addItem(new_name)
            self.cbrapo_remove.addItem(new_name)
            self.cbraports.setCurrentText(new_name)

    def populate_row_competitive(self):
        """
        read rows from database with competitive name
        :return:
        """
        try:
            session = Session()
            competits = session.query(Competitive).all()

        except:
            QMessageBox.critical(self, "Błąd", "Nie można połączyć się z bazą danych")
            return

        for compet in competits:
            self.cbraports.addItem(compet.name)
            self.cbrapo_remove.addItem((compet.name))

    def remove_competitive(self):
        """
        remove compatitive from comboboxes and database
        :return:
        """

        reply = QMessageBox.information(self, 'Uwaga!!!',
                     "Usniecia raportu spowoduje usunicie powiązanych danych.\n Kliknij nie aby anulować operacje. ",
                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            cur_text = self.cbrapo_remove.currentText()
            session = Session()
            session.query(Competitive).filter_by(name = cur_text).delete()
            session.commit()
            session.close()
            self.cbrapo_remove.removeItem(self.cbrapo_remove.currentIndex())
            self.cbraports.removeItem(self.cbraports.findText(cur_text))

    @staticmethod
    def check_is_not_empty(text):
        """
        check the line edit with new client is not empty
        """
        if len(text) == 0:
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Critical)
            msg.setText("Wartość w polu nie może być pusta")
            msg.setWindowTitle("Błąd")
            msg.setStandardButtons(QMessageBox.Ok)
            msg.exec_()
            return False
        return True



if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle('fusion')
    window = FileChoser()
    window.show()
    sys.exit(app.exec_())






