from util import fill_vars
from errors import ConvertError


class SupervisionRules(object):
    def __init__(self, log):
        self.log = log
        # A dictionary to hold the variables coded in each phase, with default values.
        self.supervision_vars = {}
        self.supervision_vars['agencytype'] = {'agencytype': 99}
        self.supervision_vars['confine'] = {'confine': 9,
                                            'cnfadmyr': 9999,
                                             'cnfadmmo': 99,
                                             'cnfadmda': 99,
                                             'cnfrelyr': 9999,
                                             'cnfrelmo': 99,
                                             'cnfrelda': 99,
                                             'cnfreltype': 99,
                                             'suptype': 99,
                                             'supstartyr': 9999,
                                             'supstartmo': 99,
                                             'supstartda': 99,
                                             'supendyr': 9999,
                                             'supendmo': 99,
                                             'supendda': 99,
                                             'supreltype': 99,
                                             'discharge': 99,
                                             'dischyr': 9999,
                                             'dischmo': 99,
                                             'dischda': 99,
                                            }
        self.supervision_vars['nonsup'] = {'nonsup': 5}

    # A private function to initialize all the variables for a phase
    def _code_unknown(self, phase, row):
        for var, val in self.supervision_vars[phase].iteritems():
            row[var] = val

    # An abstract function to handle basic coding
    def _code(self, phase, crosswalk, row):
        self._code_unknown(phase, row)

        xwalk_keys = sorted(crosswalk.keys())
        num_crosswalks = len(xwalk_keys)
        for i in xrange(0, num_crosswalks):
            xwalk = crosswalk[xwalk_keys[i]]
            key = '|'.join(xwalk.metadata.get_key(row)) if len(xwalk.metadata.keys) > 1 else xwalk.metadata.get_key(row, as_str=True)
            if key == '|' or not key:
                continue
            try:
                values = xwalk.convert(key, split='|')
                fill_vars([var[0] for var in xwalk.vars], values, row)
                break
            except ConvertError:
                if i != num_crosswalks - 1:
                    continue
                else:
                    self.log.write(phase, [row['id'], row['rapstatex'], key, "key not in crosswalk"])

    # Code agencytype
    def code_agencytype(self, crosswalk, row):
        self._code('agencytype', crosswalk, row)

    # Code confine
    def code_confine(self, crosswalk, row):
        self._code('confine', crosswalk, row)

    def code_nonsup(self, crosswalk, row):
        self._code('nonsup', crosswalk, row)