# Path: lcopt/data_store.py
# class LcoptStorage():
#     def __init__(self):
#     def load_config(self):
#     def refresh(self):
#     def write_default_config(self):
#     def write_config(self, config):
#     def models(self):
#     def search_indices(self):
#     def project_type(self):
#     def single_project_name(self):
#     def ecoinvent_version(self):
#     def ecoinvent_system_model(self):
#     def save_option(self):
# 
# Path: lcopt/constants.py
# DEFAULT_PROJECT_STEM = "LCOPT_Setup_"
# 
# DEFAULT_BIOSPHERE_PROJECT = "LCOPT_Setup_biosphere"
# 
# DEFAULT_DB_NAME = DEFAULT_PROJECT_STEM[:-1]
# 
# FORWAST_PROJECT_NAME = DEFAULT_PROJECT_STEM + "Forwast"
# 
# FORWAST_URL = r"https://lca-net.com/wp-content/uploads/forwast.bw2package.zip"
# 
# ASSET_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'assets')
# 
# 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"


import tempfile
import requests
import zipfile
import io
import os
import eidl
import yaml
import pickle
import getpass
import socket
    import brightway2 as bw2
from functools import partial
from .data_store import storage
from .constants import (DEFAULT_PROJECT_STEM,
                        DEFAULT_BIOSPHERE_PROJECT,
                        DEFAULT_DB_NAME,
                        FORWAST_PROJECT_NAME,
                        FORWAST_URL,
                        ASSET_PATH,
                        DEFAULT_CONFIG,
                        DEFAULT_SINGLE_PROJECT)
    print('Copied old lcopt setup project')
    return True

def check_for_config():

    config = None

    try:
        config = storage.config
    except:
        pass

    return config

def write_search_index(project_name, ei_name, overwrite=False):
    si_fp = fix_mac_path_escapes(os.path.join(storage.search_index_dir, '{}.pickle'.format(ei_name))) #os.path.join(ASSET_PATH, '{}.pickle'.format(ei_name))

    if not os.path.isfile(si_fp) or overwrite:

        search_index = create_search_index(project_name, ei_name)

        with open(si_fp, 'wb') as handle:
            print("Writing {} search index to search folder".format(ei_name))
            pickle.dump(search_index, handle)
    #else:
    #    print("{} search index already exists in assets folder".format(ei_name))


def lcopt_biosphere_setup():
    print("Running bw2setup for lcopt - this only needs to be done once")

# Path: lcopt/data_store.py
# class LcoptStorage():
#     def __init__(self):
#     def load_config(self):
#     def refresh(self):
#     def write_default_config(self):
#     def write_config(self, config):
#     def models(self):
#     def search_indices(self):
#     def project_type(self):
#     def single_project_name(self):
#     def ecoinvent_version(self):
#     def ecoinvent_system_model(self):
#     def save_option(self):
# 
# Path: lcopt/constants.py
# DEFAULT_PROJECT_STEM = "LCOPT_Setup_"
# 
# DEFAULT_BIOSPHERE_PROJECT = "LCOPT_Setup_biosphere"
# 
# DEFAULT_DB_NAME = DEFAULT_PROJECT_STEM[:-1]
# 
# FORWAST_PROJECT_NAME = DEFAULT_PROJECT_STEM + "Forwast"
# 
# FORWAST_URL = r"https://lca-net.com/wp-content/uploads/forwast.bw2package.zip"
# 
# ASSET_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'assets')
# 
# 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"


# Path: lcopt/utils.py
import tempfile
import requests
import zipfile
import io
import os
import eidl
import yaml
import pickle
import getpass
import socket
    import brightway2 as bw2
from functools import partial
from .data_store import storage
from .constants import (DEFAULT_PROJECT_STEM,
                        DEFAULT_BIOSPHERE_PROJECT,
                        DEFAULT_DB_NAME,
                        FORWAST_PROJECT_NAME,
                        FORWAST_URL,
                        ASSET_PATH,
                        DEFAULT_CONFIG,
                        DEFAULT_SINGLE_PROJECT)

    print('Copied old lcopt setup project')
    return True

def check_for_config():

    config = None

    try:
        config = storage.config
    except:
        pass

    return config

def write_search_index(project_name, ei_name, overwrite=False):
    si_fp = fix_mac_path_escapes(os.path.join(storage.search_index_dir, '{}.pickle'.format(ei_name))) #os.path.join(ASSET_PATH, '{}.pickle'.format(ei_name))

    if not os.path.isfile(si_fp) or overwrite:

        search_index = create_search_index(project_name, ei_name)

        with open(si_fp, 'wb') as handle:
            print("Writing {} search index to search folder".format(ei_name))
            pickle.dump(search_index, handle)
    #else:
    #    print("{} search index already exists in assets folder".format(ei_name))


def lcopt_biosphere_setup():
    print("Running bw2setup for lcopt - this only needs to be done once")

    bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT)