from .widget_sources.icon_widget_ui import Ui_Dialog
from PySide6 import QtWidgets

import sys
from loguru import logger as log

logger = log
logger.remove()
logger.add("logs/application.log", rotation="1 week", retention="1 month", enqueue=True)


# logger.add(sys.stderr, format="{time} {level} {message}", level="INFO")
logger.add(sys.stdout)


class IconWidget(QtWidgets.QWidget, Ui_Dialog):
    def __init__(self, icon_name: str, icon_filename: str):
        super(IconWidget, self).__init__(None)
        self.setupUi(self)
        self.btn_change_icon.clicked.connect(self.change_icon)
        self.icon_filename_line.setText(icon_filename)
        self.icon_name_settings.setText(icon_name)

    def change_icon(self):
        file_dialog = QtWidgets.QFileDialog()
        file_dialog.setFileMode(QtWidgets.QFileDialog.FileMode.ExistingFile)
        file_dialog.setNameFilter("Images (*.ico *.svg)")
        if file_dialog.exec():
            self.icon_filename_line.setText(
                file_dialog.selectedFiles()[0].split("/")[-1]
            )
        try:
            log.debug(
                "Icon changed to: {}", file_dialog.selectedFiles()[0].split("/")[-1]
            )
        except IndexError:
            log.debug("No file selected")

    def return_data(self):
        return self.icon_name_settings.text(), self.icon_filename_line.text()
