import json
import os

from web3 import Web3

from ethpm.tools.builder import *

# PROVIDER = os.environ.get('PROVIDER', )

w3 = Web3(Web3.EthereumTesterProvider())

def load_contracts(path):
    contracts = {}
    directory = os.fsencode(path)

    for file in os.listdir(directory):
        filename = os.fsdecode(file)
        if filename.endswith(".json"): 
            filepath = os.path.join(path, filename)
            with open(filepath) as f:
                contract = json.load(f)
                name = contract.get('contractName')
                contracts[name] = contract

    return contracts

def prepare_contract_packages(contracts):
    packages = {}
    for name, compiler_output  in contracts.items():
        package = build(
            init_manifest(name, '0.1.0'),
            contract_type(name, compiler_output),
            as_package(w3),
        )

        print(package)
        packages[name] = Package

    return packages

def deploy_tcr_libs():
    addresses = {}
    # Deploy DLL

    # Deploy attribute store
    
    return addresses

def deploy_parametrizer_factory():
    # Link DLL
    # Link Attribute Store
    pass

def deploy_plcr_factory():
    # Link DLL
    # Link Attribute Store
    pass

def deploy_tcr_factory():
    # Link DLL
    # Link Attribute Store
    pass