contract CBS is StandardToken, CanReclaimToken {
    using SafeMath for uint;

    event BuyToken(address indexed from, uint256 value);
    event SellToken(address indexed from, uint256 value, uint256 sellEth);
    event TransferContractEth(address indexed to, uint256 value);


    string public symbol;
    string public name;
    string public version = "2.0";

    uint8 public decimals;
    uint256 INITIAL_SUPPLY;
    uint256 tokens;

    uint256 public buyPrice;
    uint256 public sellPrice;
    uint256 public contractEth;
    bool public allowBuy;
    bool public allowSell;

    // constructor
    function CBS(
        string _symbol,
        string _name,
        uint8 _decimals, 
        uint256 _INITIAL_SUPPLY,
        uint256 _buyPrice,
        uint256 _sellPrice,
        bool _allowBuy,
        bool _allowSell
    ) public {
        symbol = _symbol;
        name = _name;
        decimals = _decimals;
        INITIAL_SUPPLY = _INITIAL_SUPPLY * 10 ** uint256(decimals);
        setBuyPrices(_buyPrice);
        setSellPrices(_sellPrice);

        totalSupply_ = INITIAL_SUPPLY;
        maxSupply_ = INITIAL_SUPPLY;
        balances[owner] = totalSupply_;
        allowBuy = _allowBuy;
        allowSell = _allowSell;
    }

    function setAllowBuy(bool _allowBuy) public onlyOwner {
        allowBuy = _allowBuy;
    }

    function setBuyPrices(uint256 _newBuyPrice) public onlyOwner {
        buyPrice = _newBuyPrice;
    }

    function setAllowSell(bool _allowSell) public onlyOwner {
        allowSell = _allowSell;
    }

    function setSellPrices(uint256 _newSellPrice) public onlyOwner {
        sellPrice = _newSellPrice;
    }

    function () public payable {
        BuyTokens(msg.value);
    }

    function BuyTokens(uint256 _value)  internal {
        tokens = _value.div(buyPrice).mul(100);
        require(allowBuy);
        require(_value > 0 && _value >= buyPrice && tokens > 0);
        require(balances[owner] >= tokens);

        super.transferByInternal(owner, msg.sender, tokens);
        contractEth = contractEth.add(_value);
        BuyToken(msg.sender, _value);
    }

    function transferEther(address _to, uint256 _value) onlyOwner public returns (bool) {
        require(_value <= contractEth);
        _to.transfer(_value);
        contractEth = contractEth.sub(_value);
        TransferContractEth(_to, _value);
        return true;
    }

    

    function sellTokens(uint256 _value) public returns (bool) {
        uint256 sellEth;
        require(allowSell);
        require(_value > 0);
        require(balances[msg.sender] >= _value);
        if (sellPrice == 0){
            sellEth = 0;
        }
        else
        {
            sellEth = _value.mul(sellPrice).div(100);
        }

        super.transferByInternal(msg.sender, owner, _value);
        SellToken(msg.sender, _value, sellEth);
        msg.sender.transfer(sellEth);
        contractEth = contractEth.sub(sellEth);
    }

}
function CBS(
    string _symbol,
    string _name,
    uint8 _decimals, 
    uint256 _INITIAL_SUPPLY,
    uint256 _buyPrice,
    uint256 _sellPrice,
    bool _allowBuy,
    bool _allowSell
) public {
    symbol = _symbol;
    name = _name;
    decimals = _decimals;
    INITIAL_SUPPLY = _INITIAL_SUPPLY * 10 ** uint256(decimals);
    setBuyPrices(_buyPrice);
    setSellPrices(_sellPrice);

    totalSupply_ = INITIAL_SUPPLY;
    maxSupply_ = INITIAL_SUPPLY;
    balances[owner] = totalSupply_;
    allowBuy = _allowBuy;
    allowSell = _allowSell;
}

// constructor