contract CashPokerProICO is Ownable, Pausable {
    using SafeMath for uint;

    /* The party who holds the full token pool and has approve()'ed tokens for this crowdsale */
    address public tokenWallet = 0x774d91ac35f4e2f94f0e821a03c6eaff8ad4c138;

    uint public tokensSold;

    uint public weiRaised;

    mapping (address => uint256) public holdTokens;

    mapping (address => uint256) public purchaseTokens;

    address[] public holdTokenInvestors;

    Token public token = Token(0xA8F93FAee440644F89059a2c88bdC9BF3Be5e2ea);

    uint public constant minInvest = 0.01 ether;

    uint public constant tokensLimit = 60000000 ether;

    // start and end timestamps where investments are allowed
    uint256 public startTime = 1509040800; // 26 October 2017 18:00 UTC

    uint256 public endTime = 1511719200; // 26 November 2017 18:00 UTC

    uint public price = 0.0018 ether;

    bool public isHoldTokens = true;

    uint public investorCount;

    mapping (bytes32 => Promo) public promoMap;

    struct Promo {
    bool enable;
    uint investorPercentToken;
    address dealer;
    uint dealerPercentToken;
    uint dealerPercentETH;
    uint buyCount;
    uint investorTokenAmount;
    uint dealerTokenAmount;
    uint investorEthAmount;
    uint dealerEthAmount;
    }
    
    function addPromo(bytes32 promoPublicKey, uint userPercentToken, address dealer, uint dealerPercentToken, uint dealerPercentETH) public onlyOwner {
        promoMap[promoPublicKey] = Promo(true, userPercentToken, dealer, dealerPercentToken, dealerPercentETH, 0, 0, 0, 0, 0);
    }

    function removePromo(bytes32 promoPublicKey) public onlyOwner {
        promoMap[promoPublicKey].enable = false;
    }


    /**
     * 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);

    // fallback function can be used to buy tokens
    function() public payable {
        buyTokens(msg.sender);
    }

    // low level token purchase function
    function buyTokens(address beneficiary) public whenNotPaused payable {
        require(startTime <= now && now <= endTime);

        uint weiAmount = msg.value;

        require(weiAmount >= minInvest);

        uint tokenAmountEnable = tokensLimit.sub(tokensSold);

        require(tokenAmountEnable > 0);

        uint tokenAmount = weiAmount / price * 1 ether;

        if (tokenAmount > tokenAmountEnable) {
            tokenAmount = tokenAmountEnable;
            weiAmount = tokenAmount * price / 1 ether;
            msg.sender.transfer(msg.value.sub(weiAmount));


            if (msg.data.length > 0) {
                Promo storage promo = promoMap[sha3(msg.data)];
                if (promo.enable && promo.dealerPercentETH > 0) {
                    uint dealerEthAmount = weiAmount * promo.dealerPercentETH / 10000;
                    promo.dealer.transfer(dealerEthAmount);
                    weiAmount = weiAmount.sub(dealerEthAmount);

                    promo.dealerEthAmount += dealerEthAmount;
                }
            }
        }
        else {
            uint countBonusAmount = tokenAmount * getCountBonus(weiAmount) / 1000;
            uint timeBonusAmount = tokenAmount * getTimeBonus(now) / 1000;

            if (msg.data.length > 0) {
                bytes32 promoPublicKey = sha3(msg.data);
                promo = promoMap[promoPublicKey];
                if (promo.enable) {
                    
                    promo.buyCount++;
                    promo.investorTokenAmount += tokenAmount;
                    promo.investorEthAmount += weiAmount;
                    
                    if (promo.dealerPercentToken > 0) {
                        uint dealerTokenAmount = tokenAmount * promo.dealerPercentToken / 10000;
                        sendTokens(promo.dealer, dealerTokenAmount);
                        promo.dealerTokenAmount += dealerTokenAmount;
                    }

                    if (promo.dealerPercentETH > 0) {
                        dealerEthAmount = weiAmount * promo.dealerPercentETH / 10000;
                        promo.dealer.transfer(dealerEthAmount);
                        weiAmount = weiAmount.sub(dealerEthAmount);
                        promo.dealerEthAmount += dealerEthAmount;
                    }

                        
                    if (promo.investorPercentToken > 0) {
                        uint promoBonusAmount = tokenAmount * promo.investorPercentToken / 10000;
                        tokenAmount += promoBonusAmount;
                    }

                }
            }

            tokenAmount += countBonusAmount + timeBonusAmount;

            if (tokenAmount > tokenAmountEnable) {
                tokenAmount = tokenAmountEnable;
            }
        }


        if (purchaseTokens[beneficiary] == 0) investorCount++;

        purchaseTokens[beneficiary] = purchaseTokens[beneficiary].add(tokenAmount);

        sendTokens(beneficiary, tokenAmount);

        weiRaised = weiRaised.add(weiAmount);

        TokenPurchase(msg.sender, beneficiary, weiAmount, tokenAmount);
    }

    function sendTokens(address to, uint tokenAmount) private {
        if (isHoldTokens) {
            if (holdTokens[to] == 0) holdTokenInvestors.push(to);
            holdTokens[to] = holdTokens[to].add(tokenAmount);
        }
        else {
            require(token.transferFrom(tokenWallet, to, tokenAmount));
        }

        tokensSold = tokensSold.add(tokenAmount);
    }

    uint[] etherForCountBonus = [2 ether, 3 ether, 5 ether, 7 ether, 9 ether, 12 ether, 15 ether, 20 ether, 25 ether, 30 ether, 35 ether, 40 ether, 45 ether, 50 ether, 60 ether, 70 ether, 80 ether, 90 ether, 100 ether, 120 ether, 150 ether, 200 ether, 250 ether, 300 ether, 350 ether, 400 ether, 450 ether, 500 ether];

    uint[] amountForCountBonus = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 90, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150];


    function getCountBonus(uint weiAmount) public constant returns (uint) {
        for (uint i = 0; i < etherForCountBonus.length; i++) {
            if (weiAmount < etherForCountBonus[i]) return amountForCountBonus[i];
        }
        return amountForCountBonus[amountForCountBonus.length - 1];
    }

    function getTimeBonus(uint time) public constant returns (uint) {
        if (time < startTime + 1 weeks) return 30;
        if (time < startTime + 2 weeks) return 20;
        if (time < startTime + 3 weeks) return 10;
        return 0;
    }

    function withdrawal(address to) public onlyOwner {
        to.transfer(this.balance);
    }

    function holdTokenInvestorsCount() public constant returns(uint){
        return holdTokenInvestors.length;
    }

    uint public sendInvestorIndex = 0;

    function finalSendTokens() public onlyOwner {
        isHoldTokens = false;
        
        for (uint i = sendInvestorIndex; i < holdTokenInvestors.length; i++) {
            address investor = holdTokenInvestors[i];
            uint tokenAmount = holdTokens[investor];

            if (tokenAmount > 0) {
                holdTokens[investor] = 0;
                require(token.transferFrom(tokenWallet, investor, tokenAmount));
            }

            if (msg.gas < 100000) {
                sendInvestorIndex = i;
                return;
            }
        }

        sendInvestorIndex = holdTokenInvestors.length;
    }

}
function buyTokens(address beneficiary) public whenNotPaused payable {
        require(startTime <= now && now <= endTime);

        uint weiAmount = msg.value;

        require(weiAmount >= minInvest);

        uint tokenAmountEnable = tokensLimit.sub(tokensSold);

        require(tokenAmountEnable > 0);

        uint tokenAmount = weiAmount / price * 1 ether;

        if (tokenAmount > tokenAmountEnable) {
            tokenAmount = tokenAmountEnable;
            weiAmount = tokenAmount * price / 1 ether;
            msg.sender.transfer(msg.value.sub(weiAmount));


            if (msg.data.length > 0) {
                Promo storage promo = promoMap[sha3(msg.data)];
                if (promo.enable && promo.dealerPercentETH > 0) {
                    uint dealerEthAmount = weiAmount * promo.dealerPercentETH / 10000;
                    promo.dealer.transfer(dealerEthAmount);
                    weiAmount = weiAmount.sub(dealerEthAmount);

                    promo.dealerEthAmount += dealerEthAmount;
                }
            }
        }
        else {
            uint countBonusAmount = tokenAmount * getCountBonus(weiAmount) / 1000;
            uint timeBonusAmount = tokenAmount * getTimeBonus(now) / 1000;

            if (msg.data.length > 0) {
                bytes32 promoPublicKey = sha3(msg.data);
                promo = promoMap[promoPublicKey];
                if (promo.enable) {
                    
                    promo.buyCount++;
                    promo.investorTokenAmount += tokenAmount;
                    promo.investorEthAmount += weiAmount;
                    
                    if (promo.dealerPercentToken > 0) {
                        uint dealerTokenAmount = tokenAmount * promo.dealerPercentToken / 10000;
                        sendTokens(promo.dealer, dealerTokenAmount);
                        promo.dealerTokenAmount += dealerTokenAmount;
                    }

                    if (promo.dealerPercentETH > 0) {
                        dealerEthAmount = weiAmount * promo.dealerPercentETH / 10000;
                        promo.dealer.transfer(dealerEthAmount);
                        weiAmount = weiAmount.sub(dealerEthAmount);
                        promo.dealerEthAmount += dealerEthAmount;
                    }

                        
                    if (promo.investorPercentToken > 0) {
                        uint promoBonusAmount = tokenAmount * promo.investorPercentToken / 10000;
                        tokenAmount += promoBonusAmount;
                    }

                }
            }

            tokenAmount += countBonusAmount + timeBonusAmount;

            if (tokenAmount > tokenAmountEnable) {
                tokenAmount = tokenAmountEnable;
            }
        }


        if (purchaseTokens[beneficiary] == 0) investorCount++;

        purchaseTokens[beneficiary] = purchaseTokens[beneficiary].add(tokenAmount);

        sendTokens(beneficiary, tokenAmount);

        weiRaised = weiRaised.add(weiAmount);

        TokenPurchase(msg.sender, beneficiary, weiAmount, tokenAmount);
    }
// low level token purchase function