import logging
import unittest

from control.plat.deepcoin_control import DeepCoinControl
from control.ui.settings_control import SettingsControl

logging.basicConfig(level=logging.INFO, force=True)  # 设置日志级别


class DeepCoinTestCase(unittest.TestCase):
    def setUp(self):

        settings = SettingsControl('Settings')

        serial = '127.0.0.1:64047'

        settings.init_adb([f'adb connect {serial}'])

        self.dc = settings.connect_dict.get(serial).get('control')

    def test_to_top_swipe(self):
        """
        滑动到顶部
        """
        self.dc.to_top_swipe()

    def test_to_next_swipe(self):
        """
        滑动到下一屏
        """
        self.dc.to_next_swipe(y=0.6)

    def test_save_point(self):
        """
        保存坐标
        """
        self.dc.save_point()

    def test_slider_baozhengjin(self):
        """
        保证金滑块
        """
        self.dc.slider_baozhengjin_kaicang()

    def test_click_kaicang(self):
        """
        开仓
        """
        self.dc.click_kaicang()

    def test_click_pingchang(self):
        """
        平仓
        """
        self.dc.click_pingchang()

    def test_select_weituo_xianjia(self):
        """
        选择限价委托
        """
        self.dc.select_kaicang_weituo_xianjia()

    def test_click_price(self):
        """
        点击价格
        """
        self.dc.click_price_red()

    def test_f1(self):
        """
        f1 开仓界面，仓位滑竿百分比(30-60)
        """
        # self.pc.click_kaicang()
        # self.pc.slider_baozhengjin_kaicang()
        self.dc.event_f1()

    def test_f2(self):
        """
        f2 确认开仓，开多 限价职中间数值
        检查撤销限价委托单
        """
        # self.pc.click_kaicang()
        #
        # self.pc.select_kaicang_weituo_xianjia()
        # self.pc.d.sleep(0.1)
        #
        # self.pc.click_price_red()
        #
        # # self.pc.text_xiadan_shuliang(3)
        # self.pc.slider_baozhengjin_kaicang()
        #
        # self.pc.click_mairu_kaiduo()
        self.dc.event_f2()

    def test_f3(self):
        """
        F3 平仓界面 平空，仓位滑竿百分比(90-100)确认开仓，限价职中间数值
        """
        # self.pc.to_top_swipe(sleep=0.2)
        # self.pc.click_pingchang()
        # self.pc.slider_baozhengjin_pingcang(offset=1)
        self.dc.event_f3()

    def test_f4(self):
        """
        F4   确认平仓 平多 限价取中间数值 检查撤销限价委托单
        """
        # self.pc.to_top_swipe(sleep=0.2)
        # self.pc.click_pingchang()
        # self.pc.select_kaicang_weituo_xianjia()
        # self.pc.click_price_green()
        # # self.pc.text_xiadan_shuliang(3)
        # self.pc.slider_baozhengjin_pingcang(offset=1)
        # self.pc.click_maichu_pingduo()
        self.dc.event_f4()

    def test_f5(self):
        """
        F5 开仓界面胂仓位滑竿百分比(30-60)
        """
        # self.pc.to_top_swipe(sleep=0.2)
        # self.pc.click_kaicang()
        # self.pc.slider_baozhengjin_kaicang()
        self.dc.event_f5()

    def test_f6(self):
        """
        F6 确认开仓， 开空 限价值中间数值
        检查撤销限价委托单
        """
        # self.pc.to_top_swipe(sleep=0.1)
        # self.pc.click_kaicang()
        # self.pc.select_kaicang_weituo_xianjia()
        # self.pc.click_price_red()
        # # self.pc.text_xiadan_shuliang(3)
        # self.pc.click_maichu_kaikong()
        self.dc.event_f6()

    def test_f7(self):
        """
        F7    平仓界面二仓位滑竿百分比(90-100)
        """
        # self.pc.to_top_swipe(sleep=0.2)
        # self.pc.click_pingchang()
        # self.pc.slider_baozhengjin_pingcang(offset=2)
        self.dc.event_f7()

    def test_f8(self):
        """
        F8 确认平仓， 平空 限价职中间数值
        检查撤销限价委托单
        """
        # self.pc.to_top_swipe()
        #
        # self.pc.d.sleep(0.1)
        #
        # self.pc.click_pingchang()
        #
        # self.pc.click_price_red()
        #
        # # self.pc.text_xiadan_shuliang(3)
        # self.pc.click_maichu_kaikong()
        #
        # self.pc.click_mairu_pingkong()
        self.dc.event_f8()

    def test_event_10(self):
        self.dc.event_10()

    def test_event_11(self):
        self.dc.event_11()

    def test_jiao_yi(self):
        x, y, _ = self.dc.btn_jiao_yi()
        logging.info(f"交易坐标: （{x},{y}）")

    def test_mairu_kaiduo(self):
        x, y = self.dc.btn_mairu_kaiduo()
        logging.info(f"买入开多: （{x},{y}）")

    def test_chichang(self):
        self.dc.d.xpath()

    def test_gengdan(self):
        self.dc.to_next_swipe()
        x, y = self.dc.btn_gengdan()
        logging.info(f"跟单坐标: ({x},{y})")

    def test_weituo(self):
        self.dc.to_next_swipe()
        x, y, _ = self.dc.btn_weituo()
        logging.info(f"委托坐标: ({x},{y})")

    def test_gendan_click(self):
        self.dc.click_xpath('//*[starts-with(@content-desc, "跟单(")]')

    def test_yijianchedan_click(self):
        """
        一键撤单
        """
        self.dc.to_next_swipe()

        self.dc.click_yijian_chedan()

    def test_jiaoyi_select(self):
        element = self.dc.d.xpath('//*[@content-desc="交易"]').get()

        # 获取元素位置
        bounds = element.info['bounds']
        # 截图
        screenshot = self.dc.d.screenshot()
        # 转换为PIL图像
        from PIL import Image
        img = Image.fromarray(screenshot)
        # 获取元素中心点颜色
        x = (bounds['left'] + bounds['right']) // 2
        y = (bounds['top'] + bounds['bottom']) // 2
        return img.getpixel((x, y))

    def test_slider_baozhengjin_pingcang(self):
        """
        平仓情况  保证金滑块
        """
        self.dc.slider_baozhengjin_pingcang()

    def test_exists(self):
        # b = self.pc.check_element_exists('//*[starts-with(@content-desc, "全部委托(")]')
        # logging.info("是否存在: %s", b)
        b = self.dc.check_element_exists('//*[@content-desc="限价委托"]')
        logging.info("是否存在: %s", b)

    def test_element(self):
        element = self.dc.d.xpath('//*[@content-desc="交易"]').get()
        logging.info("交易按钮: %s", element.info)

    def test_input(self):
        """
        给指定xpath的输入框输入文本
        """
        self.dc.text_xiadan_shuliang(30)

    def test_kaichang_queren(self):
        """
        开仓确认
        """

        x, y, _ = self.dc.get_point({
            "name": "btn_kaicang_queren",
            "desc": "开仓确认",
            "xpath": '//*[@content-desc="开仓确认"]'
        })
        logging.info(f"开仓确认: ({x},{y})")

        if self.dc.check_element_exists('//*[@content-desc="开仓确认"]'):
            self.dc.click_xpath('//*[@content-desc="确认"]')

    def test_get_appinfo(self):
        logging.info(f'>>>{self.dc.d.info}', )

    def test_get_label_text(self):
        """
        测试获取 委托(1) 中的数字
        :return:
        """
        wt = self.dc.d.xpath('//*[starts-with(@content-desc, "委托(")]')
        logging.info(f'>>>{wt}', )
        # 获取第一个匹配的元素 (如果存在)
        element = wt.get()

        if element:
            # 获取元素的完整文本，通常 content-desc 就是元素的文本描述
            info = element.info
            print(f"元素的信息: {info}")
            print(f"元素的完整文本: {info['contentDescription']}")
        else:
            print("没有找到匹配的元素")


if __name__ == '__main__':
    unittest.main()
