from PySide6.QtWidgets import (
    QHBoxLayout,
    QLabel,
    QLineEdit,
    QPushButton,
    QTableWidget,
    QTableWidgetItem,
    QVBoxLayout,
    QWidget,
)

from pypos.core.base_view import BaseView
from pypos.modules.customer.controllers.customer_setup_controller import CustomerSetupController


class CustomerSetupView(BaseView, QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.controller = CustomerSetupController(self)
        self._build_ui()
        self.controller.initialize()

    def _build_ui(self):
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        header = QLabel("Setup Customer")
        header.setStyleSheet("font-weight: bold; font-size: 20px; margin: 10px;")
        layout.addWidget(header)

        search_layout = QHBoxLayout()
        self.search_input = QLineEdit()
        self.search_input.setPlaceholderText("Cari Nama Customer...")
        search_button = QPushButton("Cari")
        search_button.clicked.connect(self.controller.on_search)
        search_layout.addWidget(self.search_input)
        search_layout.addWidget(search_button)
        layout.addLayout(search_layout)

        form_layout = QVBoxLayout()
        self.nama_input = QLineEdit()
        self.alamat_input = QLineEdit()
        self.telepon_input = QLineEdit()
        self.submit_button = QPushButton("Tambah Customer")
        self.submit_button.clicked.connect(self.controller.on_submit)

        form_layout.addWidget(QLabel("Nama:"))
        form_layout.addWidget(self.nama_input)
        form_layout.addWidget(QLabel("Alamat:"))
        form_layout.addWidget(self.alamat_input)
        form_layout.addWidget(QLabel("Telepon:"))
        form_layout.addWidget(self.telepon_input)
        form_layout.addWidget(self.submit_button)
        layout.addLayout(form_layout)

        self.table = QTableWidget()
        self.table.setColumnCount(5)
        self.table.setHorizontalHeaderLabels(["ID", "Nama", "Alamat", "Telepon", "Aksi"])
        self.table.setStyleSheet("QTableWidget::item:selected { background: #d0d0d0; color: black; }")
        layout.addWidget(self.table)

        bottom_layout = QHBoxLayout()
        self.prev_button = QPushButton("Previous")
        self.next_button = QPushButton("Next")
        self.prev_button.clicked.connect(self.controller.go_prev_page)
        self.next_button.clicked.connect(self.controller.go_next_page)
        bottom_layout.addWidget(self.prev_button)
        bottom_layout.addWidget(self.next_button)
        layout.addLayout(bottom_layout)

    def get_search_keyword(self):
        return str(self.search_input.text() or "").strip()

    def get_form_values(self):
        return (
            str(self.nama_input.text() or "").strip(),
            str(self.alamat_input.text() or "").strip(),
            str(self.telepon_input.text() or "").strip(),
        )

    def set_form_values(self, nama, alamat, telepon):
        self.nama_input.setText(str(nama or ""))
        self.alamat_input.setText(str(alamat or ""))
        self.telepon_input.setText(str(telepon or ""))

    def set_submit_button_label(self, label):
        self.submit_button.setText(str(label or "Tambah Customer"))

    def reset_form(self):
        self.nama_input.clear()
        self.alamat_input.clear()
        self.telepon_input.clear()

    def render_customer_rows(self, rows, on_edit, on_delete):
        self.table.setRowCount(0)
        for row_index, row in enumerate(rows or []):
            self.table.insertRow(row_index)
            for col_index, value in enumerate(row):
                self.table.setItem(row_index, col_index, QTableWidgetItem(str(value)))

            edit_button = QPushButton("Edit")
            edit_button.clicked.connect(lambda _, customer_id=row[0]: on_edit(customer_id))

            delete_button = QPushButton("Hapus")
            delete_button.clicked.connect(lambda _, customer_id=row[0]: on_delete(customer_id))

            action_layout = QHBoxLayout()
            action_layout.addWidget(edit_button)
            action_layout.addWidget(delete_button)
            action_layout.setContentsMargins(0, 0, 0, 0)

            action_widget = QWidget()
            action_widget.setLayout(action_layout)
            self.table.setCellWidget(row_index, 4, action_widget)

    def update_pagination_state(self, current_page, items_per_page, total_data):
        has_prev = current_page > 0
        has_next = ((current_page + 1) * items_per_page) < total_data
        self.prev_button.setEnabled(has_prev)
        self.next_button.setEnabled(has_next)
