class MigrationPlanRequestDetailsView(View):
    migration_request_details_list = MigrationPlanRequestDetailsList("plan-request-details-list")
    paginator_view = View.include(V2VPaginatorPane, use_parent=True)
    download_logs = Dropdown("Download Log")

    @property
    def is_displayed(self):
        return self.migration_request_details_list.is_displayed

    def plan_in_progress(self):
        """Return True or False, depending on migration plan status.
        If none of the VM migrations are in progress, return True.
        """
        self.items_on_page.item_select("15")
        migration_plan_in_progress_tracker = []
        vms = self.migration_request_details_list.read()
        for vm in vms:
            clock_reading1 = self.migration_request_details_list.get_clock(vm)
            time.sleep(1)  # wait 1 sec to see if clock is ticking
            clock_reading2 = self.migration_request_details_list.get_clock(vm)
            logger.info("clock_reading1: %s, clock_reading2:%s", clock_reading1, clock_reading2)
            migration_plan_in_progress_tracker.append(
                self.migration_request_details_list.is_in_progress(vm)
                and (clock_reading1 < clock_reading2)
            )
        return not any(migration_plan_in_progress_tracker)
class MigrationPlanRequestDetailsView(View):
    migration_request_details_list = MigrationPlanRequestDetailsList(
        "plan-request-details-list")
    sort_type = SelectorDropdown('id', 'sortTypeMenu')
    paginator_view = View.include(v2vPaginatorPane, use_parent=True)
    search_box = TextInput(
        locator=".//div[contains(@class,'input-group')]/input")
    clear_filters = Text(".//a[text()='Clear All Filters']")
    # Used for Ascending/Descending sort
    sort_order = Text(".//button[./span[contains(@class,'sort-direction')]]")
    # Used to select filter_by 'Name' or 'Status'
    filter_by_dropdown = SelectorDropdown('id', 'filterFieldTypeMenu')
    # USed to select specific status from dropdown to filter items by
    filter_by_status_dropdown = SelectorDropdown('id', 'filterCategoryMenu')
    # USed to select sort by options like 'VM Name', 'Started' or 'Status'
    sort_by_dropdown = SelectorDropdown('id', 'sortTypeMenu')

    @property
    def is_displayed(self):
        return self.migration_request_details_list.is_displayed

    def filter_by_vm_name(self, vm_name):
        """Enter VM Name in search box and hit ENTER to filter the list of VMs.

        Args:
            vm_name(str): Takes VM Name as arg.
        """
        try:
            self.filter_by_dropdown.item_select("VM Name")
        except NoSuchElementException:
            self.logger.info(
                "filter_by_dropdown not present, "
                "migration plan may not have started yet.Ignoring.")
        self.search_box.fill(vm_name)
        self.browser.send_keys(Keys.ENTER, self.search_box)

    def get_migration_status_by_vm_name(self, vm_name):
        """Search VM using filter_by_name and return its status.

        Args:
            vm_name(str): Takes VM Name as arg.
        """
        try:
            # Try to clear previously applied filters, if any.
            self.clear_filters.click()
        except NoSuchElementException:
            # Ignore as button won't be visible if there were no filters applied.
            self.logger.info("Clear Filters button not present, ignoring.")
        self.filter_by_vm_name(vm_name)
        status = {
            "Message":
            self.migration_request_details_list.get_message_text(vm_name),
            "Description":
            self.migration_request_details_list.get_progress_description(
                vm_name),
            "Time Elapsed":
            self.migration_request_details_list.get_clock(vm_name)
        }
        return status

    def filter_by_status(self, status):
        """Set filter_by_dropdown to 'Status' and uses status arg by user to set status filter.

        Args:
            status(str): Takes status string as arg. Valid status options are:
            ['Pending', 'Validating', 'Pre-migration', 'Migrating', 'VM Transformations Ccompleted',
             'VM Transformations Failed']
        """
        try:
            self.filter_by_dropdown.item_select("Status")
            self.filter_by_status_dropdown.item_select(status)
        except NoSuchElementException:
            raise ItemNotFound("Migration plan is in Not Started State,"
                               " hence filter status dropdown not visible")

    def sort_by(self, option):
        """Sort VM list by using one of the 'Started','VM Name' or 'Status' option.

        Args:
            status(str): Takes status string as arg.
        """
        try:
            self.sort_by_dropdown.item_select(option)
        except NoSuchElementException:
            raise ItemNotFound("Migration plan is in Not Started State,"
                               " hence sort_by dropdown not visible")

    def plan_in_progress(self, vms_count=5):
        """Reuturn True or False, depending on migration plan status.

        If none of the VM migrations are in progress, return True.
        """
        if vms_count > 5:
            self.items_on_page.item_select("15")
        migration_plan_in_progress_tracker = []
        vms = self.migration_request_details_list.read()
        for vm in vms:
            clock_reading1 = self.migration_request_details_list.get_clock(vm)
            time.sleep(1)  # wait 1 sec to see if clock is ticking
            self.logger.info(
                "For vm %s, current message is %s", vm,
                self.migration_request_details_list.get_message_text(vm))
            self.logger.info(
                "For vm %s, current progress description is %s", vm,
                self.migration_request_details_list.get_progress_description(
                    vm))
            clock_reading2 = self.migration_request_details_list.get_clock(vm)
            self.logger.info("clock_reading1: %s, clock_reading2:%s",
                             clock_reading1, clock_reading2)
            self.logger.info(
                "For vm %s, is currently in progress: %s", vm,
                self.migration_request_details_list.is_in_progress(vm))
            migration_plan_in_progress_tracker.append(
                self.migration_request_details_list.is_in_progress(vm)
                and (clock_reading1 < clock_reading2))
        return not any(migration_plan_in_progress_tracker)