class LongVideoCtl(ModelBaseCtl):
    FILTER_VIEW_TO_VIDEO = '筛选页进入'
    FILTER_VIEW_TO_VIDEO_NEXT = '筛选页进入-滑动进入下一集'
    MAIN_VIEW_TO_VIDEO = '频道页进入'
    MAIN_VIEW_TO_VIDEO_NEXT = '频道页进入-滑动进入下一集'
    GUESS_YOU_LIKE = '猜你喜欢进入'
    GUESS_YOU_LIKE_NEXT = '猜你喜欢进入-滑动进入下一集'
    CHOOSE_ITEM = '选集进入'
    SHORT_TO_LONG = '少儿频道(短带长)'
    SHORT_TO_LONG_NEXT = '少儿频道(短带长)-滑动进入下一集'

    @staticmethod
    def filter_view_to_video(case: XsgTestBase):
        main = MainCtl.get_panel(case.app)
        # index_view = LongVideoIndexCtl.get_panel(case.app)
        detail_view = LongVideoDetailCtl.get_panel(case.app)

        if not main.control_wait_for_visible(MainCtl.ctl.MINE, timeout=2, raise_error=False):
            # 解决进入沉浸式，无法定位到'看大片'频道
            case.remote_control(case.KEYCODE_BACK)
            case.remote_control(case.KEYCODE_RIGHT)
            case.remote_control(case.KEYCODE_RIGHT)
            cv_window = XiguaCVWindow(root=case.app)
            cv_window.click_pic_ocr("看大片")
        # while main[MainCtl.ctl.CURRENT_CHANNEL].text != '看大片':
        #     case.remote_control(case.KEYCODE_RIGHT)
        #     time.sleep(2)
        #     main[MainCtl.ctl.CURRENT_CHANNEL].refresh()
        main.control_click(MainCtl.ctl.SEE_BIG_VIDEO_TV_SERIES)
        cv_window.click_pic_ocr("免费")

        # 开启进入校验
        case.start_step(LongVideoCtl.FILTER_VIEW_TO_VIDEO)
        # 由于电视剧控件不支持点击事件
        # index_view.control_click(LongVideoIndexCtl.ctl.FIRST_ITEM)
        case.remote_control(case.KEYCODE_DOWN)
        case.remote_control(case.KEYCODE_DOWN)
        case.remote_control(case.KEYCODE_CENTER)

        detail_view.control_click(LongVideoDetailCtl.ctl.MEDIA_VIEW)
        # 开启进入下一次校验
        case.start_step(LongVideoCtl.FILTER_VIEW_TO_VIDEO_NEXT)
        case.remote_control(case.KEYCODE_DOWN)

    @staticmethod
    def choose_item(case: XsgTestBase):
        main = MainCtl.get_panel(case.app)
        index_view = LongVideoIndexCtl.get_panel(case.app)
        detail_view = LongVideoDetailCtl.get_panel(case.app)

        if not main.control_wait_for_visible(MainCtl.ctl.MINE, timeout=2, raise_error=False):
            # 解决进入沉浸式，无法定位到'看大片'频道
            case.remote_control(case.KEYCODE_BACK)
            case.remote_control(case.KEYCODE_RIGHT)
            case.remote_control(case.KEYCODE_RIGHT)
            cv_window = XiguaCVWindow(root=case.app)
            cv_window.click_pic_ocr("看大片")
        main.control_click(MainCtl.ctl.SEE_BIG_VIDEO_TV_SERIES)
        cv_window.click_pic_ocr("免费")

        # 由于电视剧控件不支持点击事件
        # index_view.control_click(LongVideoIndexCtl.ctl.FIRST_ITEM)
        case.remote_control(case.KEYCODE_DOWN)
        case.remote_control(case.KEYCODE_DOWN)
        case.remote_control(case.KEYCODE_CENTER)
        case.remote_control(case.KEYCODE_DOWN)
        case.remote_control(case.KEYCODE_RIGHT)
        case.remote_control(case.KEYCODE_CENTER)

        # detail_view.control_click(LongVideoDetailCtl.ctl.MEDIA_VIEW)
        # 逻辑过期
        # if not detail_view.control_wait_for_visible(LongVideoDetailCtl.ctl.FIRST_ITEM, raise_error=False, timeout=3):
        #     case.remote_control(case.KEYCODE_DOWN)
        # detail_view.control_click(LongVideoDetailCtl.ctl.FIRST_ITEM)
        # case.remote_control(case.KEYCODE_DOWN)
        # if not detail_view.control_wait_for_visible(LongVideoDetailCtl.ctl.SECOND_ITEM, raise_error=False, timeout=3):
        #     case.remote_control(case.KEYCODE_DOWN)
        case.start_step(LongVideoCtl.CHOOSE_ITEM)
        detail_view.control_click(LongVideoDetailCtl.ctl.SECOND_ITEM)

    @staticmethod
    def guess_you_like(case: XsgTestBase):
        main = MainCtl.get_panel(case.app)
        index_view = LongVideoIndexCtl.get_panel(case.app)
        detail_view = LongVideoDetailCtl.get_panel(case.app)

        if not main.control_wait_for_visible(MainCtl.ctl.MINE, timeout=2, raise_error=False):
            # 解决进入沉浸式，无法定位到'看大片'频道
            case.remote_control(case.KEYCODE_BACK)
            case.remote_control(case.KEYCODE_RIGHT)
            case.remote_control(case.KEYCODE_RIGHT)
            cv_window = XiguaCVWindow(root=case.app)
            cv_window.click_pic_ocr("看大片")
        main.control_click(MainCtl.ctl.SEE_BIG_VIDEO_TV_SERIES)
        cv_window.click_pic_ocr("免费")
        case.remote_control(case.KEYCODE_DOWN)
        case.remote_control(case.KEYCODE_DOWN)
        case.remote_control(case.KEYCODE_CENTER)


        detail_view.scroll_if_is_existing(coefficient_y=0.7)
        case.start_step(LongVideoCtl.GUESS_YOU_LIKE)
        #
        detail_view.control_click(LongVideoDetailCtl.ctl.FIRST_LIKE)
        case.remote_control(case.KEYCODE_CENTER)
        detail_view.control_click(LongVideoDetailCtl.ctl.MEDIA_VIEW)
        case.start_step(LongVideoCtl.GUESS_YOU_LIKE_NEXT)
        case.remote_control(case.KEYCODE_DOWN)

    @staticmethod
    def main_view_to_video(case: XsgTestBase):
        main = MainCtl.get_panel(case.app)
        detail_view = LongVideoDetailCtl.get_panel(case.app)

        if not main.control_wait_for_visible(MainCtl.ctl.MINE, timeout=2, raise_error=False):
            # 解决进入沉浸式，无法定位到'看大片'频道
            case.remote_control(case.KEYCODE_BACK)
            case.remote_control(case.KEYCODE_RIGHT)
            case.remote_control(case.KEYCODE_RIGHT)
            cv_window = XiguaCVWindow(root=case.app)
            cv_window.click_pic_ocr("看大片")
        # while main[MainCtl.ctl.CURRENT_CHANNEL].text != '看大片':
        #     case.remote_control(case.KEYCODE_RIGHT)
        #     time.sleep(2)
        #     main[MainCtl.ctl.CURRENT_CHANNEL].refresh()
        case.remote_control(case.KEYCODE_DOWN)
        case.remote_control(case.KEYCODE_CENTER)

        # for _ in range(2):
        #     time.sleep(2)
        #     main.scroll_if_is_existing(coefficient_y=0.7)
        # 解决进入了付费视频，引起的case失败
        # children = main[MainCtl.ctl.SEE_BIG_VIDEO_MAIN_RECYCLER_VIEW].children
        # for i in range(len(children) - 1, -1, -1):
        #     sub_children = children[i].children
        #     if not isinstance(sub_children, list):
        #         continue
        #     info = sub_children[0].elem_info
        #     if not (isinstance(info, dict) and info.__contains__('view_path') and isinstance(info['view_path'], str) and
        #             info['view_path'].endswith('_element')):
        #         continue
        #     flag = False
        #     for sub in sub_children:
        #         card_info = sub.children
        #         if card_info[len(card_info) - 2].text == '付费':
        #             continue
        #         case.start_step(LongVideoCtl.MAIN_VIEW_TO_VIDEO)
        #         sub.click()
        #         flag = True
        #         break
        #     if flag:
        #         break


        detail_view.control_click(LongVideoDetailCtl.ctl.MEDIA_VIEW)
        case.start_step(LongVideoCtl.MAIN_VIEW_TO_VIDEO_NEXT)
        case.remote_control(case.KEYCODE_DOWN)
        # detail_view.scroll_if_is_existing(coefficient_x=-0.8)




    @staticmethod
    def children_channel_short_to_long(case: XsgTestBase):
        main = MainCtl.get_panel(case.app)
        detail_view = LongVideoDetailCtl.get_panel(case.app)

        if not main.control_wait_for_visible(MainCtl.ctl.MINE, timeout=2, raise_error=False):
            case.remote_control(case.KEYCODE_BACK)

        # flag = True
        # while flag:
        #     channels = main[MainCtl.ctl.CHANNEL_LIST].children
        #     for i in range(len(channels)):
        #         if channels[i].text == '少儿':
        #             flag = False
        #             channels[i].click()
        #             break
        #         else:
        #             if not main.control_wait_for_visible(MainCtl.ctl.MINE, timeout=2, raise_error=False):
        #                 case.remote_control(case.KEYCODE_BACK)
        #                 case.remote_control(case.KEYCODE_RIGHT)
        #             else:
        #                 case.remote_control(case.KEYCODE_RIGHT)
        while main[MainCtl.ctl.CURRENT_CHANNEL].text != '少儿':
            if main[MainCtl.ctl.CURRENT_CHANNEL].text == '推荐':
                case.remote_control(case.KEYCODE_RIGHT)
            else:
                case.remote_control(case.KEYCODE_RIGHT)
                time.sleep(2)
                main[MainCtl.ctl.CURRENT_CHANNEL].refresh()
        time.sleep(3)
        case.remote_control(case.KEYCODE_DOWN)
        retry_time = 100
        while not main.control_wait_for_existing(MainCtl.ctl.CHILDREN_WATCH_POSITIVE, timeout=1, raise_error=False):
            case.remote_control(case.KEYCODE_DOWN)
            case.remote_control(case.KEYCODE_CENTER)
            main.refresh()
            retry_time -= 1
            if retry_time == 0:
                raise Exception('超过100次未找到目标视频')
        case.start_step(LongVideoCtl.SHORT_TO_LONG)
        main.control_click(MainCtl.ctl.CHILDREN_WATCH_POSITIVE)
        detail_view.control_click(LongVideoDetailCtl.ctl.MEDIA_VIEW)
        case.start_step(LongVideoCtl.SHORT_TO_LONG_NEXT)
        case.remote_control(case.KEYCODE_DOWN)


