contract Jackpot is Ownable, usingOraclize {

  string public constant name = "Jackpot";

  event newWinner(address winner, uint256 ticketNumber);
  event newRandomNumber_bytes(bytes);
  event newRandomNumber_uint(uint);
  event newContribution(address contributor, uint value);

  using SafeMath for uint256;
  address[] public players = new address[](10);
  uint256 public lastTicketNumber = 0;
  uint public lastIndex = 0;

  struct tickets {
    uint256 startTicket;
    uint256 endTicket;
  }

  mapping (address => tickets[]) public ticketsMap;
  mapping (address => uint256) public contributions;

  function Jackpot() {
    oraclize_setProof(proofType_Ledger); // sets the Ledger authenticity proof in the constructor
  }

    // the callback function is called by Oraclize when the result is ready
    // the oraclize_randomDS_proofVerify modifier prevents an invalid proof to execute this function code:
    // the proof validity is fully verified on-chain
  function __callback(bytes32 _queryId, string _result, bytes _proof)
  { 
      if (msg.sender != oraclize_cbAddress()) throw;
      
      if (oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) {
          // the proof verification has failed, do we need to take any action here? (depends on the use case)
      } else {
        if (lastIndex > 9) {
          // the proof verification has passed
          // now that we know that the random number was safely generated, let's use it..
          
          // newRandomNumber_bytes(bytes(_result)); // this is the resulting random number (bytes)
          
          // for simplicity of use, let's also convert the random bytes to uint if we need
          uint maxRange = 2**(8* 7); // this is the highest uint we want to get. It should never be greater than 2^(8*N), where N is the number of random bytes we had asked the datasource to return
          uint randomNumber = uint(sha3(_result)) % maxRange; // this is an efficient way to get the uint out in the [0, maxRange] range
          randomNumber = randomNumber % lastTicketNumber;
          newRandomNumber_uint(randomNumber); // this is the resulting random number (uint)
          address winner;
          bool hasWon;
          for (uint i = 0; i < lastIndex; i++) {
            address player = players[i];
            for (uint j = 0; j < ticketsMap[player].length; j++) {
              uint256 start = ticketsMap[player][j].startTicket;
              uint256 end = ticketsMap[player][j].endTicket;
              if (randomNumber >= start && randomNumber < end) {
                winner = player;
                hasWon = true;
                break;
              }
            }
            if(hasWon) break;
          }
          require(winner!=address(0) && hasWon);

          for (uint k = 0; k < lastIndex; k++) {
            delete ticketsMap[players[k]];
            delete contributions[players[k]];
          }

          lastIndex = 0;
          lastTicketNumber = 0;

          uint balance = this.balance;
          if (!owner.send(balance/10)) throw;
          //Both SafeMath.div and / throws on error
          if (!winner.send(balance - balance/10)) throw;
          newWinner(winner, randomNumber);
          
        }
      }
  }

  function getPlayers() constant returns (address[], uint256[]) {
    address[] memory addrs = new address[](lastIndex);
    uint256[] memory _contributions = new uint256[](lastIndex);
    for (uint i = 0; i < lastIndex; i++) {
      addrs[i] = players[i];
      _contributions[i] = contributions[players[i]];
    }
    return (addrs, _contributions);
  }

  function getTickets(address _addr) constant returns (uint256[] _start, uint256[] _end) {
    tickets[] tks = ticketsMap[_addr];
    uint length = tks.length;
    uint256[] memory startTickets = new uint256[](length);
    uint256[] memory endTickets = new uint256[](length);
    for (uint i = 0; i < length; i++) {
      startTickets[i] = tks[i].startTicket;
      endTickets[i] = tks[i].endTicket;
    }
    return (startTickets, endTickets);
  }

  function() payable {
    uint256 weiAmount = msg.value;
    require(weiAmount >= 1e16);

    bool isSenderAdded = false;
    for (uint i = 0; i < lastIndex; i++) {
      if (players[i] == msg.sender) {
        isSenderAdded = true;
        break;
      }
    }
    if (!isSenderAdded) {
      players[lastIndex] = msg.sender;
      lastIndex++;
    }

    tickets memory senderTickets;
    senderTickets.startTicket = lastTicketNumber;
    uint256 numberOfTickets = weiAmount/1e15;
    senderTickets.endTicket = lastTicketNumber.add(numberOfTickets);
    lastTicketNumber = lastTicketNumber.add(numberOfTickets);
    ticketsMap[msg.sender].push(senderTickets);

    contributions[msg.sender] = contributions[msg.sender].add(weiAmount);

    newContribution(msg.sender, weiAmount);

    if(lastIndex > 9) {
      uint N = 7; // number of random bytes we want the datasource to return
      uint delay = 0; // number of seconds to wait before the execution takes place
      uint callbackGas = 3000000; // amount of gas we want Oraclize to set for the callback function
      bytes32 queryId = oraclize_newRandomDSQuery(delay, N, callbackGas); // this function internally generates the correct oraclize_query and returns its queryId
    }
  }
}
function __callback(bytes32 _queryId, string _result, bytes _proof)
{ 
    if (msg.sender != oraclize_cbAddress()) throw;
      
    if (oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) {
        // the proof verification has failed, do we need to take any action here? (depends on the use case)
    } else {
      if (lastIndex > 9) {
        // the proof verification has passed
        // now that we know that the random number was safely generated, let's use it..
          
        // newRandomNumber_bytes(bytes(_result)); // this is the resulting random number (bytes)
          
        // for simplicity of use, let's also convert the random bytes to uint if we need
        uint maxRange = 2**(8* 7); // this is the highest uint we want to get. It should never be greater than 2^(8*N), where N is the number of random bytes we had asked the datasource to return
        uint randomNumber = uint(sha3(_result)) % maxRange; // this is an efficient way to get the uint out in the [0, maxRange] range
        randomNumber = randomNumber % lastTicketNumber;
        newRandomNumber_uint(randomNumber); // this is the resulting random number (uint)
        address winner;
        bool hasWon;
        for (uint i = 0; i < lastIndex; i++) {
          address player = players[i];
          for (uint j = 0; j < ticketsMap[player].length; j++) {
            uint256 start = ticketsMap[player][j].startTicket;
            uint256 end = ticketsMap[player][j].endTicket;
            if (randomNumber >= start && randomNumber < end) {
              winner = player;
              hasWon = true;
              break;
            }
          }
          if(hasWon) break;
        }
        require(winner!=address(0) && hasWon);

        for (uint k = 0; k < lastIndex; k++) {
          delete ticketsMap[players[k]];
          delete contributions[players[k]];
        }

        lastIndex = 0;
        lastTicketNumber = 0;

        uint balance = this.balance;
        if (!owner.send(balance/10)) throw;
        //Both SafeMath.div and / throws on error
        if (!winner.send(balance - balance/10)) throw;
        newWinner(winner, randomNumber);
          
      }
    }
}

// the callback function is called by Oraclize when the result is ready
// the oraclize_randomDS_proofVerify modifier prevents an invalid proof to execute this function code:
// the proof validity is fully verified on-chain