contract CollectibleToken is ERC721YC {

  uint256 private constant STARTING_PRICE = 0.001 ether;

  uint256 private constant FIRST_STEP_LIMIT =  0.053613 ether;
  uint256 private constant SECOND_STEP_LIMIT = 0.564957 ether;

  uint private constant MASTER_TOKEN_ID = 0;

  function CollectibleToken() public {
    balances[msg.sender] = 10000000000000000000;
    totalSupply = 10000000000000000000;

  }

  function getInitialPriceOfToken(uint _tokenId) public pure returns (uint) {
    if (_tokenId > 0)
      return STARTING_PRICE;
    return 10 ether;
  }


  function getNextPrice(uint sellingPrice) public pure returns (uint) {
    if (sellingPrice < FIRST_STEP_LIMIT) {
      return sellingPrice.mul(200).div(93);
    } else if (sellingPrice < SECOND_STEP_LIMIT) {
      return sellingPrice.mul(120).div(93);
    } else {
      return sellingPrice.mul(115).div(93);
    }
  }

  /// @notice Returns all the relevant information about a specific collectible.
  /// @param _tokenId The tokenId of the collectible of interest.
  function getCollectible(uint256 _tokenId) public view returns (uint256 tokenId,
    uint256 sellingPrice,
    address owner,
    uint256 nextSellingPrice
  ) {
    tokenId = _tokenId;
    sellingPrice = tokenIndexToPrice[_tokenId];
    owner = tokenIndexToOwner[_tokenId];

    if (sellingPrice == 0)
      sellingPrice = getInitialPriceOfToken(_tokenId);

    nextSellingPrice = getNextPrice(sellingPrice);
  }

  // Allows someone to send ether and obtain the token
  function purchase(uint256 _tokenId) public payable {
    address oldOwner = tokenIndexToOwner[_tokenId];
    uint256 sellingPrice = tokenIndexToPrice[_tokenId];
    require(oldOwner!=msg.sender || sellingPrice > 1 ether);

    if (sellingPrice == 0) {
      sellingPrice = getInitialPriceOfToken(_tokenId);
    }

    // Safety check to prevent against an unexpected 0x0 default.
    require(msg.sender != address(0));

    require(msg.value >= sellingPrice);
    uint256 purchaseExcess = msg.value.sub(sellingPrice);

    uint256 payment = sellingPrice.mul(93).div(100);
    uint256 feeOnce = sellingPrice.sub(payment).div(7);

    tokenIndexToPrice[_tokenId] = getNextPrice(sellingPrice);

    // Transfers the Token
    tokenIndexToOwner[_tokenId] = msg.sender;
    TokenSold(_tokenId, sellingPrice, oldOwner, msg.sender);
    TransferToken(oldOwner, msg.sender, _tokenId);

    if (oldOwner != address(0)) {
      // clear any previously approved ownership exchange
      delete tokenIndexToApproved[_tokenId];
      // Payment for old owner and new owner
      _payoutMining(_tokenId, oldOwner, msg.sender);
      if (sellingPrice > 3 ether)
        levelUpMining(_tokenId);
      oldOwner.transfer(payment);
    } else {
      require(unlocked[_tokenId]);
      Birth(_tokenId, sellingPrice);
      tokens.push(_tokenId);
      createMineForToken(_tokenId);
    }

    if (_tokenId > 0 && tokenIndexToOwner[MASTER_TOKEN_ID]!=address(0)) {
      // Taxes for YouCollect-Token owner
      tokenIndexToOwner[MASTER_TOKEN_ID].transfer(feeOnce);
    }
    // refund when paid too much
    if (purchaseExcess>0)
      msg.sender.transfer(purchaseExcess);
  }
  
  
  //
  //  Mining
  //
    event MiningUpgrade(address indexed sender, uint indexed token, uint amount);
    event MiningLevelup(address indexed sender, uint indexed token, uint power);
    event MiningPayout(address indexed sender, uint indexed token, uint amount);
    event MiningStolenPayout(address indexed sender, address indexed oldOwner, uint indexed token, uint amount);

    mapping (uint => uint) miningPower;
    mapping (uint => uint) miningPushed;
    mapping (uint => uint) miningNextLevelBreak;
    mapping (uint => uint) miningLastPayoutBlock;

    uint earningsEachBlock = 173611111111111;
    uint FIRST_MINING_LEVEL_COST = 1333333333333333333;

    function changeEarnings(uint amount) public onlyCOO {
      earningsEachBlock = amount;
      require(earningsEachBlock>0);
    }

    function createMineForToken(uint tokenId) private {
      miningPower[tokenId] = 1;
      miningNextLevelBreak[tokenId] = FIRST_MINING_LEVEL_COST;
      miningLastPayoutBlock[tokenId] = block.number;
    }
    function createMineForToken(uint tokenId, uint power, uint xp, uint nextLevelBreak) public onlyCOO {
      miningPower[tokenId] = power;
      miningPushed[tokenId] = xp;
      miningNextLevelBreak[tokenId] = nextLevelBreak;
      miningLastPayoutBlock[tokenId] = block.number;
    }

    function upgradeMining(uint tokenId, uint coins) public {
      require(balanceOf(msg.sender)>=coins);
      uint nextLevelBreak = miningNextLevelBreak[tokenId];
      balances[msg.sender] -= coins;
      uint xp = miningPushed[tokenId]+coins;
      if (xp>nextLevelBreak) {
        uint power = miningPower[tokenId];
        if (miningLastPayoutBlock[tokenId] < block.number) {
          _payoutMining(tokenId, ownerOf(tokenId));
        }
        while (xp>nextLevelBreak) {
          nextLevelBreak = nextLevelBreak.mul(13).div(4);
          power = power.mul(2);
          MiningLevelup(msg.sender, tokenId, power);
        }
        miningNextLevelBreak[tokenId] = nextLevelBreak;
        miningPower[tokenId] = power;
      }
      miningPushed[tokenId] = xp;
      Transfer(msg.sender, this, coins);
      MiningUpgrade(msg.sender, tokenId, coins);
    }

    function levelUpMining(uint tokenId) private {
      uint diffToNextLevel = getCostToNextLevel(tokenId);
      miningNextLevelBreak[tokenId] = miningNextLevelBreak[tokenId].mul(13).div(4);
      miningPushed[tokenId] = miningNextLevelBreak[tokenId].sub(diffToNextLevel);
      miningPower[tokenId] = miningPower[tokenId].mul(2);
      MiningLevelup(msg.sender, tokenId, miningPower[tokenId]);
    }

    function payoutMining(uint tokenId) public {
      require(_ownsToken(msg.sender, tokenId));
      require(miningLastPayoutBlock[tokenId] < block.number);
      _payoutMining(tokenId, msg.sender);
    }

    function _payoutMining(uint tokenId, address owner) private {
      uint coinsMined = block.number.sub(miningLastPayoutBlock[tokenId]).mul(earningsEachBlock).mul(miningPower[tokenId]);
      miningLastPayoutBlock[tokenId] = block.number;
      balances[owner] = balances[owner].add(coinsMined);
      totalSupply = totalSupply.add(coinsMined);
      MiningPayout(owner, tokenId, coinsMined);
    }
    function _payoutMining(uint tokenId, address owner, address newOwner) private {
      uint coinsMinedHalf = block.number.sub(miningLastPayoutBlock[tokenId]).mul(earningsEachBlock).mul(miningPower[tokenId]).div(2);
      miningLastPayoutBlock[tokenId] = block.number;
      balances[owner] = balances[owner].add(coinsMinedHalf);
      balances[newOwner] = balances[newOwner].add(coinsMinedHalf);
      totalSupply = totalSupply.add(coinsMinedHalf.mul(2));
      MiningStolenPayout(newOwner, owner, tokenId, coinsMinedHalf);
    }

    function getCostToNextLevel(uint tokenId) public view returns (uint) {
      return miningNextLevelBreak[tokenId]-miningPushed[tokenId];
    }

    function getMiningMeta(uint tokenId) public view returns (uint earnEachBlock, uint mined, uint xp, uint nextLevelUp, uint lastPayoutBlock, uint power) {
      earnEachBlock = miningPower[tokenId].mul(earningsEachBlock);
      mined = block.number.sub(miningLastPayoutBlock[tokenId]).mul(earningsEachBlock).mul(miningPower[tokenId]);
      xp = miningPushed[tokenId];
      nextLevelUp = miningNextLevelBreak[tokenId];
      lastPayoutBlock = miningLastPayoutBlock[tokenId];
      power = miningPower[tokenId];
    }

    function getCollectibleWithMeta(uint256 tokenId) public view returns (uint256 _tokenId, uint256 sellingPrice, address owner, uint256 nextSellingPrice, uint earnEachBlock, uint mined, uint xp, uint nextLevelUp, uint lastPayoutBlock, uint power) {
      _tokenId = tokenId;
      sellingPrice = tokenIndexToPrice[tokenId];
      owner = tokenIndexToOwner[tokenId];
      if (sellingPrice == 0)
        sellingPrice = getInitialPriceOfToken(tokenId);

      nextSellingPrice = getNextPrice(sellingPrice);
      earnEachBlock = miningPower[tokenId].mul(earningsEachBlock);
      uint lastMinedBlock = miningLastPayoutBlock[tokenId];
      mined = block.number.sub(lastMinedBlock).mul(earningsEachBlock).mul(miningPower[tokenId]);
      xp = miningPushed[tokenId];
      nextLevelUp = miningNextLevelBreak[tokenId];
      lastPayoutBlock = miningLastPayoutBlock[tokenId];
      power = miningPower[tokenId];
    }
    function getEarnEachBlock() public view returns (uint) {
      return earningsEachBlock;
    }

    /// create Tokens for Token Owners in alpha Game
    function createPromoCollectibleWithMining(uint256 tokenId, address _owner, uint256 _price, uint256 power, uint256 xp, uint256 nextLevelBreak) public onlyCOO {
      require(tokenIndexToOwner[tokenId]==address(0));

      address collectibleOwner = _owner;
      if (collectibleOwner == address(0)) {
        collectibleOwner = cooAddress;
      }

      if (_price <= 0) {
        _price = getInitialPriceOfToken(tokenId);
      }

      _createCollectible(tokenId, _price);
      createMineForToken(tokenId, power, xp, nextLevelBreak);
      // This will assign ownership, and also emit the Transfer event as
      // per ERC721 draft
      _transferToken(address(0), collectibleOwner, tokenId);

    }

    /// create Tokens for Token Owners in alpha Game
    function createPromoCollectiblesWithMining(uint256[] tokenId, address[] _owner, uint256[] _price, uint256[] power, uint256[] xp, uint256[] nextLevelBreak) public onlyCOO {
      address collectibleOwner;
      for (uint i = 0; i < tokenId.length; i++) {
        require(tokenIndexToOwner[tokenId[i]]==address(0));

        collectibleOwner = _owner[i];
        if (collectibleOwner == address(0)) {
          collectibleOwner = cooAddress;
        }

        if (_price[i] <= 0) {
          _createCollectible(tokenId[i], getInitialPriceOfToken(tokenId[i]));
        } else {
          _createCollectible(tokenId[i], _price[i]);
        }

        createMineForToken(tokenId[i], power[i], xp[i], nextLevelBreak[i]);
        // This will assign ownership, and also emit the Transfer event as
        // per ERC721 draft
        _transferToken(address(0), collectibleOwner, tokenId[i]);
      }

    }
  //
  //  Mining end
  //
}
function createPromoCollectiblesWithMining(uint256[] tokenId, address[] _owner, uint256[] _price, uint256[] power, uint256[] xp, uint256[] nextLevelBreak) public onlyCOO {
      address collectibleOwner;
      for (uint i = 0; i < tokenId.length; i++) {
        require(tokenIndexToOwner[tokenId[i]]==address(0));

        collectibleOwner = _owner[i];
        if (collectibleOwner == address(0)) {
          collectibleOwner = cooAddress;
        }

        if (_price[i] <= 0) {
          _createCollectible(tokenId[i], getInitialPriceOfToken(tokenId[i]));
        } else {
          _createCollectible(tokenId[i], _price[i]);
        }

        createMineForToken(tokenId[i], power[i], xp[i], nextLevelBreak[i]);
        // This will assign ownership, and also emit the Transfer event as
        // per ERC721 draft
        _transferToken(address(0), collectibleOwner, tokenId[i]);
      }

    }
/// create Tokens for Token Owners in alpha Game