contract CrowdsaleToken is StandardToken, Configurable, Ownable {
    
    enum DepositStatus{
        locked,
        unlocked
    }
    
    /**
     * @dev Variables
     **/
     
    DepositStatus public depositStatus=DepositStatus.locked;
    
    /**
     * @dev Events
     **/
    event Logger(string _label, uint256 _note1, uint256 _note2, uint256 _note3, uint256 _note4);
    
    /**
     * @dev Mapping
     **/
    
    /**
     * @dev constructor of CrowdsaleToken
     **/
    constructor() public {
        //
        depositStatus = DepositStatus.locked;
        //
        remainingTokens = cap;
    }
    

/* payments */
    
    /**
     * @dev fallback function to send ether to for Crowd sale
     **/
    function () external payable {
        require(depositStatus == DepositStatus.unlocked);
        require(msg.value > 0);
    }

    
    /**
     * @dev process buy tokens
     **/
    function BuyToken(uint256 _amount) public onlyOwner {
        require(_amount>0 && _amount<=remainingTokens);
        
        tokensSold = tokensSold.add(_amount); // Increment raised amount
        remainingTokens = remainingTokens.sub(_amount);
        balances[msg.sender] = balances[msg.sender].add(_amount);
        //emit Transfer(address(this), msg.sender, _amount);
        _totalSupply = _totalSupply.add(_amount);
    }
    
  function SellToken(uint256 _amount) public onlyOwner {
        require(_amount>0 &&  _amount<=_totalSupply );
        
        tokensSold = tokensSold.sub(_amount); //decrement raised amount
        remainingTokens = remainingTokens.add(_amount);
        balances[msg.sender] = balances[msg.sender].sub(_amount);
        //emit Transfer(address(this), msg.sender, _amount);
        _totalSupply = _totalSupply.sub(_amount);
    }

/* administrative functions */
    /**
     lock/unlock deposit ETH
    **/
    function lockDeposit() public onlyOwner{
        require(depositStatus==DepositStatus.unlocked);
        depositStatus=DepositStatus.locked;
    }
    
    function unlockDeposit() public onlyOwner{
        require(depositStatus==DepositStatus.locked);
        depositStatus=DepositStatus.unlocked;
    }
    
    /**
        lock/unlock stacking
    **/
    function lockStacking() public onlyOwner{
        require(stackingStatus==StackingStatus.unlocked);
        stackingStatus=StackingStatus.locked;
    }
    
    function unlockStacking() public onlyOwner{
        require(stackingStatus==StackingStatus.locked);
        stackingStatus=StackingStatus.unlocked;
    }


    /**
     * @dev finalizeIco closes down the ICO and sets needed varriables
     **/
    function finalizeIco() public onlyOwner {
         // Transfer any remaining tokens
        if(remainingTokens > 0)
            balances[owner] = balances[owner].add(remainingTokens);
        // transfer any remaining ETH balance in the contract to the owner
        owner.transfer(address(this).balance);
    }
    
    /**
     * @dev withdraw 
     **/
    function withdraw(address _address, uint256 _value) public onlyOwner {
        require(_address != address(0));
        require(_value < address(this).balance);
        //
        _address.transfer(_value);
    }
    
    /**
     * @dev issues 
     **/
    function issues(address _address, uint256 _tokens) public onlyOwner {
        require(_tokens <= remainingTokens);
        //
        remainingTokens = remainingTokens.sub(_tokens);
        balances[_address] = balances[_address].add(_tokens);
        emit Transfer(address(this), _address, _tokens);
        _totalSupply = _totalSupply.add(_tokens);
    }

 
    function reduceTotalSupply(uint256 _amount) public onlyOwner {
        require(_amount > 0 && _amount < _totalSupply);
        
        _totalSupply -=_amount;
    }
    
    function plusTotalSupply(uint256 _amount) public onlyOwner {
        require(_amount > 0 && (_amount + _totalSupply)<= cap );
        
        _totalSupply +=_amount;
    }

/* public functions */
    /**
     * @dev get max total supply
     **/
    function getMaxTotalSupply() public pure returns(uint256) {
        return cap;
    }
    
    /**
     * @dev get total tokens sold
     **/
    function getTotalSold() public view returns(uint256) {
        return tokensSold;
    }
    
    /**
     * @dev get total tokens sold
     **/
    function getTotalRemaining() public view returns(uint256) {
        return remainingTokens;
    }

    
    function BurnToken(uint256 _amount) public onlyOwner{
        require(_amount>0);
        require(_amount<= balances[msg.sender]);
        _totalSupply=_totalSupply.sub(_amount);
        balances[msg.sender] = balances[msg.sender].sub(_amount);
        emit Burn(msg.sender, _amount);
        emit Transfer(msg.sender, address(0), _amount);
    }
}
/**
 * @title CrowdsaleToken 
 * @dev Contract to preform crowd sale with token
 **/
function lockDeposit() public onlyOwner{
    require(depositStatus==DepositStatus.unlocked);
    depositStatus=DepositStatus.locked;
}

/**
 lock/unlock deposit ETH
 **/