# from utils.point_utils import UiFactory
import logging

from plat.base.base_control import AbsControl


class DeepCoinControl(AbsControl):

    def __init__(self, serial: str):
        super().__init__(serial)
        self.point_path = '../../assets/point/deepcoin_point.json'
        self.add_func(self.btn_jiao_yi)
        self.add_func(self.btn_mairu_kaiduo)
        self.add_func(self.btn_shijia_quanping)

    def btn_jiao_yi(self):
        return self.get_point({
            "name": "btn_jiao_yi",
            "desc": "交易按钮",
            "xpath": '//*[@content-desc="交易"]'
        })

    def btn_mairu_kaiduo(self):
        return self.get_point({
            "name": "btn_mairu_kaiduo",
            "desc": "买入/开多",
            "xpath": '//*[@content-desc="买入/开多"]'
        })

    def btn_mairu_pingkong(self):
        return self.get_point({
            "name": "btn_mairu_pingkong",
            "desc": "买入/平空",
            "xpath": '//*[@content-desc="买入/平空"]'
        })

    def btn_shijia_quanping(self):
        self.to_next_swipe()
        return self.get_point({
            "name": "btn_shijia_quanping",
            "desc": "买入/开多",
            "xpath": '//*[@content-desc="市价全平"]'
        })

    def btn_kaichang(self):
        return self.get_point({
            "name": "btn_kaichang",
            "desc": "开仓",
            "xpath": '//*[@content-desc="开仓"]'
        })

    def btn_gengdan(self):
        """
        跟单(n) 按钮 坐标
        """
        return self.get_point({
            "name": "btn_gengdan",
            "desc": "跟单",
            "xpath": '//*[starts-with(@content-desc, "跟单(")]'
        })

    def btn_weituo(self):
        """
        委托(n) 按钮 坐标
        FIXME 委托按钮点击2次回出现切换委托界面

        """
        return self.get_point({
            "name": "btn_weituo",
            "desc": "委托",
            "xpath": '//*[starts-with(@content-desc, "委托(")]'
        })

    def btn_yijian_chedan(self):
        """
        一键撤单按钮
        """
        return self.get_point({
            "name": "btn_yijian_chedan",
            "desc": "一键撤单",
            "xpath": '//*[@content-desc="一键撤单"]'
        })

    def slider_baozhengjin_kaicang(self, offset=0, sleep=0.1):
        """
      开仓情况  保证金滑块
           :param sleep 休眠
           :param offset 价格偏移量 默认取50% x=368 每偏移69 向下多一个 偏移-69向上多一个 取值范围 [-2,2]
        坐标： 0% 230  25% 299  50% 368  75% 437  100% 506
        :return: 买入的按钮实例
        """
        # self.to_top_swipe()
        # self.d.sleep(0.1)

        # 基于买入按钮相对计算坐标
        x, y, btn = self.btn_mairu_kaiduo()

        x1 = x + offset * 69
        y1 = y - 140
        logging.info(f"开多坐标: ({x},{y})")
        logging.info(f"保证金50%坐标: ({x1},{y1})")

        self.click_point(x1, y1)
        self.d.sleep(sleep)

        return btn

    def slider_baozhengjin_pingcang(self, offset=0, sleep=0.1):
        """
            平仓情况  保证金滑块
           :param sleep 休眠
           :param offset 价格偏移量 默认取50% x=368 每偏移69 向下多一个 偏移-69向上多一个 取值范围 [-2,2]
        坐标： 0% 230  25% 299  50% 368  75% 437  100% 506
        :return: 买入的按钮实例
        """

        # 基于买入按钮相对计算坐标
        x, y, btn = self.btn_mairu_pingkong()
        x1 = x + offset * 69
        y1 = y - 70
        logging.info(f"开多坐标: ({x},{y})")
        logging.info(f"保证金50%坐标: ({x1},{y1})")

        self.click_point(x1, y1)
        self.d.sleep(sleep)

        return btn

    def click_kaicang(self):
        """
        开仓
        """
        self.click_xpath('//*[@content-desc="开仓"]')

    def click_pingchang(self, sleep=0.1):
        """
        开仓
        :param sleep: 等待时间
        """
        self.click_xpath('//*[@content-desc="平仓"]')
        self.d.sleep(sleep)

    def select_kaicang_weituo_xianjia(self):
        """
        开仓选择限价委托
        """
        x, y, _ = self.btn_kaichang()
        logging.info(f"开仓按钮坐标: ({x},{y})")
        x1 = x + 50
        y1 = y + 55
        logging.info(f"选择委托框: ({x1},{y1})")
        self.click_point(x1, y1)
        x2 = x1
        y2 = y1 + 100
        self.d.sleep(0.2)
        logging.info(f"选择限价委托: ({x2},{y2})")
        self.click_point(x2, y2)

    def click_price_red(self, offset=0):
        """
        点击价格 开多 平空， 点击红色价格

        :param offset 价格偏移量 默认取第5个， 每偏移20 向下多一个 偏移-20 向上多一个 取值范围 [-4,4]
        """

        x1 = 50
        y1 = 330 + offset * 20
        logging.info(f"红色部分选择价格: ({x1},{y1})")
        self.click_point(x1, y1)

    def click_price_green(self, offset=0):
        """
        点击价格 开空 平多， 点击红色价格
        :param offset 价格偏移量 默认取第5个， 每偏移20 向下多一个，偏移-20 向上多一个
        """

        # self.select_kaicang_weituo_xianjia()
        #
        # self.d.sleep(0.1)

        x1 = 50
        y1 = 515 + offset
        logging.info(f"绿色部分选择价格: ({x1},{y1})")
        self.click_point(x1, y1)

    def click_mairu_kaiduo(self):
        """
        买入开多
        """
        self.click_xpath('//*[@content-desc="买入/开多"]')
        self.d.sleep(0.1)
        # 弹出了确认框
        if self.check_element_exists('//*[@content-desc="开仓确认"]'):
            logging.info("弹出了确认框")
            self.click_xpath('//*[@content-desc="确认"]')

    def click_mairu_pingkong(self):
        """
        买入平空
        """
        self.click_xpath('//*[@content-desc="买入/平空"]')

    def click_maichu_kaikong(self):
        """
        卖出开空
        """
        self.click_xpath('//*[@content-desc="卖出/开空"]')

        self.d.sleep(0.1)
        # 弹出了确认框
        if self.check_element_exists('//*[@content-desc="开仓确认"]'):
            logging.info("弹出了确认框")
            self.click_xpath('//*[@content-desc="确认"]')

    def click_maichu_pingduo(self):
        """
        卖出平多
        """
        self.click_xpath('//*[@content-desc="卖出/平多"]')

    def click_yijian_chedan(self):
        """
        一键撤单
        如果有滑动 ，回导致第一个坐标不准，点不上。 所以要提前滑动，确保第一个坐标点得上
        """
        self.click_xpath('//*[starts-with(@content-desc, "跟单(")]')
        self.d.sleep(0.1)
        self.click_xpath('//*[starts-with(@content-desc, "委托(")]')
        self.d.sleep(0.1)

        b = self.check_element_exists('//*[starts-with(@content-desc, "全部委托(")]')
        logging.info("是否弹出了选择框: %s", b)

        if b:
            self.click_xpath('//*[starts-with(@content-desc, "全部委托(")]')
            self.d.sleep(0.2)

        self.click_xpath('//*[@content-desc="一键撤单"]')
        self.d.sleep(0.1)

        x, y, _ = self.get_point({
            "name": "btn_yijian_chedan",
            "desc": "撤销掉全部委托",
            "xpath": '//*[@content-desc="撤销掉全部委托"]'
        })
        logging.info(f"撤销掉全部委托: ({x},{y})")
        #  相对坐标 撤销掉全部委托 点击确定。
        x1 = x + 80
        y1 = y + 80
        logging.info(f"撤销掉全 确定按钮: ({x1},{y1})")
        self.click_point(x1, y1)

    def text_xiadan_shuliang(self, text):
        """
        设置 下单数量
        """
        x, y, _ = self.get_point({
            "name": "btn_baozhengjin",
            "desc": "保证金",
            "xpath": '//*[@content-desc="保证金"]'
        })
        logging.info(f"保证金坐标: ({x},{y})"'')
        x1 = x - 130
        y1 = y + 50
        self.input_by_position(x1, y1, str(text), True)

    def event_f1(self):
        self.click_kaicang()
        self.slider_baozhengjin_kaicang()
        pass

    def event_f2(self):
        self.click_kaicang()
        self.select_kaicang_weituo_xianjia()
        self.d.sleep(0.1)

        self.click_price_red()

        self.text_xiadan_shuliang(3)

        self.click_mairu_kaiduo()
        pass

    def event_f3(self):
        self.to_top_swipe(sleep=0.2)
        self.click_pingchang()
        self.slider_baozhengjin_pingcang(offset=1)
        pass

    def event_f4(self):
        self.to_top_swipe(sleep=0.2)
        self.click_pingchang()
        self.select_kaicang_weituo_xianjia()
        self.click_price_green()
        self.text_xiadan_shuliang(3)
        self.click_maichu_pingduo()
        pass

    def event_f5(self):
        self.to_top_swipe(sleep=0.2)
        self.click_kaicang()
        self.slider_baozhengjin_kaicang()
        pass

    def event_f6(self):
        self.to_top_swipe(sleep=0.1)
        self.click_kaicang()
        self.select_kaicang_weituo_xianjia()
        self.click_price_red()
        self.click_maichu_kaikong()
        pass

    def event_f7(self):
        self.to_top_swipe(sleep=0.1)
        self.click_pingchang()
        self.slider_baozhengjin_pingcang(offset=2)
        pass

    def event_f8(self):
        self.to_top_swipe(sleep=0.2)

        self.click_pingchang()

        self.click_price_red()

        self.text_xiadan_shuliang(3)

        self.click_mairu_pingkong()
        pass
