from configparser import ConfigParser, ExtendedInterpolation
from pathlib import Path as p
import logging
import ast

log = logging.getLogger(__name__)

class Config:

    class __Config:

        def __init__(self, config_path, debug):

            log.debug('Initializing Config module.')
            self.path = config_path
            self.debug = debug
            self.config = ConfigParser(delimiters=('='), interpolation=ExtendedInterpolation())
            self.startup()

        def startup(self):

            hassio = '/srv/homeassistant/lib/python3.6/site-packages/gmapslocsharing/core/config.conf'
            docker = '/config/deps/lib/python3.7/site-packages/gmapslocsharing/core/config.conf'

            if p(hassio).exists():
                log.info('Loading hass.io config path.')
                log.debug('Config Path: {}'.format(hassio))
                self.config.read(hassio)
            elif p(docker).exists():
                log.info('Loading docker config path.')
                log.debug('Config Path: {}'.format(docker))
                self.config.read(docker)
            else:
                suffix = '/site-packages/gmapslocsharing/core/config.conf'
                log.info('Loading manual install config path.')
                c = ''.join([_.as_posix() for _ in (self.path / 'deps/lib').glob('python*')])
                c += suffix
                log.debug('Config Path: {}'.format(c))
                self.config.read(c)

        def get(self, section:str, key:str):

            c = self.config.get(section, key, raw=True)

            try:
                return ast.literal_eval(c)
            except:
                return c

        def set(self, section:str, key:str, value):
            """
            input: section, key, value
            """

            if not self.config.has_section(section):
                self.config.add_section(section)
            if isinstance(value, list):
                self.config.set(section, key, str(value))
            else:
                self.config.set(section, key, value)

        @property
        def email(self):
            return self.get('account', 'email')

        @property
        def password(self):
            return self.get('account', 'password')

        @property
        def path_chrome(self):
            return self.path / 'chrome'

        @property
        def path_debug(self):
            return self.path / 'debug'

        @property
        def path_debug_core(self):
            return self.path / 'debug/core'

        @property
        def path_debug_browser(self):
            return self.path / 'debug/browser'

        @property
        def path_debug_location(self):
            return self.path / 'debug/location'

        @property
        def path_debug_backup(self):
            return self.path / 'debug/backup'

        @property
        def path_chrome_nuke(self):
            chrome_cookie_paths = self.get('paths', 'chrome_cookies')
            chrome_cache_paths = self.get('paths', 'chrome_cache')
            return [self.path / path for path in (chrome_cookie_paths + chrome_cache_paths)]

        @property
        def requests_get(self):
            return self.get('requests', 'get')

        @property
        def requests_path(self):
            return self.get('requests', 'path')

        @property
        def login_start(self):
            return self.get('urls', 'login_start')

        @property
        def login_success(self):
            return self.get('urls', 'login_success')

        @property
        def cookie_check(self):
            return self.get('urls', 'cookie_check')

    instance = None

    def __init__(self, config_path=None, debug=False):

        if not Config.instance:
            Config.instance = Config.__Config(config_path, debug)

    def __getattr__(self, name):

        return getattr(self.instance, name)