LongVideoCtl.MODELS = {
    LongVideoCtl.FILTER_VIEW_TO_VIDEO: StepModel(step=LongVideoCtl.FILTER_VIEW_TO_VIDEO,
                                                 method=LongVideoCtl.filter_view_to_video,
                                                 common_param={"enter_from": "click_category",
                                                               "category_name": "subv_xg_lvideo_tv",
                                                               "impr_type": "__lv_filter__",
                                                               "recommend_type": "operate"
                                                               },
                                                 event_special_params={"go_detail": {},
                                                                       "video_play": {}}
                                                 ),
    LongVideoCtl.FILTER_VIEW_TO_VIDEO_NEXT: StepModel(step=LongVideoCtl.FILTER_VIEW_TO_VIDEO_NEXT,
                                                      method=LongVideoCtl.filter_view_to_video,
                                                      common_param={"enter_from": "click_category",
                                                                    "category_name": "subv_xg_lvideo_tv",
                                                                    "impr_type": "__lv_filter__",
                                                                    "recommend_type": "operate"
                                                                    },
                                                      event_special_params={"stay_page": {"position": "detail"},
                                                                            "video_over": {"position": "detail"},
                                                                            }
                                                      ),
    LongVideoCtl.CHOOSE_ITEM: StepModel(step=LongVideoCtl.CHOOSE_ITEM,
                                        method=LongVideoCtl.choose_item,
                                        common_param={"enter_from": "click_related",
                                                      "category_name": "related",
                                                      "impr_type": "__lv_detail__",
                                                      "recommend_type": "select"
                                                      },
                                        event_special_params={"stay_page": {},
                                                              "video_over": {},
                                                              "video_play": {},
                                                              "go_detail": {},
                                                              }
                                        ),
    LongVideoCtl.GUESS_YOU_LIKE: StepModel(step=LongVideoCtl.GUESS_YOU_LIKE,
                                           method=LongVideoCtl.guess_you_like,
                                           common_param={"enter_from": "click_category",
                                                         "category_name": "recommend_long",
                                                         "impr_type": "__lv_detail__",
                                                         "recommend_type": "recommend_long"
                                                         },
                                           event_special_params={"go_detail": {},
                                                                 "video_play": {}}
                                           ),
    LongVideoCtl.GUESS_YOU_LIKE_NEXT: StepModel(step=LongVideoCtl.GUESS_YOU_LIKE_NEXT,
                                                method=LongVideoCtl.guess_you_like,
                                                common_param={"enter_from": "click_category",
                                                              "category_name": "recommend_long",
                                                              "impr_type": "__lv_detail__",
                                                              "recommend_type": "recommend_long"
                                                              },
                                                event_special_params={"stay_page": {"position": "detail"},
                                                                      "video_over": {"position": "detail"},
                                                                      }
                                                ),
    LongVideoCtl.MAIN_VIEW_TO_VIDEO: StepModel(step=LongVideoCtl.MAIN_VIEW_TO_VIDEO,
                                               method=LongVideoCtl.main_view_to_video,
                                               common_param={"enter_from": "click_category",
                                                             "category_name": "subv_xg_lvideo_tv",
                                                             "impr_type": "__lv_channel__",
                                                             "recommend_type": "operate"
                                                             },
                                               event_special_params={"go_detail": {},
                                                                     "video_play": {}}
                                               ),
    LongVideoCtl.MAIN_VIEW_TO_VIDEO_NEXT: StepModel(step=LongVideoCtl.MAIN_VIEW_TO_VIDEO_NEXT,
                                                    method=LongVideoCtl.main_view_to_video,
                                                    common_param={"enter_from": "click_category",
                                                                  "category_name": "subv_xg_lvideo_tv",
                                                                  "impr_type": "__lv_channel__",
                                                                  "recommend_type": "operate"
                                                                  },
                                                    event_special_params={"stay_page": {"position": "detail"},
                                                                          "video_over": {"position": "detail"},
                                                                          }
                                                    ),
    LongVideoCtl.SHORT_TO_LONG: StepModel(step=LongVideoCtl.SHORT_TO_LONG,
                                          method=LongVideoCtl.children_channel_short_to_long,
                                          common_param={"enter_from": "click_category",
                                                        "category_name": "xg_tv_children",
                                                        "impr_type": "__homo_lvideo__",
                                                        },
                                          event_special_params={"go_detail": {},
                                                                "video_play": {}}
                                          ),
    LongVideoCtl.SHORT_TO_LONG_NEXT: StepModel(step=LongVideoCtl.SHORT_TO_LONG_NEXT,
                                               method=LongVideoCtl.children_channel_short_to_long,
                                               common_param={"enter_from": "click_category",
                                                             "category_name": "xg_tv_children",
                                                             "impr_type": "__homo_lvideo__",
                                                             },
                                               event_special_params={"stay_page": {"position": "detail"},
                                                                     "video_over": {"position": "detail"},
                                                                     }
                                               ),
}


