contract LexTokenFactoryMaker {
    uint256 public factoryFee;
    address payable public _lexDAO; 
    bool public certified; // lexDAO certification status
    
    LexTokenFactory private factory;
    address[] public factories; 
    
    event CertificationUpdated(bool indexed updatedCertification);
    event FactoryFeeUpdated(uint256 indexed updatedFactoryFee);
    event LexDAOPaid(string indexed details, uint256 indexed payment);
    event LexDAOUpdated(address indexed updatedLexDAO);
    event LexTokenFactoryDeployed(address indexed deployer, address indexed factory, bool indexed _gated);
    
    constructor (address payable lexDAO) public 
	{
        _lexDAO = lexDAO;
	}
    
    function newLexTokenFactory(
        string memory _stamp,
        uint256 _factoryFee,
        address _deployer, 
        bool _gated) payable public {
        require(msg.value == factoryFee);
		require(_lexDAO != address(0));
       
        factory = new LexTokenFactory(
            _stamp, 
            _factoryFee, 
            _deployer, 
            _lexDAO,
            certified,
            _gated);
        
        factories.push(address(factory));
        emit LexTokenFactoryDeployed(_deployer, address(factory), _gated);
    }
    
    function getFactoryCount() public view returns (uint256 factoryCount) {
        return factories.length;
    }
    
    /***************
    LEXDAO FUNCTIONS
    ***************/
    function payLexDAO(string memory details) payable public { 
        _lexDAO.transfer(msg.value);
        emit LexDAOPaid(details, msg.value);
    }
    
    function updateCertification(bool updatedCertification) public {
        require(msg.sender == _lexDAO);
        certified = updatedCertification;
        emit CertificationUpdated(updatedCertification);
    }
    
    function updateFactoryFee(uint256 updatedFactoryFee) public {
        require(msg.sender == _lexDAO);
        factoryFee = updatedFactoryFee;
        emit FactoryFeeUpdated(updatedFactoryFee);
    }
    
    function updateLexDAO(address payable updatedLexDAO) public {
        require(msg.sender == _lexDAO);
        _lexDAO = updatedLexDAO;
        emit LexDAOUpdated(updatedLexDAO);
    }
}
/**
 * @dev Master factory pattern to clone new token factory contracts with lexDAO governance.
 */
function payLexDAO(string memory details) payable public { 
    _lexDAO.transfer(msg.value);
    emit LexDAOPaid(details, msg.value);
}

/***************
 LEXDAO FUNCTIONS
 ***************/