# Path: lcopt/constants.py
# ASSET_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'assets')
# 
# BIOSPHERE_SI = 'biosphere3.pickle'
# 
# FORWAST_SI = 'forwast.pickle'
# 
# ECOINVENT_EXAMPLE = 'ecoinvent_example.lcopt'
# 
# FORWAST_EXAMPLE = 'forwast_example.lcopt'
# 
# DEFAULT_CONFIG = {
#     'ecoinvent':{
#     	'username': None,
#     	'password': None,
#         'version': DEFAULT_ECOINVENT_VERSION,
#         'system_model': DEFAULT_ECOINVENT_SYSTEM_MODEL,
#     },
#     'model_storage': {
#         'location': DEFAULT_SAVE_OPTION,
#         'project': DEFAULT_PROJECT_TYPE,
#         },
#     }
# 
# DEFAULT_SINGLE_PROJECT = "LCOPT_Models"
# 
# DEFAULT_ECOINVENT_VERSION = '3.3'
# 
# DEFAULT_ECOINVENT_SYSTEM_MODEL = 'cutoff'
# 
# DEFAULT_SAVE_OPTION = 'appdir'
# 
# DEFAULT_PROJECT_TYPE = 'unique'


import os
import yaml
import glob
import shutil
import appdirs
from .constants import (ASSET_PATH,
                        BIOSPHERE_SI, 
                        FORWAST_SI, 
                        ECOINVENT_EXAMPLE, 
                        FORWAST_EXAMPLE,
                        DEFAULT_CONFIG,
                        DEFAULT_SINGLE_PROJECT,
                        DEFAULT_ECOINVENT_VERSION,
                        DEFAULT_ECOINVENT_SYSTEM_MODEL,
                        DEFAULT_SAVE_OPTION,
                        DEFAULT_PROJECT_TYPE)
            shutil.copy(os.path.join(ASSET_PATH, FORWAST_SI), os.path.join(self.search_index_dir, FORWAST_SI))

        # lcoptview files
        self.lcoptview_dir = os.path.join(self.lcopt_dir, 'lcoptview')
        if not os.path.isdir(self.lcoptview_dir):
            os.mkdir(self.lcoptview_dir)

        # disclosures
        self.disclosures_dir = os.path.join(self.lcopt_dir, 'disclosures')
        if not os.path.isdir(self.disclosures_dir):
            os.mkdir(self.disclosures_dir)

        # simapro
        self.simapro_dir = os.path.join(self.lcopt_dir, 'simapro')
        if not os.path.isdir(self.simapro_dir):
            os.mkdir(self.simapro_dir)

    def load_config(self):
        with open(self.config_file, 'r') as cf:
            config = yaml.safe_load(cf)
        if config is None:
            self.write_default_config()
            config = DEFAULT_CONFIG
        return config

    def refresh(self):
        with open(self.config_file, 'r') as cf:
            self.config = yaml.safe_load(cf)

    def write_default_config(self):

# Path: lcopt/constants.py
# ASSET_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'assets')
# 
# BIOSPHERE_SI = 'biosphere3.pickle'
# 
# FORWAST_SI = 'forwast.pickle'
# 
# ECOINVENT_EXAMPLE = 'ecoinvent_example.lcopt'
# 
# FORWAST_EXAMPLE = 'forwast_example.lcopt'
# 
# DEFAULT_CONFIG = {
#     'ecoinvent':{
#     	'username': None,
#     	'password': None,
#         'version': DEFAULT_ECOINVENT_VERSION,
#         'system_model': DEFAULT_ECOINVENT_SYSTEM_MODEL,
#     },
#     'model_storage': {
#         'location': DEFAULT_SAVE_OPTION,
#         'project': DEFAULT_PROJECT_TYPE,
#         },
#     }
# 
# DEFAULT_SINGLE_PROJECT = "LCOPT_Models"
# 
# DEFAULT_ECOINVENT_VERSION = '3.3'
# 
# DEFAULT_ECOINVENT_SYSTEM_MODEL = 'cutoff'
# 
# DEFAULT_SAVE_OPTION = 'appdir'
# 
# DEFAULT_PROJECT_TYPE = 'unique'


# Path: lcopt/data_store.py
import os
import yaml
import glob
import shutil
import appdirs
from .constants import (ASSET_PATH,
                        BIOSPHERE_SI, 
                        FORWAST_SI, 
                        ECOINVENT_EXAMPLE, 
                        FORWAST_EXAMPLE,
                        DEFAULT_CONFIG,
                        DEFAULT_SINGLE_PROJECT,
                        DEFAULT_ECOINVENT_VERSION,
                        DEFAULT_ECOINVENT_SYSTEM_MODEL,
                        DEFAULT_SAVE_OPTION,
                        DEFAULT_PROJECT_TYPE)

            shutil.copy(os.path.join(ASSET_PATH, FORWAST_SI), os.path.join(self.search_index_dir, FORWAST_SI))

        # lcoptview files
        self.lcoptview_dir = os.path.join(self.lcopt_dir, 'lcoptview')
        if not os.path.isdir(self.lcoptview_dir):
            os.mkdir(self.lcoptview_dir)

        # disclosures
        self.disclosures_dir = os.path.join(self.lcopt_dir, 'disclosures')
        if not os.path.isdir(self.disclosures_dir):
            os.mkdir(self.disclosures_dir)

        # simapro
        self.simapro_dir = os.path.join(self.lcopt_dir, 'simapro')
        if not os.path.isdir(self.simapro_dir):
            os.mkdir(self.simapro_dir)

    def load_config(self):
        with open(self.config_file, 'r') as cf:
            config = yaml.safe_load(cf)
        if config is None:
            self.write_default_config()
            config = DEFAULT_CONFIG
        return config

    def refresh(self):
        with open(self.config_file, 'r') as cf:
            self.config = yaml.safe_load(cf)

    def write_default_config(self):

        self.write_config(DEFAULT_CONFIG)