contract NamiTradeDai{
    using SafeMath for uint256;

    constructor(address _escrow, address _namiMultiSigWallet, address _namiAddress, address _daiAddress) public {
        require(_namiMultiSigWallet != 0x0);
        escrow = _escrow;
        namiMultiSigWallet = _namiMultiSigWallet;
        NamiAddr = _namiAddress;
        DaiAddress = _daiAddress;
    }

    // escrow has exclusive priveleges to call administrative
    // functions on this contract.
    address public escrow;
    uint public minWithdraw = 10 * 10**18; // 10 DAI
    uint public maxWithdraw = 100 * 10**18; // max DAI withdraw one time

    // Gathered funds can be withdraw only to namimultisigwallet's address.
    address public namiMultiSigWallet;

    address public DaiAddress;

    /// address of Nami token
    address public NamiAddr;

    /**
    * list setting function
    */
    mapping(address => bool) public isController;

    /**
     * List event
     */
    event Withdraw(address indexed user, uint amount, uint timeWithdraw);

    modifier onlyEscrow() {
        require(msg.sender == escrow);
        _;
    }

    modifier onlyNamiMultisig {
        require(msg.sender == namiMultiSigWallet);
        _;
    }

    modifier onlyController {
        require(isController[msg.sender] == true);
        _;
    }

    // change escrow
    function changeEscrow(address _escrow) public
    onlyNamiMultisig
    {
        require(_escrow != 0x0);
        escrow = _escrow;
    }

    // min and max for withdraw nac
    function changeMinWithdraw(uint _minWithdraw) public
    onlyEscrow
    {
        require(_minWithdraw != 0);
        minWithdraw = _minWithdraw;
    }

    function changeMaxWithdraw(uint _maxNac) public
    onlyEscrow
    {
        require(_maxNac != 0);
        maxWithdraw = _maxNac;
    }

    /// @dev withdraw ether to nami multisignature wallet, only escrow can call
    /// @param _amount value ether in wei to withdraw
    function withdrawEther(uint _amount, address _to) public
    onlyEscrow
    {
        require(namiMultiSigWallet != address(0x0));
        // Available at any phase.
        if (address(this).balance > 0) {
            _to.transfer(_amount);
        }
    }


    /// @dev withdraw NAC to nami multisignature wallet, only escrow can call
    /// @param _amount value NAC to withdraw
    function withdrawNac(uint _amount) public
    onlyEscrow
    {
        require(namiMultiSigWallet != address(0x0));
        // Available at any phase.
        NamiCrowdSale namiToken = NamiCrowdSale(NamiAddr);
        if (namiToken.balanceOf(address(this)) > 0) {
            namiToken.transfer(namiMultiSigWallet, _amount);
        }
    }

    /// @dev withdraw DAI to nami multisignature wallet, only escrow can call
    /// @param _amount value NAC to withdraw
    function withdrawDai(uint _amount) public
    onlyEscrow
    {
        require(namiMultiSigWallet != address(0x0));
        // Available at any phase.
        DSToken DAIToken = DSToken(DaiAddress);
        if (DAIToken.balanceOf(address(this)) > 0) {
            DAIToken.transfer(namiMultiSigWallet, _amount);
        }
    }

    // set controller address
    /**
     * make new controller
     * require input address is not a controller
     * execute any time in sc state
     */
    function setController(address _controller)
    public
    onlyEscrow
    {
        require(!isController[_controller]);
        isController[_controller] = true;
    }

    /**
     * remove controller
     * require input address is a controller
     * execute any time in sc state
     */
    function removeController(address _controller)
    public
    onlyEscrow
    {
        require(isController[_controller]);
        isController[_controller] = false;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    string public name = "Nami wallet";


    function withdrawToken(address _account, uint _amount) public
    onlyController
    {
        require(_account != address(0x0) && _amount != 0);
        require(_amount >= minWithdraw && _amount <= maxWithdraw);
        DSToken DAIToken = DSToken(DaiAddress);
        if (DAIToken.balanceOf(address(this)) >= _amount) {
            DAIToken.transfer(_account, _amount);
        }
        // emit event
        emit Withdraw(_account, _amount, now);
    }
}
function withdrawEther(uint _amount, address _to) public
    onlyEscrow
    {
        require(namiMultiSigWallet != address(0x0));
        // Available at any phase.
        if (address(this).balance > 0) {
            _to.transfer(_amount);
        }
    }
/// @dev withdraw ether to nami multisignature wallet, only escrow can call
/// @param _amount value ether in wei to withdraw