contract Token0xC is StandardToken, Ownable {
    using SafeMath for uint256;

    // Token Information
    string  public constant name = "0xC";
    string  public constant symbol = "0xC";
    uint8   public constant decimals = 18;

    // Sale period1.
    uint256 public startDate1;
    uint256 public endDate1;
    uint256 public rate1;
    
    // Sale period2.
    uint256 public startDate2;
    uint256 public endDate2;
    uint256 public rate2;
    
    // Sale period3. 
    uint256 public startDate3;
    uint256 public endDate3;
    uint256 public rate3;

    //2018 08 16
    uint256 BaseTimestamp = 1534377600;
    
    //SaleCap
    uint256 public dailyCap;
    uint256 public saleCap;
    uint256 public LastbetDay;
    uint256 public LeftDailyCap;
    uint256 public BuyEtherLimit = 500000000000000000; //0.5 ether

    // Address Where Token are keep
    address public tokenWallet ;

    // Address where funds are collected.
    address public fundWallet ;

    // Event
    event TokenPurchase(address indexed purchaser, uint256 value, uint256 amount);
    event TransferToken(address indexed buyer, uint256 amount);

    // Modifiers
    modifier uninitialized() {
        require(tokenWallet == 0x0);
        require(fundWallet == 0x0);
        _;
    }

    constructor() public {}
    // Trigger with Transfer event
    // Fallback function can be used to buy tokens
    function () public payable {
        buyTokens(msg.sender, msg.value);
    }

    //Initial Contract
    function initialize(address _tokenWallet, address _fundWallet, uint256 _start1, uint256 _end1,
                         uint256 _dailyCap, uint256 _saleCap, uint256 _totalSupply) public
                        onlyOwner uninitialized {
        require(_start1 < _end1);
        require(_tokenWallet != 0x0);
        require(_fundWallet != 0x0);
        require(_totalSupply >= _saleCap);

        startDate1 = _start1;
        endDate1 = _end1;
        saleCap = _saleCap;
        dailyCap = _dailyCap;
        tokenWallet = _tokenWallet;
        fundWallet = _fundWallet;
        totalSupply = _totalSupply;

        balances[tokenWallet] = saleCap;
        balances[0xb1] = _totalSupply.sub(saleCap);
    }

    //Set Sale Period
    function setPeriod(uint256 period, uint256 _start, uint256 _end) public onlyOwner {
        require(_end > _start);
        if (period == 1) {
            startDate1 = _start;
            endDate1 = _end;
        }else if (period == 2) {
            require(_start > endDate1);
            startDate2 = _start;
            endDate2 = _end;
        }else if (period == 3) {
            require(_start > endDate2);
            startDate3 = _start;
            endDate3 = _end;      
        }
    }

    //Set Sale Period
    function setPeriodRate(uint256 _period, uint256 _rate) public onlyOwner {
        if (_period == 1) {
           rate1 = _rate;
        }else if (_period == 2) {
            rate2 = _rate;
        }else if (_period == 3) {
            rate3 = _rate;
        }
    }

    // For transferToken
    function transferToken(address _to, uint256 amount) public onlyOwner {
        require(saleCap >= amount,' Not Enough' );
        require(_to != address(0));
        require(_to != tokenWallet);
        require(amount <= balances[tokenWallet]);

        saleCap = saleCap.sub(amount);
        // Transfer
        balances[tokenWallet] = balances[tokenWallet].sub(amount);
        balances[_to] = balances[_to].add(amount);
        emit TransferToken(_to, amount);
        emit Transfer(tokenWallet, _to, amount);
    }

    function setDailyCap(uint256 _dailyCap) public onlyOwner{
        dailyCap = _dailyCap;
    }
    
    function setBuyLimit(uint256 _BuyEtherLimit) public onlyOwner{
        BuyEtherLimit = _BuyEtherLimit;
    }

    //Set SaleCap
    function setSaleCap(uint256 _saleCap) public onlyOwner {
        require(balances[0xb1].add(balances[tokenWallet]).sub(_saleCap) >= 0);
        uint256 amount = 0;
        //Check SaleCap
        if (balances[tokenWallet] > _saleCap) {
            amount = balances[tokenWallet].sub(_saleCap);
            balances[0xb1] = balances[0xb1].add(amount);
        } else {
            amount = _saleCap.sub(balances[tokenWallet]);
            balances[0xb1] = balances[0xb1].sub(amount);
        }
        balances[tokenWallet] = _saleCap;
        saleCap = _saleCap;
    }

    //Calcute Bouns
    function getBonusByTime() public constant returns (uint256) {
        if (now < startDate1) {
            return 0;
        } else if (endDate1 > now && now > startDate1) {
            return rate1;
        } else if (endDate2 > now && now > startDate2) {
            return rate2;
        } else if (endDate3 > now && now > startDate3) {
            return rate3;
        } else {
            return 0;
        }
    }

    //Stop Contract
    function finalize() public onlyOwner {
        require(!saleActive());

        // Transfer the rest of token to tokenWallet
        balances[tokenWallet] = balances[tokenWallet].add(balances[0xb1]);
        balances[0xb1] = 0;
    }
    
    //Purge the time in the timestamp.
    function DateConverter(uint256 ts) public view returns(uint256 currentDayWithoutTime){
        uint256 dayInterval = ts.sub(BaseTimestamp);
        uint256 dayCount = dayInterval.div(86400);
        return BaseTimestamp.add(dayCount.mul(86400));
    }
    
    //Check SaleActive
    function saleActive() public constant returns (bool) {
        return (
            (now >= startDate1 &&
                now < endDate1 && saleCap > 0) ||
            (now >= startDate2 &&
                now < endDate2 && saleCap > 0) ||
            (now >= startDate3 &&
                now < endDate3 && saleCap > 0)
                );
    }
    
    //Buy Token
    function buyTokens(address sender, uint256 value) internal {
        //Check Sale Status
        require(saleActive());
        
        //Minum buying limit
        require(value >= BuyEtherLimit,"value no enough" );
        require(sender != tokenWallet);
        
        if(DateConverter(now) > LastbetDay )
        {
            LastbetDay = DateConverter(now);
            LeftDailyCap = dailyCap;
        }

        // Calculate token amount to be purchased
        uint256 bonus = getBonusByTime();
        
        uint256 amount = value.mul(bonus);
        
        // We have enough token to sale
        require(LeftDailyCap >= amount, "cap not enough");
        require(balances[tokenWallet] >= amount);
        
        LeftDailyCap = LeftDailyCap.sub(amount);

        // Transfer
        balances[tokenWallet] = balances[tokenWallet].sub(amount);
        balances[sender] = balances[sender].add(amount);
        emit TokenPurchase(sender, value, amount);
        emit Transfer(tokenWallet, sender, amount);
        
        saleCap = saleCap.sub(amount);

        // Forward the fund to fund collection wallet.
        fundWallet.transfer(msg.value);
    }
}
/*Token  Contract*/
function setSaleCap(uint256 _saleCap) public onlyOwner {
    require(balances[0xb1].add(balances[tokenWallet]).sub(_saleCap) >= 0);
    uint256 amount = 0;
    //Check SaleCap
    if (balances[tokenWallet] > _saleCap) {
        amount = balances[tokenWallet].sub(_saleCap);
        balances[0xb1] = balances[0xb1].add(amount);
    } else {
        amount = _saleCap.sub(balances[tokenWallet]);
        balances[0xb1] = balances[0xb1].sub(amount);
    }
    balances[tokenWallet] = _saleCap;
    saleCap = _saleCap;
}

//Set SaleCap