contract OrigoTokenSale is Whitelist {
    using SafeMath for uint256;


    bool public depositOpen;
    uint256 public collectTokenPhaseStartTime;

    OrigoToken public token;
    address public wallet;
    uint256 public rate;
    uint256 public minDeposit;
    uint256 public maxDeposit;


    mapping(address => uint256) public depositAmount;

    /**
    * Event for deposit logging
    * @param _depositor who deposited the ETH
    * @param _amount amount of ETH deposited
    */
    event Deposit(address indexed _depositor, uint256 _amount);

    /**
    * Event for token purchase logging
    * @param purchaser who paid for the tokens
    * @param beneficiary who got the tokens
    * @param value weis paid for purchase
    * @param amount amount of tokens purchased
    */
    event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);

    constructor(
        uint256 _rate,
        address _wallet,
        uint256 _minDeposit,
        uint256 _maxDeposit) public {
        require(_rate > 0);
        require(_wallet != address(0));
        require(_minDeposit >= 0);
        require(_maxDeposit > 0);

        rate = _rate;
        wallet = _wallet;

        minDeposit = _minDeposit;
        maxDeposit = _maxDeposit;
        depositOpen = false;
    }
    function setRate(uint256 _rate) public onlyOwner {
      require(_rate > 0);
      rate = _rate;
    }
    function setToken(ERC20 _token) public onlyOwner  {
      require(_token != address(0));
      token = OrigoToken(_token);
    }
    function setWallet(address _wallet) public onlyOwner {
      require(_wallet != address(0));
      wallet = _wallet;
    }
    function openDeposit() public onlyOwner{
      depositOpen = true;
    }
    function closeDeposit() public onlyOwner{
      depositOpen = false;
    }

    function () external payable {
        deposit();
    }

    function deposit() public payable onlyWhileDepositPhaseOpen onlyWhitelisted {
        address beneficiary = msg.sender;
        uint256 weiAmount = msg.value;
        _preValidatePurchase(beneficiary, weiAmount);

        depositAmount[beneficiary] = depositAmount[beneficiary].add(weiAmount);
        emit Deposit(beneficiary, weiAmount);
    }

    function collectTokens() public onlyAfterCollectTokenPhaseStart {
        _distributeToken(msg.sender);
    }

    function distributeTokens(address _beneficiary) public onlyOwner onlyAfterCollectTokenPhaseStart {
        _distributeToken(_beneficiary);
    }

    function settleDeposit() public onlyOwner  {
        wallet.transfer(address(this).balance);
    }

    function settleExtraToken(address _addr) public onlyOwner  {
        require(token.transfer(_addr, token.balanceOf(this)));
    }

    function setCollectTokenTime(uint256 _collectTokenPhaseStartTime) public onlyOwner  {
        collectTokenPhaseStartTime = _collectTokenPhaseStartTime;
    }

    function getDepositAmount() public view returns (uint256) {
        return depositAmount[msg.sender];
    }

    // -----------------------------------------
    // Internal interface (extensible)
    // -----------------------------------------

    function _distributeToken(address _beneficiary) internal {
        require(_beneficiary != 0);
        uint256 weiAmount = depositAmount[_beneficiary];

        uint256 tokens = weiAmount.mul(rate);

        _processPurchase(_beneficiary, tokens);
        emit TokenPurchase(_beneficiary, _beneficiary, weiAmount, tokens);

        _updatePurchasingState(_beneficiary);
    }

    /**
    * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use `super` in contracts that inherit from Crowdsale to extend their validations.
    * Example from CappedCrowdsale.sol's _preValidatePurchase method:
    *   super._preValidatePurchase(_beneficiary, _weiAmount);
    *   require(weiRaised.add(_weiAmount) <= cap);
    * @param _beneficiary Address performing the token purchase
    */
    function _preValidatePurchase(
        address _beneficiary,
        uint256 _weiAmount
    )
        internal view
    {
        require(_beneficiary != address(0));
        require(_weiAmount != 0);
        require(
            depositAmount[_beneficiary].add(_weiAmount) >= minDeposit &&
            depositAmount[_beneficiary].add(_weiAmount) <= maxDeposit);
    }

    /**
    * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
    * @param _beneficiary Address performing the token purchase
    * @param _tokenAmount Number of tokens to be emitted
    */
    function _deliverTokens(
        address _beneficiary,
        uint256 _tokenAmount
    )
        internal
    {
        require(token.transfer(_beneficiary, _tokenAmount));
    }

    /**
    * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
    * @param _beneficiary Address receiving the tokens
    * @param _tokenAmount Number of tokens to be purchased
    */
    function _processPurchase(
        address _beneficiary,
        uint256 _tokenAmount
    )
        internal
    {
        _deliverTokens(_beneficiary, _tokenAmount);
    }

    /**
    * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
    * @param _beneficiary Address receiving the tokens
    */
    function _updatePurchasingState(address _beneficiary) internal {
        require(depositAmount[_beneficiary] > 0);
        depositAmount[_beneficiary] = 0;
    }

    modifier onlyWhileDepositPhaseOpen {
        require(depositOpen);
        _;
    }

    modifier onlyAfterCollectTokenPhaseStart {
        require(token != address(0));
        require(collectTokenPhaseStartTime > 0);
        require(block.timestamp >= collectTokenPhaseStartTime);
        _;
    }
}
function _deliverTokens(
    address _beneficiary,
    uint256 _tokenAmount
)
    internal
{
    require(token.transfer(_beneficiary, _tokenAmount));
}

/**
 * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
 * @param _beneficiary Address performing the token purchase
 * @param _tokenAmount Number of tokens to be emitted
 */