#!/usr/bin/env python3
from controlfloor import *

def clear_downloads():
    browse_strings       = get_ios_strings("DocumentManager", "BROWSE")
    select_strings       = get_ios_strings("DocumentManager", "SELECT")
    delete_strings       = get_ios_strings("DocumentManager", "DELETE")
    # The "Downloads" cell (a big folder icon) has a label and an identifier.  Don't know if the identifier is
    # localized or not, so I've added the English to be safe.
    downloads_strings    = get_ios_strings("DocumentManager", "DOWNLOADS") + ["Downloads"]
    on_my_device_strings = get_ios_strings("DocumentManager", "ON_MY_DEVICE")
    no_downloaded_files_strings = get_ios_strings("DocumentManager", "NO_DOWNLOADED_FILES")

    select_all_strings  = get_ios_strings("UIKit", "SELECT_ALL")

    # "On My iPhone is Empty"
    # You get this screen if you have never downloaded a file, and don't even have a "Downloads" folder yet
    empty_device_matcher = get_ios_string_matcher("DocumentManager", "X_IS_EMPTY")

    launchApplication("com.apple.DocumentsApp")
    wait(2)

    elements = getNamedElements("QLOverlayDoneButtonAccessibilityIdentifier")
    # label = "Done"
    # When a file is opened, it stays open even if you kill/restart the file browser.
    # Tapping "Done" will close the file
    if done_button := elements.get("QLOverlayDoneButtonAccessibilityIdentifier", "button", exceptions=False):
        done_button.tap()
        wait(2)

    tap(browse_strings, "button")
    wait(1)
    tap(browse_strings, "button")
    tap(on_my_device_strings, "staticText")

    if not tap(downloads_strings, "cell", allow_errors=1):
        wait(1)
        els = getNamedElements(None, "staticText")
        for el in els:
            if empty_device_matcher.matches(el.id):
                return
        else:
            # If we're confident, we could just return as long as there is no 'Downloads' folder at all...
            # raise Exception("Could not find 'Downloads' folder, and files are not empty")
            return
    wait(1)
    if getElements(no_downloaded_files_strings):
        return

    # Each file is nested inside a "cell".  Tapping "..."->"Select" does not
    # scroll the screen, and XCTest will scroll any of these cells onto the
    # screen as necessary, so we don't have to worry about partially offscreen
    # items.
    more_strings = get_ios_strings("UIKit", "MORE") + ["DOC.itemCollectionMenuButton.Ellipsis"]

    cells = getElements(None, "cell")
    while cells:
        # ... Menu in the top-right, label = "More"
        tap(more_strings, "button")
        #tap(select_strings,"button",useCoordinates=True) #Hangs without coords
        tap(select_strings,"button") #Hangs without coords
        wait(1)
        pushDelay(0.1)
        for cell in cells:
            p = cell.point(0.5,0.2)
            if p.y > 0.9 * deviceInfo.frame.height:
                continue
            p.tap()
        popDelay()

        # No "confirm" dialog? Surprising...
        tap(delete_strings,"button")

        wait(2)
        cells = getElements(None, "cell")

    # If everything is deleted, we should see this message:
    getElement(no_downloaded_files_strings)

cfrun(__name__, clear_downloads)
