__author__ = 'Wendell Jones'
import configparser
import os
import time
class Config(object):
    global ROOTDIR
    def __init__(self, ROOTDIR):
        self.config_options = {}
        self.configurer = configparser.ConfigParser()
        self.read_settings(ROOTDIR)

    def read_settings(self, ROOTDIR):
        if os.name == 'nt':
            self.configurer.read(ROOTDIR + '\\settings\\tvsync_settings.cfg')
        else:
            self.configurer.read(ROOTDIR + '/settings/tvsync_settings.cfg')
        print(f"Reading configuration file from {ROOTDIR}/settings")
        self.set_data()
        self.get_data()

    def set_data(self):
        self.config_options['loglevel'] = self.configurer.get('global', 'loglevel')
        self.config_options['hostname'] = self.configurer.get('dbsettings', 'hostname')
        self.config_options['mghostname'] = self.configurer.get('dbsettings', 'mghostname')
        self.config_options['dbname'] = self.configurer.get('dbsettings', 'dbname')
        self.config_options['mgdbname'] = self.configurer.get('dbsettings', 'mgdbname')
        self.config_options['mysqlusername'] = self.configurer.get('dbsettings', 'mysqlusername')
        self.config_options['pgsqlusername'] = self.configurer.get('dbsettings', 'pgsqlusername')
        self.config_options['mssqlusername'] = self.configurer.get('dbsettings', 'mssqlusername')
        self.config_options['mysqlpassword'] = self.configurer.get('dbsettings', 'mysqlpassword')
        self.config_options['pgsqlpassword'] = self.configurer.get('dbsettings', 'pgsqlpassword')
        self.config_options['mssqlpassword'] = self.configurer.get('dbsettings', 'mssqlpassword')
        self.config_options['pgsqlport'] = self.configurer.get('dbsettings', 'pgsqlport')
        self.config_options['mysqlport'] = self.configurer.get('dbsettings', 'mysqlport')
        self.config_options['mssqlport'] = self.configurer.get('dbsettings', 'mssqlport')
        self.config_options['mgport'] = self.configurer.get('dbsettings', 'mgport')
        self.config_options['updatetype'] = self.configurer.get('updatetype', 'type')
        self.config_options['apitype'] = self.configurer.get('api', 'type')
        self.config_options['updateschema'] = self.configurer.get('database', 'updateschema')
        self.config_options['dbtype'] = self.configurer.get('database', 'dbtype')
        self.config_options['initdb'] = self.configurer.get('database', 'initialize')
        self.config_options['initload'] = self.configurer.get('database', 'initload')
        self.config_options['skipcache'] = self.configurer.get('updatetype', 'cacheskip')
        self.config_options['refreshcache'] = self.configurer.get('updatetype', 'refreshcache')
        self.config_options['full'] = self.configurer.get('updatetype', 'full')
        self.config_options['monthly'] = self.configurer.get('updatetype', 'monthly')
        self.config_options['weekly'] = self.configurer.get('updatetype', 'weekly')
        self.config_options['daily'] = self.configurer.get('updatetype', 'daily')
        self.config_options['12h'] = self.configurer.get('updatetype', '12h')
        self.config_options['6h'] = self.configurer.get('updatetype', '6h')
        self.config_options['custom'] = self.configurer.get('updatetype', 'custom')        
        self.config_options['serieslist'] = list()
        self.config_options['timedifference'] = self.configurer.get('updatetype', self.config_options['updatetype'])
        self.config_options['current_timestamp'] = time.time()
        if self.config_options['timedifference'].lower() == 'full':
            self.config_options['timedifference'] = int(self.config_options['current_timestamp'])
        
    def get_data(self):
        return self.config_options

    def __del__(self):

        pass


def read_config_file(path_or_rootdir):
    """Read configuration and return a dict of options.

    If `path_or_rootdir` is a file path to a config file, it will be used
    directly. Otherwise it's treated as the project root directory and
    the config file is expected at `<ROOTDIR>/settings/tvsync_settings.cfg`.
    """
    configurer = configparser.ConfigParser()
    # determine config file path
    if os.path.isfile(path_or_rootdir):
        cfg_path = path_or_rootdir
    else:
        cfg_path = os.path.join(path_or_rootdir, 'settings', 'tvsync_settings.cfg')

    configurer.read(cfg_path)
    options = {}
    # Mirror keys used by the Config class
    options['loglevel'] = configurer.get('global', 'loglevel')
    options['hostname'] = configurer.get('dbsettings', 'hostname')
    options['mghostname'] = configurer.get('dbsettings', 'mghostname')
    options['dbname'] = configurer.get('dbsettings', 'dbname')
    options['mgdbname'] = configurer.get('dbsettings', 'mgdbname')
    options['mysqlusername'] = configurer.get('dbsettings', 'mysqlusername')
    options['pgsqlusername'] = configurer.get('dbsettings', 'pgsqlusername')
    options['mssqlusername'] = configurer.get('dbsettings', 'mssqlusername')
    options['mysqlpassword'] = configurer.get('dbsettings', 'mysqlpassword')
    options['pgsqlpassword'] = configurer.get('dbsettings', 'pgsqlpassword')
    options['mssqlpassword'] = configurer.get('dbsettings', 'mssqlpassword')
    options['pgsqlport'] = configurer.get('dbsettings', 'pgsqlport')
    options['mysqlport'] = configurer.get('dbsettings', 'mysqlport')
    options['mssqlport'] = configurer.get('dbsettings', 'mssqlport')
    options['mgport'] = configurer.get('dbsettings', 'mgport')
    options['updatetype'] = configurer.get('updatetype', 'type')
    options['apitype'] = configurer.get('api', 'type')
    options['updateschema'] = configurer.get('database', 'updateschema')
    options['dbtype'] = configurer.get('database', 'dbtype')
    options['initdb'] = configurer.get('database', 'initialize')
    options['initload'] = configurer.get('database', 'initload')
    options['skipcache'] = configurer.get('updatetype', 'cacheskip')
    options['refreshcache'] = configurer.get('updatetype', 'refreshcache')
    options['full'] = configurer.get('updatetype', 'full')
    options['monthly'] = configurer.get('updatetype', 'monthly')
    options['weekly'] = configurer.get('updatetype', 'weekly')
    options['daily'] = configurer.get('updatetype', 'daily')
    options['12h'] = configurer.get('updatetype', '12h')
    options['6h'] = configurer.get('updatetype', '6h')
    options['custom'] = configurer.get('updatetype', 'custom')
    options['serieslist'] = list()
    options['timedifference'] = configurer.get('updatetype', options['updatetype'])
    options['current_timestamp'] = time.time()
    if isinstance(options['timedifference'], str) and options['timedifference'].lower() == 'full':
        options['timedifference'] = int(options['current_timestamp'])
    return options
