import shell
import prints
import collections
import os
import re
import time

### BEGIN CONFIGURATION OPTIONS ###

# location of your itunes library
# (or whatever directory you want to recursively transfer)
ITUNES_LIBRARY = "/Users/chris/Music/iTunes/iTunes Media/Music/"
GALAXY_LIBRARY = "/sdcard/Music/" # use "adb -d shell ls /" to find this

# location of adb
# mine was originally at:
#       ~/Downloads/adt-bundle-mac-x86_64-20140321/sdk/platform-tools/adb
# I created a symlink for convenience, but this is not necessary.
ADB = "/usr/local/bin/adb"

# It often takes more than 1 try to connect to the device
# the device is `offline` at first but wakes up after the
# first attempt. 5 is a liberal number - 2 tries typically works.
CONNECTION_TRIES = 5

DBG_REPORT = True  # Whether to print a summary of the sync
DBG_ERROR  = True  # Whether to print errors
DBG_WARN   = True  # Whether to print warnings
DBG_VERB   = True  # Whether to print a summary of every transaction

### END CONFIGURATION OPTIONS ###

SHELL_ALIASES = {'adb': ADB,
                 'galaxy': '%s -d' % ADB, # Alias for sending commands to phone
                 'restart': '%s %s ; %s %s' %
                 (ADB, 'kill-server', ADB, 'start-server')}


DEVICE_STATES = collections.defaultdict(bool)
DEVICE_STATES.update({
    "offline": False,
    "device": True,
    "unauthorized": False,
    "no device": False,
})

# Parses the serial numbers and states returned by "adb devices"
DEVICE_REPORT_PATTERN = "\n([0-9\-a-zA-Z]+)\t(%s)" % (
    '|'.join(DEVICE_STATES.keys()))

# Shell interface - see shell.py. Global here but initialized in setup()
SH = None

def report(s):
    if DBG_REPORT:
        prints.stdout(s+'\n')

def warn(s):
    if DBG_WARN:
        prints.stdout(s+'\n')

def error(s):
    if DBG_ERROR:
        prints.stderr(s+'\n')

def log(s):
    if DBG_VERB:
        prints.stdout(s+'\n')

def i_to_g(s):
    """
    Convert iTunes path to Galaxy filesystem location.
    Technically this could fail if you had some weird