import os
import sys
import time

import uiautomator2 as u2
from cnocr import CnOcr


def say_poc(serial):
    d = u2.connect(serial)
    d.screen_on()
    d.unlock()
    d.debug = False

    d.app_stop("com.instagram.android")
    d.app_start("com.instagram.android", use_monkey=True)
    time.sleep(3)

    """搜索"""
    d.xpath('//*[@resource-id="com.instagram.android:id/search_tab"]/android.widget.ImageView[1]').click(timeout=3)
    time.sleep(1)
    d.xpath('//*[@resource-id="com.instagram.android:id/action_bar_search_edit_text"]').click(timeout=3)
    d(focused=True).set_text('wudejames')
    time.sleep(3)
    if d.xpath('//*[@resource-id="com.instagram.android:id/row_search_user_username"]').get_text() == 'wudejames':
        d.xpath('//*[@resource-id="com.instagram.android:id/row_search_user_username"]').click()

        """发消息"""
        d.xpath('//*[@text="发消息"]').click(timeout=3)

        """如果存在弹出框"""
        if d.xpath('//*[@resource-id="com.instagram.android:id/bb_primary_action"]').exists:
            d.xpath('//*[@resource-id="com.instagram.android:id/bb_primary_action"]').click(timeout=3)

        d.xpath('//*[@resource-id="com.instagram.android:id/row_thread_composer_edittext"]').click(timeout=3)
        time.sleep(1)
        d(focused=True).set_text('Good evening')
        d.xpath('//*[@resource-id="com.instagram.android:id/row_thread_composer_send_button_icon"]').click(timeout=3)


def spider_poc(serial):
    d = u2.connect(serial)
    d.screen_on()
    d.unlock()
    d.debug = False

    d.app_stop("com.instagram.android")
    d.app_start("com.instagram.android", use_monkey=True)

    time.sleep(3)

    """滑动一屏幕"""
    # d.swipe_ext('up', scale=1)
    # time.sleep(1)

    root_dir = os.path.dirname(sys.argv[0])
    screenshot_name = f'screenshots/{time.time()}.png'
    screenshot_dir = os.path.join(root_dir, screenshot_name)
    d.screenshot().save(screenshot_dir)

    ocr = CnOcr()
    res = ocr.ocr(screenshot_dir)
    # print(res)

    # 找到包含“你的快拍”的元素
    target_text = '你的快拍'
    i = 0
    for i, item in enumerate(res):
        if target_text in item['text']:
            break

    if i < len(res) - 1:
        next_text = res[i + 1]['text']
        # print(f"“{target_text}”后的文字是：“{next_text}”")
        print(f"昵称：{next_text}")
    else:
        print("未找到昵称")

    """点赞"""
    # d.xpath('//*[@resource-id="com.instagram.android:id/row_feed_button_like"]').click(timeout=3)
    d.double_click(0.51, 0.493)
    time.sleep(3)

    """搜索"""
    d.xpath('//*[@resource-id="com.instagram.android:id/search_tab"]/android.widget.ImageView[1]').click(timeout=3)
    time.sleep(1)
    d.xpath('//*[@resource-id="com.instagram.android:id/action_bar_search_edit_text"]').click(timeout=3)
    d(focused=True).set_text('电子元器件')
    time.sleep(3)
    d.xpath('//*[@resource-id="com.instagram.android:id/row_search_profile_image"]').click(timeout=3)

    """点击账户"""
    d.xpath('//*[@text="帐户"]').click()


if __name__ == '__main__':
    say_poc("5D7HSG5XXCKVBI85")
