"""司机端 APP — 运输中（装货/卸货） Page Object。

Usage:
    transport = DriverTransportPage(driver)
    transport.load_goods(order_no)
    transport.unload_goods(order_no)
"""

from tests.app._base_page import AppBasePage


class DriverTransportPage(AppBasePage):
    """司机端 APP - 运输中 - 装货/卸货。"""

    def __init__(self, driver, platform: str = "android"):
        super().__init__(driver, platform)
        # ⚠️ 以下 accessibility_id 需要根据实际 APP 元素 ID 调整
        # 使用 Appium Desktop Inspector 或 uiautomatorviewer 获取

        # ── 导航 tab ──
        self.my_orders_tab = "tab_orders"                   # ⚠️ 我的运单 tab

        # ── 运单列表/详情 ──
        self.order_item = "order_item"                      # ⚠️ 运单项
        self.order_detail_btn = "btn_order_detail"          # ⚠️ 进入详情

        # ── 装货 ──
        self.load_goods_btn = "btn_load_goods"              # ⚠️ 装货按钮
        self.confirm_load_btn = "btn_confirm"               # ⚠️ 确认装货
        self.upload_load_photo_btn = "btn_upload_photo"     # ⚠️ 上传装货照片 (如有)
        self.load_success_text = "装货成功"                 # ⚠️ 成功提示

        # ── 卸货 ──
        self.unload_goods_btn = "btn_unload_goods"          # ⚠️ 卸货按钮
        self.confirm_unload_btn = "btn_confirm"             # ⚠️ 确认卸货
        self.upload_unload_photo_btn = "btn_upload_photo"   # ⚠️ 上传卸货照片 (如有)
        self.unload_success_text = "卸货成功"               # ⚠️ 成功提示

    def go_to_my_orders(self) -> None:
        """切换到我的运单 tab。"""
        self.tap_by_id(self.my_orders_tab)
        self.driver.implicitly_wait(1)

    def find_and_open_order(self, order_no: str) -> None:
        """查找并打开指定运单的详情页。

        Args:
            order_no: 订单号
        """
        # ⚠️ 策略取决于 APP 实际布局:
        # 策略1: 通过订单号文本定位并点击
        try:
            self.tap_by_text(order_no)
            self.driver.implicitly_wait(1)
            return
        except Exception:
            pass

        # 策略2: 滚动查找
        for _ in range(5):  # 最多滚动 5 次
            try:
                self.tap_by_text(order_no)
                self.driver.implicitly_wait(1)
                return
            except Exception:
                self.scroll_down()

        raise RuntimeError(f"在运单列表中未找到订单: {order_no}")

    def load_goods(self, order_no: str) -> None:
        """执行装货流程。

        Args:
            order_no: 订单号
        """
        self.go_to_my_orders()
        self.find_and_open_order(order_no)

        # 点击装货
        self.tap_by_id(self.load_goods_btn)
        self.driver.implicitly_wait(1)

        # 上传照片 (如有)
        try:
            self.tap_by_id(self.upload_load_photo_btn)
            self.driver.implicitly_wait(1)
        except Exception:
            pass

        # 确认装货
        self.tap_by_id(self.confirm_load_btn)
        self.driver.implicitly_wait(2)

    def unload_goods(self, order_no: str) -> None:
        """执行卸货流程。

        Args:
            order_no: 订单号
        """
        self.go_to_my_orders()
        self.find_and_open_order(order_no)

        # 点击卸货
        self.tap_by_id(self.unload_goods_btn)
        self.driver.implicitly_wait(1)

        # 上传照片 (如有)
        try:
            self.tap_by_id(self.upload_unload_photo_btn)
            self.driver.implicitly_wait(1)
        except Exception:
            pass

        # 确认卸货
        self.tap_by_id(self.confirm_unload_btn)
        self.driver.implicitly_wait(2)

    def verify_load_complete(self) -> bool:
        """验证装货完成。"""
        try:
            self.wait_visible_by_text(self.load_success_text, timeout_s=5)
            return True
        except Exception:
            return False

    def verify_unload_complete(self) -> bool:
        """验证卸货完成。"""
        try:
            self.wait_visible_by_text(self.unload_success_text, timeout_s=5)
            return True
        except Exception:
            return False