class LongVideoContext(OracleContext):
    def predicate(self):
        from xsglib.utils.EventUtils import EventUtils
        param_check_dict = EventUtils.generate_param_check(LongVideoCtl)
        for k, v in param_check_dict.items():
            events = []
            for ik, iv in v.items():
                events.append(XiGuaV3EventCheckerDefaultLocalSchema(event=ik, **iv))
            self.set_oracle(k, EventOracle(events=events))


class LongVideoFilterViewToVideo(XsgTestBase):
    """
    筛选页
    """
    owner = "xiongyulin"
    timeout = 900
    tags = "lvideo"
    oracle_context_classes = [LongVideoContext]  # 框架提供属性：传入埋点校验规则

    def run_test(self):
        LongVideoCtl.MODELS[LongVideoCtl.FILTER_VIEW_TO_VIDEO].method(self)


class LongVideoMainViewToVideo(XsgTestBase):
    """
    主页
    """
    owner = "xiongyulin"
    timeout = 900
    tags = "lvideo"
    oracle_context_classes = [LongVideoContext]  # 框架提供属性：传入埋点校验规则

    def run_test(self):
        LongVideoCtl.MODELS[LongVideoCtl.MAIN_VIEW_TO_VIDEO].method(self)


class LongVideoGuessYouLikeViewToVideo(XsgTestBase):
    """
    猜你喜欢
    """
    owner = "xiongyulin"
    timeout = 900
    tags = "lvideo"
    oracle_context_classes = [LongVideoContext]  # 框架提供属性：传入埋点校验规则

    def run_test(self):
        LongVideoCtl.MODELS[LongVideoCtl.GUESS_YOU_LIKE].method(self)


class LongVideoChooseItemViewToVideo(XsgTestBase):
    """
    切换集数
    """
    owner = "xiongyulin"
    timeout = 900
    tags = "lvideo"
    oracle_context_classes = [LongVideoContext]  # 框架提供属性：传入埋点校验规则

    def run_test(self):
        LongVideoCtl.MODELS[LongVideoCtl.CHOOSE_ITEM].method(self)


class LongVideoShortToLongVideo(XsgTestBase):
    """
    短切换长
    """
    owner = "xiongyulin"
    timeout = 900
    tags = "lvideo"
    oracle_context_classes = [LongVideoContext]  # 框架提供属性：传入埋点校验规则

    def run_test(self):
        # LongVideoCtl.MODELS[LongVideoCtl.SHORT_TO_LONG].method(self)
        # LongVideoCtl.MODELS[LongVideoCtl.MAIN_VIEW_TO_VIDEO].method(self)
        # LongVideoCtl.MODELS[LongVideoCtl.FILTER_VIEW_TO_VIDEO].method(self)
        LongVideoCtl.MODELS[LongVideoCtl.GUESS_YOU_LIKE].method(self)

if __name__ == '__main__':
    LongVideoShortToLongVideo().debug_run()
解释下上面代码