# edited by glg
from typing import Tuple

from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
    QFormLayout,
    QFrame,
    QGroupBox,
    QLabel,
    QScrollArea,
    QSizePolicy,
    QVBoxLayout,
    QWidget,
)

_ALIGN_RIGHT = Qt.AlignmentFlag.AlignRight
_ALIGN_VCENTER = Qt.AlignmentFlag.AlignVCenter
_ALIGN_TOP = Qt.AlignmentFlag.AlignTop
_SCROLLBAR_ALWAYS_OFF = Qt.ScrollBarPolicy.ScrollBarAlwaysOff
_SCROLLBAR_AS_NEEDED = Qt.ScrollBarPolicy.ScrollBarAsNeeded
_SIZE_EXPANDING = QSizePolicy.Policy.Expanding
_SIZE_FIXED = QSizePolicy.Policy.Fixed
_SIZE_PREFERRED = QSizePolicy.Policy.Preferred


class TransaksiInfoPembayaranPanelMixin:
    # edited by glg
    @staticmethod
    def _build_info_label(text, css_class="tpv-info-label"):
        label = QLabel(text)
        label.setProperty("class", css_class)
        return label

    # edited by glg
    @staticmethod
    def _build_info_value_label(
        text,
        css_class="tpv-info-value",
        alignment=_ALIGN_RIGHT | _ALIGN_VCENTER,
        word_wrap=False,
    ):
        label = QLabel(text)
        label.setProperty("class", css_class)
        label.setAlignment(alignment)
        label.setWordWrap(bool(word_wrap))
        return label

    # edited by glg
    def _setup_info_pembayaran_panel(self, info_pembayaran_min_height: int) -> None:
        (
            label_total_produk,
            label_diskon,
            label_free_item,
            label_total_bayar,
        ) = self._init_info_pembayaran_labels_and_values()
        self._init_info_pembayaran_layout()
        self._populate_info_pembayaran_rows(
            label_total_produk=label_total_produk,
            label_diskon=label_diskon,
            label_free_item=label_free_item,
        )
        self._build_info_pembayaran_footer(label_total_bayar=label_total_bayar)
        self._finalize_info_pembayaran_panel(info_pembayaran_min_height=info_pembayaran_min_height)

    # edited by glg
    def _init_info_pembayaran_layout(self) -> None:
        self.info_pembayaran_group = QGroupBox("")
        self.info_pembayaran_main_layout = QVBoxLayout()
        self.info_pembayaran_main_layout.setContentsMargins(6, 4, 6, 4)
        self.info_pembayaran_main_layout.setSpacing(4)

        # edited by glg
        # Detail info pembayaran dibuat scrollable; total bayar ditempel
        # di footer agar selalu terlihat (sticky).
        self.info_pembayaran_scroll = QScrollArea()
        self.info_pembayaran_scroll.setWidgetResizable(True)
        self.info_pembayaran_scroll.setFrameShape(QFrame.NoFrame)
        self.info_pembayaran_scroll.setHorizontalScrollBarPolicy(_SCROLLBAR_ALWAYS_OFF)
        self.info_pembayaran_scroll.setVerticalScrollBarPolicy(_SCROLLBAR_AS_NEEDED)

        self.info_pembayaran_scroll_content = QWidget()
        self.info_pembayaran_layout = QFormLayout()
        self.info_pembayaran_layout.setSpacing(3)
        self.info_pembayaran_layout.setContentsMargins(0, 0, 0, 0)
        self.info_pembayaran_layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow)
        self.info_pembayaran_title_label = self._build_info_label(
            "Info Pembayaran",
            css_class="tpv-info-pembayaran-title",
        )

    # edited by glg
    def _init_info_pembayaran_labels_and_values(self) -> Tuple[QLabel, QLabel, QLabel, QLabel]:
        label_total_produk = self._build_info_label("(BELUM TERMASUK POTONGAN) BRUTO JUAL:")
        label_diskon = self._build_info_label("TOTAL DISKON PRODUK:")
        self.label_diskon_member_persen = self._build_info_label("DISKON MEMBER (%):")
        self.label_diskon_customer = self._build_info_label("DISKON MEMBER (RP):")
        self.label_additional_diskon = self._build_info_label("ADDITIONAL DISKON:")
        self.label_grand_total = self._build_info_label("GRAND TOTAL:")
        self.label_cashback = self._build_info_label("CASHBACK:")
        self.label_point = self._build_info_label("POINT:")
        label_free_item = self._build_info_label("FREE ITEM:")
        label_total_bayar = self._build_info_label("Total yang harus dibayar:", css_class="tpv-info-total-label")

        self.info_total_produk_label = self._build_info_value_label("0")
        self.info_diskon_label = self._build_info_value_label("0")
        self.info_diskon_member_persen_label = self._build_info_value_label("0.00 %")
        self.info_diskon_customer_label = self._build_info_value_label("0")
        self.info_additional_diskon_label = self._build_info_value_label("0")
        self.info_grand_total_label = self._build_info_value_label("0")
        self.info_cashback_label = self._build_info_value_label("0")
        self.info_point_label = self._build_info_value_label("0")
        self.info_free_item_label = self._build_info_value_label(
            "-",
            alignment=_ALIGN_RIGHT | _ALIGN_TOP,
            word_wrap=True,
        )
        self.total_label = self._build_info_value_label(
            "0",
            css_class="tpv-info-total-value",
        )
        # edited by glg
        # Pastikan nominal total tetap tampil di resolusi kecil.
        self.total_label.setWordWrap(False)
        self.total_label.setMinimumWidth(150)
        self.total_label.setMinimumHeight(30)
        self.total_label.setSizePolicy(_SIZE_EXPANDING, _SIZE_FIXED)
        return label_total_produk, label_diskon, label_free_item, label_total_bayar

    # edited by glg
    def _populate_info_pembayaran_rows(
        self,
        *,
        label_total_produk: QLabel,
        label_diskon: QLabel,
        label_free_item: QLabel,
    ) -> None:
        # edited by glg
        # Judul info pembayaran ditempatkan di luar area scroll agar selalu
        # terlihat dan tidak ikut bergulir.
        self.info_pembayaran_main_layout.addWidget(self.info_pembayaran_title_label, 0)
        self.info_pembayaran_layout.addRow(label_total_produk, self.info_total_produk_label)
        self.info_pembayaran_layout.addRow(label_diskon, self.info_diskon_label)
        self.info_pembayaran_layout.addRow(self.label_diskon_member_persen, self.info_diskon_member_persen_label)
        self.info_pembayaran_layout.addRow(self.label_diskon_customer, self.info_diskon_customer_label)
        self.info_pembayaran_layout.addRow(self.label_additional_diskon, self.info_additional_diskon_label)
        self.info_pembayaran_layout.addRow(self.label_grand_total, self.info_grand_total_label)
        self.info_pembayaran_layout.addRow(self.label_cashback, self.info_cashback_label)
        self.info_pembayaran_layout.addRow(self.label_point, self.info_point_label)
        self.info_pembayaran_layout.addRow(label_free_item, self.info_free_item_label)

        self.info_pembayaran_scroll_content.setLayout(self.info_pembayaran_layout)
        self.info_pembayaran_scroll.setWidget(self.info_pembayaran_scroll_content)

    # edited by glg
    def _build_info_pembayaran_footer(self, *, label_total_bayar: QLabel) -> None:
        self.info_pembayaran_footer_widget = QWidget()
        self.info_pembayaran_footer_layout = QVBoxLayout()
        self.info_pembayaran_footer_layout.setContentsMargins(0, 0, 0, 0)
        self.info_pembayaran_footer_layout.setSpacing(3)

        self.info_pembayaran_divider = QFrame()
        self.info_pembayaran_divider.setFrameShape(QFrame.HLine)
        self.info_pembayaran_divider.setProperty("class", "tpv-info-divider")
        self.info_pembayaran_footer_layout.addWidget(self.info_pembayaran_divider)

        self.info_pembayaran_total_layout = QFormLayout()
        self.info_pembayaran_total_layout.setContentsMargins(0, 0, 0, 0)
        self.info_pembayaran_total_layout.setSpacing(3)
        self.info_pembayaran_total_layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow)
        self.info_pembayaran_total_layout.addRow(label_total_bayar, self.total_label)
        self.info_pembayaran_footer_layout.addLayout(self.info_pembayaran_total_layout)
        self.info_pembayaran_footer_widget.setLayout(self.info_pembayaran_footer_layout)
        self.info_pembayaran_footer_widget.setSizePolicy(_SIZE_PREFERRED, _SIZE_FIXED)

        self.info_pembayaran_main_layout.addWidget(self.info_pembayaran_scroll, 1)
        self.info_pembayaran_main_layout.addWidget(self.info_pembayaran_footer_widget, 0)

    # edited by glg
    def _finalize_info_pembayaran_panel(self, *, info_pembayaran_min_height: int) -> None:
        self.info_pembayaran_group.setLayout(self.info_pembayaran_main_layout)
        resolved_info_min_h = max(120, int(info_pembayaran_min_height or 0))
        self.info_pembayaran_group.setMinimumHeight(resolved_info_min_h)
        self.info_pembayaran_group.setProperty("class", "tpv-info-pembayaran")
        self.info_pembayaran_group.setSizePolicy(_SIZE_PREFERRED, _SIZE_EXPANDING)
