# 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)

def lcopt_biosphere_setup():
    print("Running bw2setup for lcopt - this only needs to be done once")
    bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT)
    bw2.bw2setup()



def lcopt_bw2_autosetup(ei_username=None, ei_password=None, write_config=None, ecoinvent_version='3.3', ecoinvent_system_model = "cutoff", overwrite=False):  

    """
    Utility function to automatically set up brightway2 to work correctly with lcopt.

    It requires a valid username and password to login to the ecoinvent website.

    These can be entered directly into the function using the keyword arguments `ei_username` and `ei_password` or entered interactively by using no arguments.
    
    `ecoinvent_version` needs to be a string representation of a valid ecoinvent database, at time of writing these are "3.01", "3.1", "3.2", "3.3", "3.4"

    `ecoinvent_system_model` needs to be one of "cutoff", "apos", "consequential"

    To overwrite an existing version, set overwrite=True
    """

    ei_name = "Ecoinvent{}_{}_{}".format(*ecoinvent_version.split('.'), ecoinvent_system_model)

    config = check_for_config()
    # If, for some reason, there's no config file, write the defaults
    if config is None:
        config = DEFAULT_CONFIG

# 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)


def lcopt_biosphere_setup():
    print("Running bw2setup for lcopt - this only needs to be done once")
    bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT)
    bw2.bw2setup()



def lcopt_bw2_autosetup(ei_username=None, ei_password=None, write_config=None, ecoinvent_version='3.3', ecoinvent_system_model = "cutoff", overwrite=False):  

    """
    Utility function to automatically set up brightway2 to work correctly with lcopt.

    It requires a valid username and password to login to the ecoinvent website.

    These can be entered directly into the function using the keyword arguments `ei_username` and `ei_password` or entered interactively by using no arguments.
    
    `ecoinvent_version` needs to be a string representation of a valid ecoinvent database, at time of writing these are "3.01", "3.1", "3.2", "3.3", "3.4"

    `ecoinvent_system_model` needs to be one of "cutoff", "apos", "consequential"

    To overwrite an existing version, set overwrite=True
    """

    ei_name = "Ecoinvent{}_{}_{}".format(*ecoinvent_version.split('.'), ecoinvent_system_model)

    config = check_for_config()
    # If, for some reason, there's no config file, write the defaults
    if config is None:
        config = DEFAULT_CONFIG

        with open(storage.config_file, "w") as cfg: