'''
Created on Jun 30, 2020 @author: Levi
'''

import zipfile
import os
from xml.etree import ElementTree as ET
from dataclasses import dataclass

@dataclass
class Account:
    name: str = None
    folder: str = None
    id: str = None
    updated: bool = None
    imported: bool = None

# print(os.environ)
us_profile = os.environ.get('USERPROFILE').replace('\\', '/')
# print(us_profile)
teamnet = 'appdata/Roaming/Teamnet/'
path = os.path.join(us_profile, teamnet)
conf_folder = list(filter(lambda f: 'Teamnet' in f, os.listdir(path)))[0]
path = os.path.join(path, conf_folder)
ver = os.listdir(path)[-1]
ver_folder = os.path.join(path, ver)
# print(os.listdir(ver_folder))
conf_file = os.path.join(ver_folder, 'user.config')

revisal_tree = ET.parse(conf_file)
root = revisal_tree.getroot()
# for child in root[0][0][8][0][0]:
accounts = []
for child in root.iter('AngajatorInfo'):
    account = Account()
    for name, dir_, ident, isupd, isimport in zip(child.iter('Nume'), child.iter('Folder'), child.iter('Identificator'), child.iter('IsUpdated'), child.iter('IsImportFinished')):
        account.name = name.text
        account.folder = dir_.text
        account.id = ident.text
        account.updated = isupd.text
        account.imported = isimport.text
    accounts.append(account)
path1 = os.path.join(us_profile, teamnet)
db_folder = list(filter(lambda f: 'Revisal' in f, os.listdir(path1)))[0]
print(db_folder)
pass

# path = 'c:/Users/levi/Documents/DECEL/Revisal Hg500/Rapoarte/'
#
# 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]

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