contract DataUnionFactoryMainnet {
    event MainnetDUCreated(address indexed mainnet, address indexed sidechain, address indexed owner, address template);

    address public dataUnionMainnetTemplate;

    // needed to calculate address of sidechain contract
    address public dataUnionSidechainTemplate;
    address public dataUnionSidechainFactory;
    uint256 public sidechainMaxGas;
    FactoryConfig public migrationManager;

    constructor(address _migrationManager,
                address _dataUnionMainnetTemplate,
                address _dataUnionSidechainTemplate,
                address _dataUnionSidechainFactory,
                uint256 _sidechainMaxGas)
        public
    {
        migrationManager = FactoryConfig(_migrationManager);
        dataUnionMainnetTemplate = _dataUnionMainnetTemplate;
        dataUnionSidechainTemplate = _dataUnionSidechainTemplate;
        dataUnionSidechainFactory = _dataUnionSidechainFactory;
        sidechainMaxGas = _sidechainMaxGas;
    }

    function amb() public view returns (IAMB) {
        return IAMB(ITokenMediator(migrationManager.currentMediator()).bridgeContract());
    }
 
    function token() public view returns (address) {
        return migrationManager.currentToken();
    }


    function sidechainAddress(address mainetAddress)
        public view
        returns (address)
    {
        return CloneLib.predictCloneAddressCreate2(
            dataUnionSidechainTemplate,
            dataUnionSidechainFactory,
            bytes32(uint256(mainetAddress))
        );
    }
    /*

    */
    function mainnetAddress(address deployer, string memory name)
        public view
        returns (address)
    {
        bytes32 salt = keccak256(abi.encode(bytes(name), deployer));
        return CloneLib.predictCloneAddressCreate2(
            dataUnionMainnetTemplate,
            address(this),
            salt
        );
    }


/*
    function initialize(
        address _token,
        address _tokenMediator,
        address _sidechainDataUnionFactory,
        uint256 _sidechainMaxgas,
        address _sidechainTemplateDataUnion,
        address _owner,
        uint256 adminFeeFraction,
        address[] memory agents
    )  public {
    users can only deploy with salt = their key.
*/
    function deployNewDataUnion(address owner, uint256 adminFeeFraction, address[] memory agents, string memory name)
        public
        returns (address)
    {
        bytes32 salt = keccak256(abi.encode(bytes(name), msg.sender));
        bytes memory data = abi.encodeWithSignature("initialize(address,address,uint256,address,address,uint256,address[])",
            migrationManager,
            dataUnionSidechainFactory,
            sidechainMaxGas,
            dataUnionSidechainTemplate,
            owner,
            adminFeeFraction,
            agents
        );
        address du = CloneLib.deployCodeAndInitUsingCreate2(CloneLib.cloneBytecode(dataUnionMainnetTemplate), data, salt);
        emit MainnetDUCreated(du, sidechainAddress(du), owner, dataUnionMainnetTemplate);
        return du;
    }
}
function deployNewDataUnion(address owner, uint256 adminFeeFraction, address[] memory agents, string memory name)
    public
    returns (address)
{
    bytes32 salt = keccak256(abi.encode(bytes(name), msg.sender));
    bytes memory data = abi.encodeWithSignature("initialize(address,address,uint256,address,address,uint256,address[])",
        migrationManager,
        dataUnionSidechainFactory,
        sidechainMaxGas,
        dataUnionSidechainTemplate,
        owner,
        adminFeeFraction,
        agents
    );
    address du = CloneLib.deployCodeAndInitUsingCreate2(CloneLib.cloneBytecode(dataUnionMainnetTemplate), data, salt);
    emit MainnetDUCreated(du, sidechainAddress(du), owner, dataUnionMainnetTemplate);
    return du;
}

/*
 function initialize(
 address _token,
 address _tokenMediator,
 address _sidechainDataUnionFactory,
 uint256 _sidechainMaxgas,
 address _sidechainTemplateDataUnion,
 address _owner,
 uint256 adminFeeFraction,
 address[] memory agents
 )  public {
 users can only deploy with salt = their key.
 */