# from utils.point_utils import UiFactory
import logging

from plat.base.abs_ui import AbsUi


class PointCollection(AbsUi):

    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_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 slider_baozhengjin(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

        """
        self.to_top_swipe()
        self.d.sleep(0.1)
        x, y = 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)

    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_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]
        """
        self.select_weituo_xianjia()
        self.d.sleep(0.1)

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

        self.d.sleep(0.1)

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

    def click_mairu_kaiduo(self):
        """
        买入开多
        """
        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="卖出/开空"]')

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