import time

import uiautomator2 as u2

# 连接到设备，默认连接第一个设备，如果有多个设备可以指定序列号
d = u2.connect('127.0.0.1:6555')

# 获取所有运行的APP
running_apps = d.app_list_running()
print("安装列表：", running_apps)
# 如果app没有启动， 启动APP
if "com.niocpeed.dna" not in running_apps:
    print("com niocpeed.dna 重新启动")
    d.app_start("com.niocpeed.dna")
    # tab 交易按钮
btn_jy = d.xpath('//*[@content-desc="交易"]')
# btn_jy.click()
# 交易方式选择
btn_sel = d.xpath('//*[@content-desc="USDT合约"]')
if btn_sel.exists:
    btn_sel.click()

# 设置委托
input_weituo = d.xpath('//*[@content-desc="最优"]')
# 设置数量
input_num = d.xpath('//android.widget.ScrollView/android.view.View[18]/android.widget.EditText[1]')
btn_buy = d(description="买入/开多")

buy_x, buy_y = btn_buy.center()

start1 = time.time()
input_weituo.click()
end1 = time.time()
print("点击：最优委托 操作耗时：", end1 - start1)

# start1 = time.time()
# input_weituo.set_text("0.114")
# end1 = time.time()
# print("设置：操作耗时：", end1 - start1)
start1 = time.time()
input_num.click()
end1 = time.time()
print("点击数量：操作耗时：", end1 - start1)
# start1 = time.time()
# input_num.set_text("40")
# end1 = time.time()
# print("设置数量：操作耗时：", end1 - start1)

# for i in range(10):
#     # 初始化点击按钮
#     start = time.time()
#     # start1 = time.time()
#     # input_weituo.click()
#     # end1 = time.time()
#     # print("点击：最优委托 操作耗时：", end1 - start1)
#     #
#     # # start1 = time.time()
#     # # input_weituo.set_text("0.114")
#     # # end1 = time.time()
#     # # print("设置：操作耗时：", end1 - start1)
#     # start1 = time.time()
#     # input_num.click()
#     # end1 = time.time()
#     # print("点击：操作耗时：", end1 - start1)
#     # start1 = time.time()
#     # input_num.set_text("40")
#     # end1 = time.time()
#     # print("设置：操作耗时：", end1 - start1)
#     start1 = time.time()
#     #  直接点击   xpath直接点击
#     # btn_buy.click()
#     # 比上面的快50毫秒
#     # d.click(1078, 1094)  # ui点击 指定坐标
#     # 比上面的快100毫秒  直接调用shell 指定坐标
#     d.shell(f"input tap {buy_x} {buy_y}")
#     end1 = time.time()
#     print("点击下单：操作耗时：", end1 - start1)
#     end = time.time()
#     # print("点击整体：操作耗时：", end - start)
#     time.sleep(5)

# 给开仓确认的弹框 选中 下次不在提醒，能减少时间
# d(description="确认").click()
# d.xpath('//*[@content-desc="确认"]').click()


# button = d.xpath("//android.widget.Button[@text='Button']")
