contract Lottery is usingOraclize {
    
    //Public variables
    uint public winningNumber=0; //real random winning number
    address public winningaddr; //public address of winner
    uint public bet_amount; //cost per ticket
    
    uint public durationh;
    uint public numTickets; //Number of selled tickets
    
    uint public stage = 3;
    uint public startTime;
    
    bytes32 queryId1;

    //private variables
    address owner;

    mapping(uint => address) public tickets;
 
    function Lottery(){
        owner = msg.sender;
        //oraclize_setNetwork(networkID_testnet);
    }
    
    function __callback(bytes32 _queryId, string _result, bytes _proof)
    { 
        if (msg.sender != oraclize_cbAddress()) throw;
        if(_queryId == queryId1) {stage = 1; return;}
        if (oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) {
            stage = 3;
        } else {
            // the proof verification has passed
            winningNumber = (uint(sha3(_result)) % (numTickets-1))+1; // this is an efficient way to get the uint out in the [0, maxRange] range
            winningaddr = tickets[winningNumber];
            //send all ETH to winner
            winningaddr.transfer(this.balance);
            stage = 3;
        }
    }

    //Owner calls after EndLottery
    function newGame(uint _bet_amount, uint _durationh, uint _gwei) public payable{
        if((stage == 3)&&(msg.sender == owner)){
            //reset for new game
            for(uint i = 1; i<numTickets; i++){
                tickets[i] = 0;
            }
            oraclize_setCustomGasPrice(_gwei * 1000000000 wei);
            winningNumber = 0;
            bet_amount = _bet_amount * 1 finney;
            durationh = _durationh * 1 hours;
            numTickets = 1;
            stage = 0;
            startTime = now;
            //ready for next round(next bets)
            oraclize_setProof(proofType_TLSNotary);
            queryId1 = oraclize_query(durationh, "URL", "", 65000);
        }
        else
        throw;
    }
    
    //Called from participants
    function Bet() public payable  {
        if ((msg.value == bet_amount) && (stage == 0)) {
            tickets[numTickets] = msg.sender; //save address of participant
            numTickets++;
        }
        else
         throw;
    }
    
    function payout() public payable{
        if(stage == 1){
            if(numTickets == 1)
            {
                stage = 3;
            }
            else
            {
                oraclize_setProof(proofType_Ledger);
                oraclize_newRandomDSQuery(0, 4, 150000);
                //Fee for owner 0.66%
                owner.transfer(this.balance/150);
                stage = 2;
            }
        }
        else
         throw;
    }
}
// </ORACLIZE_API>
function Bet() public payable  {
    if ((msg.value == bet_amount) && (stage == 0)) {
        tickets[numTickets] = msg.sender; //save address of participant
        numTickets++;
    }
    else
     throw;
}

//Called from participants