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

# These need to be localized before using
camera_modes = [
   "TIME_LAPSE",
   "SLO_MO",
   "VIDEO",
   "PHOTO",
   "PORTRAIT",
   "PANO",
]

# Localizations:
camera_mode_strings = []

def change_camera_mode(to_mode = "PHOTO", previous_index = -1):
    if not camera_mode_strings:
        for k in camera_modes:
            camera_mode_strings.append( [s.upper() for s in get_ios_strings("PhotosUI", k)] )

    camera_mode_button = getElement("CameraMode")
    current_mode = camera_mode_button.value

    to_index = camera_modes.index(to_mode)
    current_index = -1

    for i,strings in enumerate(camera_mode_strings):
        if current_mode.upper() in strings:
            current_index = i
            break
    else:
        raise Exception("Failed to identify camera mode: " + current_mode)

    if current_index == previous_index:
        raise Exception("Swiping failed to change camera mode to: " + to_mode)

    if to_index == current_index:
        return

    # print( "%s(%d) -> %s(%d) ", camera_modes[current_index], current_index, camera_modes[to_index], to_index )

    if to_index < current_index:
        camera_mode_button.swipeRight()
    else:
        camera_mode_button.swipeLeft()

    return change_camera_mode(to_mode, previous_index = current_index)

def take_photos(n=10, delay=3):
    launchApplication("com.apple.camera")
    wait(3)
    change_camera_mode("PHOTO")

    setDelay(delay)

    mode = getElement("CameraMode")

    # This button isn't going anywhere, faster to cache and tap by position
    photo_capture_button = getElement("PhotoCapture")

    if n > 100:
        n = 100
    while n:
        n -= 1
        photo_capture_button.center.tap()

def add_options( parser ):
    parser.add_argument( '-n', '--n',     type=int,    default=1, help="Number of photos to take" )
    parser.add_argument( '-d', '--delay', type=float,  default=3, help="Delay in seconds between photos" )

def run( opts ):
    take_photos( opts.n, opts.delay )
    home()

cfrun(__name__, run, add_options )


"""============================================================================================="""

#doesn't work, keeping until we confirm different ios versions
# Good attempt, not used because "TimelapseCapture" also used in "Panorama" mode.
camera_button_identifiers_by_mode = [
   "TimelapseCapture", # label = "Record time lapse video"
   "SlomoCapture",
   "VideoCapture",
   "PhotoCapture",
   "PortraitCapture",
   "TimelapseCapture", # label = "Take Picture"
]




def change_camera_mode_by_identifier(to_mode = "PHOTO", previous_index = -1):
    camera_capture_button = getElement(ids = camera_modes)

    current_mode = camera_capture_button.identifier
    current_index = camera_modes.index(current_mode)
    to_index = camera_modes.index(to_mode)

    if current_index == previous_index:
        raise Exception("Failed to change camera mode to: " + to_mode)

    if to_index == current_index:
        return

    print( "%s(%d) -> %s(%d) ", camera_modes[current_index], current_index, camera_modes[to_index], to_index )

    if to_index < current_index:
        camera_capture_button.swipeRight()
    else:
        camera_capture_button.swipeLeft()

    return change_camera_mode(to_mode, previous_index = current_index)
