# -*- coding: utf-8 -*-

import hashlib
import logging

import xml.etree.cElementTree as ET


ACCOUNT_FILE = 'storj_account_conf.xml'


class AccountManager:

    __logger = logging.getLogger('%s.AccountManager' % __name__)

    def __init__(self, login_email=None, password=None):
        self.login_email = login_email
        self.password = password

    def save_account_credentials(self):
        root = ET.Element('account')
        doc = ET.SubElement(root, 'credentials')

        ET.SubElement(doc, 'login_email').text = str(self.login_email)
        ET.SubElement(doc, 'password').text = \
            str(hashlib.sha256(self.password.encode('ascii')).hexdigest())
        ET.SubElement(doc, 'logged_in').text = '1'
        tree = ET.ElementTree(root)
        tree.write('storj_account_conf.xml')

    def if_logged_in(self):
        """Return True if user has already logged in with these credentials"""

        logged_in = '0'
        try:
            et = ET.parse('storj_account_conf.xml')
            for tags in et.iter('logged_in'):
                logged_in = tags.text

        except IOError:
            self.__logger.error('Error in Account Manager login')
            self.__logger.error('Function: if_logged_in')
            self.__logger.error('Credentials file not existing')
            return False

        return logged_in == '1'

    def logout(self):
        self.__logger.debug('TODO')
        self.__logger.debug('1')

    def get_user_password(self):
        password = ""
        try:
            et = ET.parse('storj_account_conf.xml')
            for tags in et.iter('password'):
                password = str(tags.text.encode('ascii'))
                self.__logger.debug('zpliku %s' % tags.text)
        except IOError as e:
            self.__logger.error(e)
            self.__logger.error('Error in Account Manager get password')
            self.__logger.error('Credentials file not existing')
        return password

    def get_user_email(self):
        email = ''
        try:
            et = ET.parse('storj_account_conf.xml')
            for tags in et.iter('login_email'):
                email = tags.text

        except IOError as e:
            self.__logger.error(e)
            self.__logger.error('Error in Account Manager get email')
            self.__logger.error('Credentials file not existing')

        return email

    def validate_password_local(self, password):
        password_hashed = str(hashlib.sha256(password.encode('ascii')).hexdigest())
        password_from_config = self.get_user_password()

        if password_from_config == password_hashed:
            return True
        else:
            return False


@@ -1390,68 +1390,359 @@
     
-self.__logger.debug('TODO')%0A        self.__logger.debug('1')
+print %22Log out action%22%0A        root = ET.Element(%22account%22)%0A        doc = ET.SubElement(root, %22credentials%22)%0A        ET.SubElement(doc, %22login_email%22).text = %220%22%0A        ET.SubElement(doc, %22password%22).text = %220%22%0A        ET.SubElement(doc, %22logged_in%22).text = %220%22%0A        tree = ET.ElementTree(root)%0A        tree.write(ACCOUNT_FILE)%0A        return True
 %0A%0A  

Implement logout