"""
mobile_sim_dialog.py — Giao diện mô phỏng eTax Mobile trên điện thoại.
Giúp người dân dễ dàng đối chiếu và nhập liệu vào App của Tổng cục Thuế.
"""
from PySide6.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QLabel, QFrame, 
    QScrollArea, QPushButton, QWidget
)
from PySide6.QtCore import Qt
from PySide6.QtGui import QFont, QColor

from gui.theme import COLORS

class MobileSimDialog(QDialog):
    def __init__(self, tax_data: dict, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Mô phỏng eTax Mobile")
        self.setFixedSize(380, 680)
        self.setWindowFlags(self.windowFlags() | Qt.WindowType.FramelessWindowHint)
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
        
        self.tax_data = tax_data or {}
        self._init_ui()

    def _init_ui(self):
        # Root layout cho dialog (căn giữa phone frame)
        root = QVBoxLayout(self)
        root.setContentsMargins(0, 0, 0, 0)
        root.setAlignment(Qt.AlignmentFlag.AlignCenter)

        # Outer "Phone" container — dùng setFixedSize thay setGeometry
        self.phone_frame = QFrame()
        self.phone_frame.setFixedSize(380, 680)   # ← FIX: không dùng setGeometry
        self.phone_frame.setStyleSheet(f"""
            QFrame {{
                background-color: #f8fafc;
                border: 12px solid #1e293b;
                border-radius: 40px;
            }}
        """)
        root.addWidget(self.phone_frame)

        layout = QVBoxLayout(self.phone_frame)
        layout.setContentsMargins(20, 40, 20, 30)
        layout.setSpacing(0)


        # Status Bar Mockup
        status_bar = QHBoxLayout()
        time_lbl = QLabel("10:00")
        time_lbl.setStyleSheet("color: #1e293b; font-weight: bold; background: transparent;")
        status_bar.addWidget(time_lbl)
        status_bar.addStretch()
        icons_lbl = QLabel("📶 🔋")
        icons_lbl.setStyleSheet("background: transparent;")
        status_bar.addWidget(icons_lbl)
        layout.addLayout(status_bar)
        layout.addSpacing(20)

        # Header
        header = QLabel("TỜ KHAI 01/CNKD")
        header.setFont(QFont("Segoe UI", 14, QFont.Weight.Bold))
        header.setAlignment(Qt.AlignmentFlag.AlignCenter)
        header.setStyleSheet("color: #c0392b; background: transparent;")
        layout.addWidget(header)
        layout.addSpacing(15)

        # Instructions
        instr = QLabel("Hãy nhập các số dưới đây vào App eTax trên điện thoại của bạn:")
        instr.setWordWrap(True)
        instr.setAlignment(Qt.AlignmentFlag.AlignCenter)
        instr.setStyleSheet("color: #64748b; font-size: 11px; background: transparent;")
        layout.addWidget(instr)
        layout.addSpacing(20)

        # Content Area (Scrollable)
        scroll = QScrollArea()
        scroll.setWidgetResizable(True)
        scroll.setFrameShape(QFrame.Shape.NoFrame)
        scroll.setStyleSheet("background: transparent;")
        
        content = QWidget()
        content.setStyleSheet("background: transparent;")
        content_layout = QVBoxLayout(content)
        content_layout.setSpacing(12)

        # Helper to create fields
        def add_field(code, label, value):
            box = QFrame()
            box.setStyleSheet(f"background: white; border: 1px solid #e2e8f0; border-radius: 8px;")
            box_layout = QVBoxLayout(box)
            
            lbl_row = QHBoxLayout()
            code_lbl = QLabel(f"[{code}]")
            code_lbl.setStyleSheet("color: #e67e22; font-weight: bold; border: none;")
            name_lbl = QLabel(label)
            name_lbl.setStyleSheet("color: #1e293b; font-weight: 500; border: none;")
            lbl_row.addWidget(code_lbl)
            lbl_row.addWidget(name_lbl)
            lbl_row.addStretch()
            
            val_lbl = QLabel(f"{value:,.0f}")
            val_lbl.setFont(QFont("Segoe UI", 12, QFont.Weight.Bold))
            val_lbl.setAlignment(Qt.AlignmentFlag.AlignRight)
            val_lbl.setStyleSheet("color: #2c3e50; border: none; padding-top: 5px;")
            
            box_layout.addLayout(lbl_row)
            box_layout.addWidget(val_lbl)
            content_layout.addWidget(box)

        # Add specific eTax fields
        add_field("20", "Tổng doanh thu phát sinh", self.tax_data.get('revenue', 0))
        add_field("25", "Doanh thu tính thuế GTGT", self.tax_data.get('revenue', 0))
        add_field("26", "Số thuế GTGT phải nộp", self.tax_data.get('gtgt_due', 0))
        add_field("30", "Doanh thu tính thuế TNCN", self.tax_data.get('revenue', 0))
        add_field("31", "Số thuế TNCN phải nộp", self.tax_data.get('tncn_due', 0))
        
        content_layout.addStretch()
        scroll.setWidget(content)
        layout.addWidget(scroll)

        # Bottom Button
        close_btn = QPushButton("Đã hiểu & Đóng")
        close_btn.setFixedHeight(45)
        close_btn.setStyleSheet(f"""
            QPushButton {{
                background-color: #c0392b;
                color: white;
                border-radius: 12px;
                font-weight: bold;
                font-size: 14px;
            }}
            QPushButton:hover {{
                background-color: #a93226;
            }}
        """)
        close_btn.clicked.connect(self.accept)
        layout.addSpacing(15)
        layout.addWidget(close_btn)
        
        # Center the dialog manually since it's frameless
        main_geo = self.parent().geometry() if self.parent() else None
        if main_geo:
            self.move(main_geo.center().x() - 190, main_geo.center().y() - 340)
