from datetime import datetime

def set_doa(arrest_records, sentence_records):
    """If DATE of ARREST fields (ARRYR, ARRMO,ARRDA) are missing (9999,99,99), we need to fill this in if possible. 
        Follow the steps below, to find the first successful means for completion. When arrest is filled in, flag the case: ADATEFLAG=0.
    
    1. If another entry in the same cycle has a non-missing arrest date, copy this in when brought into the recid. relational file.
    
    2. If the entry has a non-missing (OFFYR, OFFMO, OFFDA), insert this into the arrest date feels when brought into the recid. Relational file.
    
    3. If the entry has a non-missing (ADISPYR, ADISPMO, ADISPDA), insert this into the arrest date feels when brought into the recid. Relational file.
    
    4. If the entry has a non-missing (CTDISPYR, CTDISPMO, CTDISPDA), insert this into the arrest date feels when brought into the recid. Relational file.
       
    5. Take from arrest date in (Casenum, Cycle-1)
    
    6. If the entry has a non-missing (AEVENTYR, AEVENTMO, AEVENTDA), insert this into the arrest date feels when brought into the recid. Relational file.
    
    7. If the only problem with the incoming date is that the year is 20XX and is later than the current year, then change the year to 19XX
    """
    arrest_cycle_date = {}
    sentence_cycle_date = {}
    
    #get the earliest date for each arrest cycle
    for row in arrest_records.itervalues():    
        if row['arryr'] != 9999:
            if row['cycnum'] not in arrest_cycle_date:
                arrest_cycle_date[row['cycnum']] = datetime(row['arryr'], row['arrmo'], row['arrda'])
            else:
                _dt = datetime(row['arryr'], row['arrmo'], row['arrda'])
                if datetime(row['arryr'], row['arrmo'], row['arrda']) < arrest_cycle_date[row['cycnum']]:
                    arrest_cycle_date[row['cycnum']] = _dt
    
    #get the earliest date for each sentence cycle
    for row in sentence_records.itervalues():
        if row['cdispyr'] != 9999:
            #get the earliest date for a given cycle
            if row['cycnum'] not in sentence_cycle_date:
                sentence_cycle_date[row['cycnum']] = datetime(row['cdispyr'], row['cdispmo'], row['cdispda'])
            else:
                _dt = datetime(row['cdispyr'], row['cdispmo'], row['cdispda'])
                if datetime(row['cdispyr'], row['cdispmo'], row['cdispda']) < sentence_cycle_date[row['cycnum']]:
                    sentence_cycle_date[row['cycnum']] = _dt
                    
    for id, row in sorted(arrest_records.iteritems(), key= lambda k: k[1]['cycnum']):
        row['adateflag'] = True
        #date is valid set adateflag and move to next record
        if row['arryr'] != 9999:
            row['adateflag'] = False
            continue
        
        #step1
        elif row['cycnum'] in arrest_cycle_date:
            row['arryr'] = arrest_cycle_date[row['cycnum']].year
            row['arrmo'] = arrest_cycle_date[row['cycnum']].month
            row['arrda'] = arrest_cycle_date[row['cycnum']].day
            row['adateflag'] = False
            
        #step2
        elif row['offyr'] != 9999:
            row['arryr'], row['arrmo'], row['arrda'] = row['offyr'], row['offmo'], row['offda']
            row['adateflag'] = False
            
        #step3
        elif row['adspyr'] != 9999:
            row['arryr'], row['arrmo'], row['arrda'] = row['adspyr'], row['adspmo'], row['adspda']
            row['adateflag'] = False
            
        #step4
        elif row['cycnum'] in sentence_cycle_date:
            row['arryr'] = sentence_cycle_date[row['cycnum']].year
            row['arrmo'] = sentence_cycle_date[row['cycnum']].month
            row['arrda'] = sentence_cycle_date[row['cycnum']].day
            row['adateflag'] = False
            
        #step5
        elif row['cycnum'] > 1 and row['cycnum'] - 1 in arrest_cycle_date:
            row['arryr'] = arrest_cycle_date[row['cycnum'] - 1].year
            row['arrmo'] = arrest_cycle_date[row['cycnum'] - 1].month
            row['arrda'] = arrest_cycle_date[row['cycnum'] - 1].day
            row['adateflag'] = False
            
        #step 6
        elif row['eventyr'] != 9999:
            row['arryr'], row['arrmo'], row['arrda'] = row['eventyr'], row['eventmo'], row['eventda']
            row['adateflag'] = False
            
        #step 7
        else:
            try:
                v = row['arrdatex'].replace('-', '')
                if len(v) == 8 and int(value[:4]) >= 1850:
                    dt = strptime(v, '%Y%m%d')
                    if dt[0] > date.today().year:
                        year = int('19%s' % str(dt[0])[2:])
                        row['arryr'], row['arrmo'], row['arrda'] = year, dt[1], dt[2]
                        row['adateflag'] = False
            except:
                row['adateflag'] = True
                