'''
Created on Apr 27, 2020 @author: Levi
'''

import xml.etree.ElementTree as ET
import os
import zipfile
import re
import sys
from collections import namedtuple

# ent_name = re.search('[A-Z ]*', sys.argv[0])
ent_name = \
    'mozaik'.upper()\
    # input(' name like ').upper()

path = 'c:/Users/DECEL/Revisal Hg500/Rapoarte/'
# working_dir = os.getcwd()
def unzip_rvs(path, entity_name=None):
    if entity_name:
        rvs_files = list(filter(lambda f: entity_name in f and '.rvs' in f, os.listdir(path)))
    else:
        rvs_files = list(filter(lambda f: f.endswith('.rvs'), os.listdir(path)))
    rvs_files_sorted = sorted(rvs_files, key=lambda x: os.stat(os.path.join(path, x)).st_mtime, reverse=True)
    rvs_file = rvs_files_sorted[0]

    with zipfile.ZipFile(os.path.join(path, rvs_file)) as zip_file:
        zip_file.extractall(path=path)


unzip_rvs(path=path, entity_name=ent_name)

xml_files = list(filter(lambda f: f.endswith('.xml'), os.listdir(path)))
xml_files_sorted = sorted(xml_files, key=lambda x: os.stat(os.path.join(path, x)).st_mtime, reverse=True)
print(xml_files_sorted[0])

# tree = ET.parse('S.C. MOZAIK CONSULTING SRL S.R.L. 17259191 Apr 30 2020 13_40_26.xml')
tree = ET.parse(f'{path}{xml_files_sorted[0]}')
root = tree.getroot()


# roots = ('{http://schemas.datacontract.org/2004/07/Revisal.Entities}Cor',
#          '{http://schemas.datacontract.org/2004/07/Revisal.Entities}StareCurenta',
#          '{http://schemas.datacontract.org/2004/07/Revisal.Entities}TimpMunca')
# d = []
# synt = re.search('(?<=})[A-Za-z]+', roots[0])
# print(synt.group(0))
Contract = namedtuple('Contract',
                      [
                          'codcor',
                          'datacontract',
                          'tipcontract',
                          'tipdurata',
                          'timpmunca',
                          'datainceput',
                          'datasfarsit',
                          'detalii',
                          'numar',
                          'salariu',
                          'stare',
                          'inceputsusp',
                          'sfarsitsusp',
                          'incetaresusp',
                          'datacons',
                          'dataincetare',
                          'temei'
                      ],
                      defaults=[0])

Revisal = namedtuple('Revisal',
                     [
                         'nume',
                         'prenume',
                         'adresa',
                         'cnp',
                         'nationalitate',
                         'tara',
                         'tipact',
                         'contracts'
                     ],
                     defaults=[0])
contracts = []


def get_revisal(deep=5):
    global root
    for idx in range(deep):
        for child in root[1]:
            try:
                if child[5][idx][15].attrib['{http://www.w3.org/2001/XMLSchema-instance}type'] == 'ContractStareSuspendare':
                    contract = Contract(codcor=child[5][idx][1][0].text, datacontract=child[5][idx][3].text[:10], datacons=child[5][idx][2].text[:10],
                                        datainceput=child[5][idx][4].text[:10], datasfarsit=child[5][idx][5].text[:10],
                                        detalii=child[5][idx][7].text, numar=child[5][idx][10].text, salariu=child[5][idx][13].text,
                                        stare=child[5][idx][15].attrib['{http://www.w3.org/2001/XMLSchema-instance}type'],
                                        inceputsusp=child[5][idx][15][3].text[:10], sfarsitsusp=child[5][idx][15][5].text[:10],
                                        incetaresusp=child[5][idx][15][1].text[:10], dataincetare=child[5][idx][15][4].text[:10],
                                        temei=child[5][idx][15].text,
                                        tipcontract=child[5][idx][17].text, tipdurata=child[5][idx][18].text,
                                        timpmunca=child[5][idx][16][2].text)
                    rvs = Revisal(nume=child[10].text, prenume=child[11].text, adresa=child[0].text, cnp=child[3].text,
                                  nationalitate=child[9][0].text, tara=child[12][0].text, tipact=child[13].text, contracts=contract)
                    contracts.append(rvs)
                elif child[5][idx][15].attrib['{http://www.w3.org/2001/XMLSchema-instance}type'] == 'ContractStareIncetare':
                    contract = Contract(codcor=child[5][idx][1][0].text, datacontract=child[5][idx][3].text[:10], datacons=child[5][idx][2].text[:10],
                                        datainceput=child[5][idx][4].text[:10], datasfarsit=child[5][idx][5].text,
                                        detalii=child[5][idx][7].text, numar=child[5][idx][10].text, salariu=child[5][idx][13].text,
                                        stare=child[5][idx][15].attrib['{http://www.w3.org/2001/XMLSchema-instance}type'],
                                        dataincetare=child[5][idx][15][3].text[:10], temei=child[5][idx][15][5].text,
                                        inceputsusp='', sfarsitsusp='', incetaresusp='',
                                        tipcontract=child[5][idx][17].text, tipdurata=child[5][idx][18].text,
                                        timpmunca=child[5][idx][16][2].text)
                    rvs = Revisal(nume=child[10].text, prenume=child[11].text, adresa=child[0].text, cnp=child[3].text,
                                  nationalitate=child[9][0].text, tara=child[12][0].text, tipact=child[13].text, contracts=contract)
                    contracts.append(rvs)
                elif child[5][idx][15].attrib['{http://www.w3.org/2001/XMLSchema-instance}type'] == 'ContractStareActiv':
                    contract = Contract(codcor=child[5][idx][1][0].text, datacontract=child[5][idx][3].text[:10], datacons=child[5][idx][2].text[:10],
                                        datainceput=child[5][idx][4].text[:10], datasfarsit=child[5][idx][5].text,
                                        detalii=child[5][idx][7].text, numar=child[5][idx][10].text, salariu=child[5][idx][13].text,
                                        stare=child[5][idx][15].attrib['{http://www.w3.org/2001/XMLSchema-instance}type'],
                                        inceputsusp='', sfarsitsusp='', incetaresusp='', dataincetare='',
                                        tipcontract=child[5][idx][17].text, tipdurata=child[5][idx][18].text,
                                        timpmunca=child[5][idx][16][2].text)
                    rvs = Revisal(nume=child[10].text, prenume=child[11].text, adresa=child[0].text, cnp=child[3].text,
                                  nationalitate=child[9][0].text, tara=child[12][0].text, tipact=child[13].text, contracts=contract)
                    contracts.append(rvs)
            except IndexError:
                continue
    return contracts


if __name__ == '__main__':
    for i in get_revisal(deep=5):
        print(i.nume, i.prenume)
