#!/usr/bin/env python
# -*- coding: utf8 -*-

import re
from BitVector import BitVector

from GenericFile import GenericFile

class MOFile (GenericFile):

    name = "Automat Moore'a"
    extension = 'MO'

    def __init__(self, content=None):
        GenericFile.__init__(self)

        if content is not None:
            self.init_from_content(content)

        self.state = 0

    def init_from_content(self, content):
        #TODO: Don't know how to make it YET
        pass

    def parse_body(self, content):
        self._function = []
        split = []

        for line in content:
            if not re.compile('^[\da-zA-Z] [\-\da-zA-Z]{' + str(self.args) +
                    '} [\da-zA-Z]$').match(line):
                #TODO: Exception - bad line
                pass

            split = re.split(' ', line)

            self._function.append((
                    [self.char_to_value(char) for char in split[1]],
                    self.char_to_value(split[2]))
                )

    def prepare_body(self):
        result = []

        for line, i in zip(self._function, range(0, \
            len(self._function))):
            result.append(
                self.value_to_char(i) + ' ' \
                + [self.value_to_char(val) \
                    for val in line[0]] + ' ' \
                + self.value_to_char(line[1]) + '\n')

        return result

    def calculate_function(self, args):
        old_state = self.state
        self.state = self._function[self.state][0][args]

        return self._function[old_state][1]

