import logging
import unittest

from plat.deepcoin_control import DeepCoinControl

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


class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.pc = DeepCoinControl('Deepcoin')

        self.pc.connect_adb('127.0.0.1:6555')

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

    def test_to_next_swipe(self):
        """
        滑动到下一屏
        """
        self.pc.to_next_swipe()

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

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

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

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

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

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

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

    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.click_mairu_kaiduo()

    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)

    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.click_maichu_pingduo()

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

    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()

    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)

    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_mairu_pingkong()

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

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

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

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

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

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

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

        self.pc.click_yijian_chedan()

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

        # 获取元素位置
        bounds = element.info['bounds']
        # 截图
        screenshot = self.pc.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.pc.slider_baozhengjin_pingcang()

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

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

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

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

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

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


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