contract PoolEscrow {

    using SafeERC20 for IERC20;
    using SafeMath for uint256;

    modifier onlyGov() {
        require(msg.sender == governance, "only governance");
        _;
    }

    address public shareToken;
    address public pool;
    address public ausc;
    address public secondary;
    address public development;
    address public governancePool;
    address public dao;
    address public governance;
    uint256 public lastMint;

    constructor(address _shareToken,
        address _pool,
        address _ausc,
        address _secondary,
        address _development,
        address _governancePool,
        address _dao) public {
        shareToken = _shareToken;
        pool = _pool;
        ausc = _ausc;
        secondary = _secondary;
        development = _development;
        governancePool = _governancePool;
        dao = _dao;
        governance = msg.sender;
        lastMint = 0;
    }

    function setSecondary(address account) external onlyGov {
        secondary = account;
    }

    function setDevelopment(address account) external onlyGov {
        development = account;
    }

    function setGovernancePool(address account) external onlyGov {
        governancePool = account;
    }

    function setDao(address account) external onlyGov {
        dao = account;
    }

    function setGovernance(address account) external onlyGov {
        governance = account;
    }

    function startSecondaryDistribution() external onlyGov {
        lastMint = 1;
    }

    function release(address recipient, uint256 shareAmount) external {
        require(msg.sender == pool, "only pool can release tokens");
        IERC20(shareToken).safeTransferFrom(msg.sender, address(this), shareAmount);
        uint256 endowment = getTokenNumber(shareAmount).mul(5).div(100);
        uint256 reward = getTokenNumber(shareAmount);
        if (secondary != address(0)) {
            IERC20(ausc).safeApprove(secondary, 0);
            IERC20(ausc).safeApprove(secondary, endowment);
            PoolEscrow(secondary).notifySecondaryTokens(endowment);
            reward = reward.sub(endowment);
        }
        if (development != address(0)) {
            IERC20(ausc).safeTransfer(development, endowment);
            reward = reward.sub(endowment);
        }
        if (governancePool != address(0)) {
            IERC20(ausc).safeTransfer(governancePool, endowment);
            reward = reward.sub(endowment);
        }
        if (dao != address(0)) {
            IERC20(ausc).safeTransfer(dao, endowment);
            reward = reward.sub(endowment);
        }
        IERC20(ausc).safeTransfer(recipient, reward);
        IERC20Burnable(shareToken).burn(shareAmount);
    }

    function getTokenNumber(uint256 shareAmount) public view returns(uint256) {
        return IERC20(ausc).balanceOf(address(this))
            .mul(shareAmount)
            .div(IERC20(shareToken).totalSupply());
    }

    /**
    * Functionality for secondary pool escrow. Transfers AUSC tokens from msg.sender to this
    * escrow. At most per day, mints a fixed number of escrow tokens to the pool, and notifies
    * the pool. The period 1 day should match the secondary pool.
    */
    function notifySecondaryTokens(uint256 number) external {
        IERC20(ausc).safeTransferFrom(msg.sender, address(this), number);
        if (lastMint.add(1 days) < block.timestamp && lastMint != 0) {
            uint256 dailyMint = 1000 * 1e18;
            IERC20Mintable(shareToken).mint(pool, dailyMint);
            AuricRewards(pool).notifyRewardAmount(dailyMint);
            lastMint = block.timestamp;
        }
    }
}
function notifySecondaryTokens(uint256 number) external {
    IERC20(ausc).safeTransferFrom(msg.sender, address(this), number);
    if (lastMint.add(1 days) < block.timestamp && lastMint != 0) {
        uint256 dailyMint = 1000 * 1e18;
        IERC20Mintable(shareToken).mint(pool, dailyMint);
        AuricRewards(pool).notifyRewardAmount(dailyMint);
        lastMint = block.timestamp;
    }
}

/**
 * Functionality for secondary pool escrow. Transfers AUSC tokens from msg.sender to this
 * escrow. At most per day, mints a fixed number of escrow tokens to the pool, and notifies
 * the pool. The period 1 day should match the secondary pool.
 */