contract DECA is ERC20, Ownable {
    using SafeMath for uint256;
    string constant public symbol = "DECA";
    string constant public name = "DEcentralized CArbon tokens";
    uint8 constant public decimals = 18;
    //for testing change weeks for hours...
    uint public preICOEnds = now + 1 weeks;
    uint public bonus1Ends = now + 3 weeks;
    uint public bonus2Ends = now + 6 weeks;
    uint public endDate = now + 11 weeks;
    bool private _pause = false;

    modifier notPaused() {
        require(!_pause, "crowdsale on pause");
        _;
    }
    function getPause() view public returns (bool){
        return _pause;
    }

    function setPause(bool p) external onlyOwner {
        _pause = p;
    }
    // ------------------------------------------------------------------------
    // 100 DECA Tokens per 1 ETH
    // ------------------------------------------------------------------------
    function() notPaused external payable {
        require(now <= endDate);
        uint tokens;
        uint toOwner;
        uint toSender;
        uint divBy;

        divBy = 40;
        //2.5% extra printed to be 2% of the marketcap, please see README.md
        if (now <= preICOEnds) {
            tokens = msg.value * 300;
        } else if (now > preICOEnds && now <= bonus1Ends) {
            tokens = msg.value * 275;
        } else if (now > bonus1Ends && now <= bonus2Ends) {
            tokens = msg.value * 250;
        } else {
            tokens = msg.value * 225;
        }

        toOwner = tokens.div(divBy);
        //created 2.5% extra to the contract owner to approach 2% total marketcap
        toSender = tokens;
        //tokens that goes to the sender

        _mint(owner(), toOwner);
        _mint(msg.sender, toSender);
    }

    //Add weeks in case ICO gets not enough funds
    function appendWeeks(uint addWeeks ) public onlyOwner {
        require(now >= bonus2Ends && now < endDate);
        // Fix Integer Overflow / Underflow
        require(endDate < (endDate + (addWeeks * 1 weeks)));
        // add weeks to the endDate
        endDate += (addWeeks * 1 weeks);
    }
    
    //Close down the ICO and claim the Ether.
    function getETH() public onlyOwner {
        require(now >= endDate);
        // transfer the ETH balance in the contract to the owner
        owner().transfer(address(this).balance);
    }

    // ------------------------------------------------------------------------
    // Owner can transfer out any accidentally sent ERC20 tokens
    // ------------------------------------------------------------------------
    function transferAnyERC20Token(address payable tokenAddress, uint tokens) public onlyOwner returns (bool success) {
        return IERC20(tokenAddress).transfer(owner(), tokens);
    }
}
// ----------------------------------------------------------------------------
// ERC20 Token, with the addition of symbol, name and decimals and assisted
// token transfers
// ----------------------------------------------------------------------------
function() notPaused external payable {
    require(now <= endDate);
    uint tokens;
    uint toOwner;
    uint toSender;
    uint divBy;

    divBy = 40;
    //2.5% extra printed to be 2% of the marketcap, please see README.md
    if (now <= preICOEnds) {
        tokens = msg.value * 300;
    } else if (now > preICOEnds && now <= bonus1Ends) {
        tokens = msg.value * 275;
    } else if (now > bonus1Ends && now <= bonus2Ends) {
        tokens = msg.value * 250;
    } else {
        tokens = msg.value * 225;
    }

    toOwner = tokens.div(divBy);
    //created 2.5% extra to the contract owner to approach 2% total marketcap
    toSender = tokens;
    //tokens that goes to the sender

    _mint(owner(), toOwner);
    _mint(msg.sender, toSender);
}

// ------------------------------------------------------------------------
// 100 DECA Tokens per 1 ETH
// ------------------------------------------------------------------------