#python imports
from os import path
from datetime import datetime
from itertools import izip_longest, izip
from re import sub
#third-party
import cardsharp as cs
#src
from .log import Log
from .configuration import config

def _row_test(i, r1, r2, vars, log, segment):
    err = 'Row %i does not match after saving and loading' % i
    
    if segment != 'summary':
        if 'cstate' in vars:
            region = 'cstate'
        elif 'pstate' in vars:
            region = 'pstate'
        elif 'sstate' in vars:
            region = 'sstate'
        else:
            region = 'astate'

    if r1 is None:
        log.write_compare('diff_size', [int(r2['id']), '', str(r2)], 78)
        return

    if r2 is None:
        log.write_compare('diff_size', [int(r1['id']), str(r1), ''], 78)
        return

    for var in vars:
        if var.name in ('id', 'ppg', 'pcdv', 'pdom', 'pweap'):
            continue
        if r1[var.name] != r2[var.name]:
            if var.name in ('aori_invalid', 'cori_invalid'):
                continue
            
            if segment == 'summary': #if summary difference report
                log.write_compare('diff', [int(r1['casenum']), var.name, str(r1[var.name]), str(r2[var.name])], 78)
            else:
                log.write_compare('diff', [r1['id'], r1[region], r1['rapstatex'], var.name, r1[var.name], str(r2[var.name])], 77)
            
            
    
def compare(ds1, ds2, **kw):
    '''
    ds1: one of two datasets to compare
    ds2: two of two datasets to compare
    out_format: the format for the output to log files, 
        var = only output variable difference (default)
        row = output full row difference
    verbose: print output default = False
    '''
    log = Log(**kw)
    
    #validate same number of variables in both datasets
    if len(ds1.variables) != len(ds2.variables):
        print 'Number of variables do not match'

    #compare all rows of the datasets
    for i, (r1, r2) in enumerate(izip_longest(ds1, ds2)):
        # assert r1 is not None and r2 is not None, "Datasets have unequal number of rows"
        _row_test(i, r1, r2, ds1.variables, log, kw['segment'])
                    