from .widget_sources.Ui_icon_widget import Ui_Dialog
from PyQt6 import QtWidgets
from src import logger


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]
            )
        logger.debug(
            "Icon changed to: {}", file_dialog.selectedFiles()[0].split("/")[-1]
        )

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