from pathlib import Path

from PySide6.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QHBoxLayout, QPushButton, QFileDialog, \
    QCheckBox, QComboBox


class ExportKontorDialog(QDialog):
    def __init__(self, parent=None, kontor_db=None):
        super().__init__(parent)

        self.parent = parent
        self.kontor_db = kontor_db
        self.file_name = "data.json"
        self.tables = []
        self._table_options = {}

        self.export_options = {"JSON": {"ext": ".json"}, "YAML": {"ext": ".yaml"}, "SQLite": {"ext": ".db"}}
        self.current_export_type = "JSON"

        buttons = (QDialogButtonBox.Ok | QDialogButtonBox.Cancel)

        self.buttonBox = QDialogButtonBox(buttons)
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

        layout = QVBoxLayout()

        self.label = QLabel()
        self.label.setText("Export DB to data.json")

        self.combo_box = QComboBox()
        self.combo_box.addItems(["JSON", "YAML", "SQLite"])
        self.combo_box.currentTextChanged.connect(self.change_export_type)
        file_layout = QHBoxLayout()
        file_layout.addWidget(self.label)
        file_layout.addWidget(self.combo_box)
        file_button = QPushButton("Select file")
        file_button.clicked.connect(self.select_file)
        file_layout.addWidget(file_button)
        layout.addLayout(file_layout)

        for table_name in self.kontor_db.get_table_names():
            check_box = QCheckBox(table_name)
            check_box.setChecked(True)
            self.tables.append(table_name)
            self._table_options[table_name] = check_box
            check_box.stateChanged.connect(self.change_selection)
            layout.addWidget(check_box)
        layout.addWidget(self.buttonBox)
        self.setLayout(layout)

    def change_selection(self):
        self.tables.clear()
        for (name, box) in self._table_options.items():
            if box.isChecked():
                self.tables.append(name)

    def change_export_type(self, text):
        self.current_export_type = text
        self.label.setText(f'Export DB to data.{self.export_options[text]["ext"]}')

    def select_file(self):
        file_dialog = QFileDialog()
        file_dialog.setFileMode(QFileDialog.FileMode.AnyFile)
        file_dialog.setDefaultSuffix(self.export_options[self.current_export_type]["ext"])
        file_dialog.setNameFilter(f'*{self.export_options[self.current_export_type]["ext"]}')
        if file_dialog.exec():
            self.file_name = file_dialog.selectedFiles()[0]
            export_file = Path(self.file_name)
            self.file_name = export_file.with_suffix(self.export_options[self.current_export_type]["ext"])
            self.label.setText(f"Export DB to {self.file_name}")

    def get_tables_to_export(self) -> list:
        return self.tables


class ImportKontorDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.file_name = None

        QBtn = (QDialogButtonBox.Ok | QDialogButtonBox.Cancel)

        self.buttonBox = QDialogButtonBox(QBtn)
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

        self.label = QLabel()
        self.label.setText("Import DB from data.json")
        layout = QVBoxLayout()
        file_layout = QHBoxLayout()
        file_layout.addWidget(self.label)
        file_button = QPushButton("Select file")
        file_button.clicked.connect(self.select_file)
        file_layout.addWidget(file_button)
        layout.addLayout(file_layout)
        layout.addWidget(self.buttonBox)
        self.setLayout(layout)

    def select_file(self):
        file_dialog = QFileDialog()
        file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile)
        if file_dialog.exec():
            self.file_name = file_dialog.selectedFiles()[0]
            self.label.setText(f"Import DB from {self.file_name}")
