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

    return True


def forwast_autodownload(FORWAST_URL):      

    """
    Autodownloader for forwast database package for brightway. Used by `lcopt_bw2_forwast_setup` to get the database data. Not designed to be used on its own
    """
    dirpath = tempfile.mkdtemp()
    r = requests.get(FORWAST_URL)
    z = zipfile.ZipFile(io.BytesIO(r.content))
    z.extractall(dirpath)
    return os.path.join(dirpath, 'forwast.bw2package')


def lcopt_bw2_forwast_setup(use_autodownload=True, forwast_path=None, db_name=FORWAST_PROJECT_NAME, overwrite=False):        

    """
    Utility function to set up brightway2 to work correctly with lcopt using the FORWAST database instead of ecoinvent

    By default it'll try and download the forwast database as a .bw2package file from lca-net

    If you've downloaded the forwast .bw2package file already you can set use_autodownload=False and forwast_path to point to the downloaded file

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

    if use_autodownload:
        

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


    return True


def forwast_autodownload(FORWAST_URL):      

    """
    Autodownloader for forwast database package for brightway. Used by `lcopt_bw2_forwast_setup` to get the database data. Not designed to be used on its own
    """
    dirpath = tempfile.mkdtemp()
    r = requests.get(FORWAST_URL)
    z = zipfile.ZipFile(io.BytesIO(r.content))
    z.extractall(dirpath)
    return os.path.join(dirpath, 'forwast.bw2package')


def lcopt_bw2_forwast_setup(use_autodownload=True, forwast_path=None, db_name=FORWAST_PROJECT_NAME, overwrite=False):        

    """
    Utility function to set up brightway2 to work correctly with lcopt using the FORWAST database instead of ecoinvent

    By default it'll try and download the forwast database as a .bw2package file from lca-net

    If you've downloaded the forwast .bw2package file already you can set use_autodownload=False and forwast_path to point to the downloaded file

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

    if use_autodownload:
        

        forwast_filepath = forwast_autodownload(FORWAST_URL)