#!/usr/bin/env python
from generator.actions import Actions

import random
import re
import string
import struct
import sys
import numpy

char_maps = {
    'a': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','1','1','1','1','0','0',
        '0','0','0','0','0','1','0',
        '0','0','1','1','1','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','0','1','1','1','0','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'b': [
        '1','1','0','0','0','0','0',
        '0','1','0','0','0','0','0',
        '0','1','0','0','0','0','0',
        '0','1','1','1','1','0','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '1','0','1','1','1','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'c': [
          '0','1','1','1','1','0','0',
          '0','1','0','0','0','0','0',
          '0','1','0','0','0','0','0',
          '0','1','0','0','0','0','0',
          '0','1','0','0','0','0','0',
          '0','1','0','0','0','0','0',
          '0','1','0','0','0','0','0',
          '0','1','0','0','0','0','0',
          '0','1','0','0','0','0','0',
          '0','1','0','0','0','0','0',
          '0','1','1','1','1','0','0',
          '0','0','0','0','0','0','0',
    ],
    'd': [
        '0','0','0','0','1','1','0',
        '0','0','0','0','0','1','0',
        '0','0','0','0','0','1','0',
        '0','0','1','1','1','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','0','1','1','1','0','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'e': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','1','1','1','1','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','1','1','1','0',
        '0','1','0','0','0','0','0',
        '0','1','0','0','0','1','0',
        '0','0','1','1','1','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'f': [
        '0','0','1','1','1','1','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '1','1','1','1','1','1','1',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','1','1','1','1','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'g': [
        '0','0','0','0','0','0','0',
        '0','1','1','1','1','0','1',
        '0','1','0','0','0','0','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','1','1','1','1','0',
        '0','0','0','0','0','1','0',
        '0','0','0','0','0','1','0',
        '0','0','0','0','0','1','0',
        '0','0','1','1','1','0','0',
    ],
    'h': [
        '1','1','0','0','0','0','0',
        '0','1','1','1','0','0','0',
        '0','1','0','0','1','0','0',
        '0','1','0','0','0','0','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '1','1','1','0','1','1','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'i': [
        '0','0','0','1','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','1','1','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','1','1','1','1','1','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'j': [
        '0','0','0','0','1','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','1','1','1','1','0','0',
        '0','0','0','0','1','0','0',
        '0','0','0','0','1','0','0',
        '0','0','0','0','1','0','0',
        '0','0','0','0','1','0','0',
        '0','0','0','0','1','0','0',
        '0','0','0','0','1','0','0',
        '0','0','0','0','1','0','0',
        '0','1','1','1','0','0','0',
    ],
    'k': [
        '1','1','0','0','0','0','0',
        '0','1','0','0','0','0','0',
        '0','1','0','0','0','0','0',
        '0','1','0','0','1','1','0',
        '0','1','0','1','0','0','0',
        '0','1','1','0','0','0','0',
        '0','1','0','1','0','0','0',
        '0','1','0','0','1','0','0',
        '1','1','0','0','0','1','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'l': [
        '0','1','1','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','1','1','1','1','1','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'm': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '1','1','1','0','1','0','0',
        '0','1','0','1','0','1','0',
        '0','1','0','1','0','1','0',
        '0','1','0','1','0','1','0',
        '0','1','0','1','0','1','0',
        '1','1','0','1','0','1','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'n': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '1','0','1','1','1','0','0',
        '0','1','1','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '1','1','1','0','0','1','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'o': [
          '0','0','0','0','0','0','0',
          '0','1','1','1','1','1','0',
          '0','1','0','0','0','1','0',
          '0','1','0','0','0','1','0',
          '0','1','0','0','0','1','0',
          '0','1','0','1','0','1','0',
          '0','1','0','1','0','1','0',
          '0','1','0','1','0','1','0',
          '0','1','0','0','0','1','0',
          '0','1','0','0','0','1','0',
          '0','1','1','1','1','1','0',
          '0','0','0','0','0','0','0',
    ],
    'p': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '1','0','1','1','1','0','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','1','1','1','0','0',
        '0','1','0','0','0','0','0',
        '0','1','0','0','0','0','0',
        '1','1','1','0','0','0','0',
    ],
    'q': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','1','1','1','0','1',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','0','1','1','1','1','0',
        '0','0','0','0','0','1','0',
        '0','0','0','0','0','1','0',
        '0','0','0','0','1','1','1',
    ],
    'r': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','1','0','1','1','1','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','1','1','1','1','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    's': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','1','1','1','0','0',
        '0','1','0','0','0','1','0',
        '0','0','1','1','1','0','0',
        '0','0','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','0','1','1','1','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    't': [
        '0','0','0','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','1','1','1','1','1','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','0',
        '0','0','1','0','0','0','1',
        '0','0','0','1','1','1','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'u': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','1','1','0',
        '0','0','0','0','0','1','0',
        '1','1','0','0','0','1','0',
        '1','1','0','0','0','1','0',
        '1','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','0','1','1','1','0','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'v': [
        '0','0','0','0','0','0','0',
        '1','1','1','0','1','1','1',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','0','1','0','1','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'w': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '1','1','1','0','1','1','1',
        '0','1','0','0','0','1','0',
        '0','1','0','1','0','1','0',
        '0','1','0','1','0','1','0',
        '0','0','1','0','1','0','0',
        '0','0','1','0','1','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'x': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '1','1','1','0','1','1','1',
        '0','0','1','0','1','0','0',
        '0','0','0','1','0','0','0',
        '0','0','1','0','1','0','0',
        '0','1','0','0','0','1','0',
        '1','1','0','0','0','1','1',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    'y': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '1','1','1','0','1','1','1',
        '0','1','0','0','0','1','0',
        '0','1','0','0','0','1','0',
        '0','0','1','0','1','0','0',
        '0','0','1','1','1','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','0','0','1','0','0','0',
        '0','1','1','0','0','0','0',
    ],
    'z': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','1','1','1','1','1','0',
        '0','1','0','0','1','0','0',
        '0','0','0','1','0','0','0',
        '0','0','1','0','0','0','0',
        '0','1','0','0','0','1','0',
        '0','1','1','1','1','1','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
    ' ': [
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
        '0','0','0','0','0','0','0',
    ],
}

class Machine(Actions):
    def start(self):
        self.state['char_set'] = string.lowercase + ' '
        self.state['string'] = ''
        self.state['output'] = []
        self.width = 0
        self.height = 12
        self.char_width = 7

    def pick_chars(self):
        for _ in xrange(random.randint(1, 64)):
            c = random.choice(self.state['char_set'])
            self.state['string'] += c

    def munge_maps(self):
        for idx in xrange(len(self.state['string'])):
            self.state['cur_map'] = char_maps[self.state['string'][idx]][:]
            changes = random.sample(xrange(len(self.state['cur_map'])), 4)
            for c_idx in changes:
                if self.state['cur_map'][c_idx] == '0':
                    self.state['cur_map'][c_idx] = '1'
                elif self.state['cur_map'][c_idx] == '1':
                    self.state['cur_map'][c_idx] = '0'

            self.state['output'].append(''.join(self.state['cur_map']))
            self.width += self.char_width



    def insert_newlines(self):
        self.state['final_output'] = ''
        for l in xrange(self.height):
            for i in xrange(len(self.state['output'])):
                self.state['final_output'] += self.state['output'][i][l * self.char_width:(l + 1) * self.char_width]
            self.state['final_output'] += '\n'

    def validate(self):
        self.read(expect='Welcome to the super awesome OCR engine! Enter your input to have it OCR\'d', delim='\n')
        self.write('Z1\n')
        self.write('{} {}\n'.format(self.width, self.height))
        self.write(self.state['final_output'])
        self.read(expect='Result: {}'.format(self.state['string']), delim='\n')
