from PyQt6 import QtWidgets

from src.logic.dataclass import BookData

from .dialog_sources.Ui_edit_bookdata import Ui_Dialog


class BookDataUI(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

    def populate_fields(self, data: BookData):
        self.line_author.setText(data.author)
        self.line_edition.setText(data.edition)
        self.line_isbn.setText(", ".join(data.isbn))
        self.line_lang.setText(data.language)
        self.line_link.setText(data.link)
        self.line_pages.setText(data.pages)
        self.line_ppn.setText(data.ppn)
        self.line_publisher.setText(data.publisher)
        self.line_signature.setText(data.signature)
        self.line_title.setText(data.title)
        self.line_year.setText(data.year)

    def get_data(self) -> BookData:
        return BookData(
            ppn=self.line_ppn.text().strip(),
            title=self.line_title.text().strip(),
            signature=self.line_signature.text().strip(),
            edition=self.line_edition.text().strip(),
            link=self.line_link.text().strip(),
            isbn=self.line_isbn.text().split(","),
            author=self.line_author.text().strip(),
            language=self.line_lang.text().strip(),
            publisher=self.line_publisher.text().strip(),
            year=self.line_year.text().strip(),
            pages=self.line_pages.text().strip(),
        )
