#!/usr/bin/env python
import sys
import time
from collections import OrderedDict


def introduction():
    """
    returns dict of author information
    """
    author = {
        'name': 'David St.Germain',
        'address': 'P.O. Box 283',
        'city': 'Barre, MA 01005',
        'phone': '978-273-2772',
        'email': 'dave@st.germa.in',
    }
    return author


def work_experience():
    """
    returns dict of work experience
    """
    jobs = OrderedDict()
    jobs['Handyman'] = {
        'location': 'Worcester area',
        'years': (2015, 2016),
        'position': 'Carpenter/Property Manager',
        'tasks': [
            'Repaired and renovated roofs, floors, walls, bathrooms',
            'Communicated with other contractors to deal with property issues',
            'Regularly checked in on tenant/property to address problems'
        ]
    }
    jobs['Farm'] = {
        'location': 'Barre, Ma',
        'years': (2015, 2015),
        'position': 'Farm Laborer',
        'tasks': [
            'Delivered hay bales to clients',
            'Shoveled feed trough',
            'Spread manure',
            'Dug holes'
        ]
    }
    jobs['edX'] = {
        'location': 'Cambridge, MA',
        'years': (2014, 2014),
        'position': 'Software Consultant',
        'tasks': [
            'Improved site accessibility',
            'Fixed security bugs identified by security audit',
            'Supported conversion of video processing workflow to enable mobile apps'
        ]
    }
    jobs['Sensable Technologies Inc.'] = {
        'location': 'Wilmington, MA',
        'years': (2011, 2011),
        'position': 'Software Consultant',
        'tasks': [
            'Developed a Django site to handle dental technician workflow',
            'Integrated with legacy Windows GUI software and Citrix to provide a CAD experience "through the web"',
            'Managed servers running Django, nginx, MySQL, and MongoDB'
        ]
    }

    jobs['Hewlett Packard'] = {
        'location': 'Marlborough, MA',
        'years': (2007, 2011),
        'position': 'Software Designer Level V',
        'tasks': [
            'Worked on Django web services supporting a new web-connected consumer printer',
            'Worked on personal publishing sites and applications integrating with '
            'Myspace, Facebook, Flickr, and Disney',
            'Continued to support servers from Tabblo, transitioning them into HP\'s datacenter',
            'Developed in-house key-value server to handle changing data storage needs'
        ]
    }

    jobs['Tabblo.com'] = {
        'location': 'Cambridge, MA',
        'years': (2006, 2007),
        'position': 'Programmer',
        'tasks': [
            'Implemented fulltext search engine, social features, infrastructure, shopping cart, using Django',
            'Integrated with external credit card, print service, and shipping APIs',
            'Administered a dozen Linux servers',
            'Helped with book production in a dynamic, synergized, jazzercised group',
            'Promoted the site endlessly'
        ]
    }

    jobs['The Eagle-Tribune Publishing Co.'] = {
        'location': 'North Andover, MA',
        'years': (2003, 2005),
        'position': 'Programmer and Network Administrator',
        'tasks': [
            'Developed web applications in Java, Python, and PHP used for '
            'managing news, advertising, and production processes',
            # 'Created Mac OS X desktop applications to improve photographers\' workflow',
            'Developed custom credit card processing and authentication services for the web',
            'Created a PDF and JPEG conversion workflow that integrated proprietary and in-house systems',
            # 'Programmed web-based reports using data from multiple databases',
            'Administered several OS X, FreeBSD, and Linux servers for e-mail, DNS, FTP, and HTTP',
            # 'Setup a help desk system using free software and worked on developing help desk workflow',
            # 'Saved money by converting proprietary systems to standard open source solutions',
        ]
    }
    return jobs


def other_experience():
    """
    returns dict of other relevant experience
    """
    exp = OrderedDict()
    exp['House Construction'] = {
        'years': (2012, 2016),
        'detail': 'Designed and built a small solar-powered house on wheels and several other structures '
        'using skills in carpentry, plumbing, and electrical installation'
        }
    exp['Programming Class'] = {
        'years': (2011, 2011),
        'detail': 'Taught a project-based python programming class at Sprout in Somerville'
    }
    exp['Documentary Photography'] = {
        'years': (2007, 2012),
        'detail': 'Pursued documentary photography through photo essays, self-directed learning, classes, etc.'
    }
    return exp


def formal_education():
    """
    returns dict of education credentials
    """
    return {}


class Writer(object):
    """
    write strings to the output file object,
    with optional delay
    """
    boldstart = '\x1b[1m'
    boldend = '\x1b[22m'
    clearscreen = '\x1b[2J\x1b[H'

    def __init__(self, out=sys.stdout, delay=0.2):
        self.out = out
        self.delay = delay

    def __call__(self, s, delay=None):
        """
        write the string
        """
        delay = delay or self.delay
        for c in s:
            self.out.write(c)
            self.out.flush()
            if delay:
                time.sleep(delay)

    def bold(self, s, delay=None):
        """
        write the string in bold
        """
        self(self.boldstart, delay)
        self(s, delay)
        self(self.boldend, delay)

    def clear(self):
        """
        clear the screen (where possible)
        """
        self(self.clearscreen, delay=0)


class Space:
    """
    helper for printing a number of spaces
    """
    def __getattr__(self, numspaces):
        return ' ' * int(numspaces)


def teletype(baudrate=14400, out=sys.stdout, writer=None):
    """
    print the resume like a teletype
    baudrate=bits per second
    """
    if baudrate:
        delay = 1.0 / (baudrate / 8.0)
    else:
        delay = 0

    space = Space()
    write = writer or Writer(out=out, delay=delay)
    write.clear()
    intro = introduction()
    write.bold('{name:>80s}'.format(**intro))
    write("""
{address:>80s}
{city:>80s}
{email:>80s}
{phone:>80s}\n\n""".format(**intro))
    for jobname, job in work_experience().items():
        write('\n')
        write.bold('{name:<35s}'.format(name=jobname))
        write('{location:<20s}{years[0]} - {years[1]}\n'.format(**job))
        write('{0.4}{position}\n'.format(space, **job))
        for task in job['tasks']:
            write.bold('{0.6}*'.format(space))
            write(' {0}\n'.format(task))

    extra = other_experience()
    if extra:
        write.bold('\n\nIndependent Experience\n')
        for item, what in extra.items():
            write('{0.4}'.format(space))
            write.bold('{0:<30s}'.format(item))
            write(' {years[0]} - {years[1]}\n{0.6}{detail}\n\n'.format(space, **what))

    write('\n\n')


if __name__ == '__main__':
    if len(sys.argv) > 1:
        baudrate = int(sys.argv[1])
    else:
        baudrate = 14400
    teletype(baudrate)