from tddcommit import Kind

class StateTransitionError(Exception):
    pass


class State:
    def __init__(self):
        self._current_state = None
        self._had_green = False

    def allowed(self, proposed_state):
        if not self._current_state:
            if proposed_state is Kind.initial:
                return True
        elif self._current_state is Kind.initial:
            if proposed_state is Kind.red \
            or proposed_state is Kind.merge \
            or proposed_state is Kind.beige:
                return True
        elif self._current_state is Kind.red:
            if proposed_state is Kind.green:
                return True
        elif self._current_state is Kind.green \
          or self._current_state is Kind.refactor \
          or self._current_state is Kind.merge \
          or self._current_state is Kind.beige:
            # Check to prevent refactors when there has been no green
            if (self._current_state is Kind.beige \
            or self._current_state is Kind.merge) \
            and proposed_state is Kind.refactor \
            and not self._had_green:
                return False
            # Otherwise allow all sensible state changes
            if proposed_state is Kind.refactor \
            or proposed_state is Kind.red \
            or proposed_state is Kind.merge \
            or proposed_state is Kind.beige:
                return True
        return False

    def change(self, to_state):
        if self.allowed(to_state):
            self._current_state = to_state
            if to_state is Kind.green:
                self._had_green = True
        else:
            raise StateTransitionError(
                'It is not valid to move from ' + str(self._current_state)
                + 'to ' + str(to_state))

@@ -941,17 +941,16 @@
      if 
-(
 self._cu
@@ -969,19 +969,19 @@
 is Kind.
-bei
+mer
 ge %5C%0A   
@@ -1024,16 +1024,14 @@
 ind.
-merge) %5C
+beige:
 %0A   
@@ -1039,19 +1039,22 @@
         
-and
+    if
  propose
@@ -1078,30 +1078,16 @@
 efactor 
-%5C%0A            
 and not 
@@ -1099,24 +1099,28 @@
 _had_green:%0A
+    
             

REFACTOR Tidier state-transition conditionals.