# Path: sexting/lib/transformer.py
# class Transformer(object):
# 
#     def can_handle_character(self, character):
#         False
# 
#     def can_handle_contact(self, contact, clock):
#         False
# 
#     def num_required_contacts(self):
#         0
# 
#     def transform(self, character, contacts, clock):
#         return 'Character: {0}'.format(character)
# 
# Path: sexting/lib/instruction.py
# class Instruction(object):
# 
#     def __init__(self, medium, character, clock, contact, data = {}):
#         self._medium = medium
#         self._character = character
#         self._clock = clock
#         self._contact = contact
#         self._data = data
# 
#     def medium(self):
#         return self._medium
# 
#     def character(self):
#         return self._character
# 
#     def clock(self):
#         return self._clock
# 
#     def contact(self):
#         return self._contact
# 
#     def data(self, key):
#         return self._data[key]
# 
#     def __str__(self):
#         data = ', '.join(map(lambda k: "{0}: {1}".format(k, self._data[k]), self._data))
#         return "For: '{0}', at {1}, {2}, should {3} ({4})".format(self._character, self._clock.block_range_str(), self._contact.get('name'), self._medium, data)


from ..lib.transformer import Transformer
from ..lib.instruction import Instruction
from LatLon import LatLon
import csv, os
# Tube station data from http://commons.wikimedia.org/wiki/London_Underground_geographic_maps/CSV
class Tube(Transformer):

    __punctuation_characters = ['.', ',', '?', '!', ';', ':', '-']

    def __init__(self):
        self.__import_stations()

    def can_handle_character(self, character):
        return character in Tube.__punctuation_characters

    def can_handle_contact(self, contact, clock):
        return contact.has('tubestation')

    def num_required_contacts(self):
        return 1

    def transform(self, character, contacts, clock):
        contact = contacts[0]
        station = contact.get('tubestation')
        if contact.has_state('lasttube'):
            station = contact.get_state('lasttube')

        stations = self.__nearest_stations(station, len(Tube.__punctuation_characters))
        index = Tube.__punctuation_characters.index(character)
        destination = stations[index]

        contact.set_state('lasttube', destination)
        contact.set_busy_func('tube', lambda clk: clock.jump_forward(12) > clk)


# Path: sexting/lib/transformer.py
# class Transformer(object):
# 
#     def can_handle_character(self, character):
#         False
# 
#     def can_handle_contact(self, contact, clock):
#         False
# 
#     def num_required_contacts(self):
#         0
# 
#     def transform(self, character, contacts, clock):
#         return 'Character: {0}'.format(character)
# 
# Path: sexting/lib/instruction.py
# class Instruction(object):
# 
#     def __init__(self, medium, character, clock, contact, data = {}):
#         self._medium = medium
#         self._character = character
#         self._clock = clock
#         self._contact = contact
#         self._data = data
# 
#     def medium(self):
#         return self._medium
# 
#     def character(self):
#         return self._character
# 
#     def clock(self):
#         return self._clock
# 
#     def contact(self):
#         return self._contact
# 
#     def data(self, key):
#         return self._data[key]
# 
#     def __str__(self):
#         data = ', '.join(map(lambda k: "{0}: {1}".format(k, self._data[k]), self._data))
#         return "For: '{0}', at {1}, {2}, should {3} ({4})".format(self._character, self._clock.block_range_str(), self._contact.get('name'), self._medium, data)


# Path: sexting/transformers/tube.py
from ..lib.transformer import Transformer
from ..lib.instruction import Instruction
from LatLon import LatLon
import csv, os

# Tube station data from http://commons.wikimedia.org/wiki/London_Underground_geographic_maps/CSV
class Tube(Transformer):

    __punctuation_characters = ['.', ',', '?', '!', ';', ':', '-']

    def __init__(self):
        self.__import_stations()

    def can_handle_character(self, character):
        return character in Tube.__punctuation_characters

    def can_handle_contact(self, contact, clock):
        return contact.has('tubestation')

    def num_required_contacts(self):
        return 1

    def transform(self, character, contacts, clock):
        contact = contacts[0]
        station = contact.get('tubestation')
        if contact.has_state('lasttube'):
            station = contact.get_state('lasttube')

        stations = self.__nearest_stations(station, len(Tube.__punctuation_characters))
        index = Tube.__punctuation_characters.index(character)
        destination = stations[index]

        contact.set_state('lasttube', destination)
        contact.set_busy_func('tube', lambda clk: clock.jump_forward(12) > clk)


        return Instruction('tube', character, clock, contact, {'from_station': station, 'to_station': destination})