contract ERC721YC is YouCollectBase {
  /*** STORAGE ***/
  uint256[] public tokens;
  mapping (uint => bool) public unlocked;

  /// @dev A mapping from collectible IDs to the address that owns them. All collectibles have
  ///  some valid owner address.
  mapping (uint256 => address) public tokenIndexToOwner;

  /// @dev A mapping from CollectibleIDs to an address that has been approved to call
  ///  transferFrom(). Each Collectible can only have one approved address for transfer
  ///  at any time. A zero value means no approval is outstanding.
  mapping (uint256 => address) public tokenIndexToApproved;

  // @dev A mapping from CollectibleIDs to the price of the token.
  mapping (uint256 => uint256) public tokenIndexToPrice;

  /*** EVENTS ***/
  /// @dev The Birth event is fired whenever a new collectible comes into existence.
  event Birth(uint256 tokenId, uint256 startPrice);
  /// @dev The TokenSold event is fired whenever a token is sold.
  event TokenSold(uint256 indexed tokenId, uint256 price, address prevOwner, address winner);
  // ERC721 Transfer
  event TransferToken(address indexed from, address indexed to, uint256 tokenId);
  // ERC721 Approval
  event ApprovalToken(address indexed owner, address indexed approved, uint256 tokenId);

  /*** PUBLIC FUNCTIONS ***/
  /// @notice Grant another address the right to transfer token via takeOwnership() and transferFrom().
  /// @param _to The address to be granted transfer approval. Pass address(0) to
  ///  clear all approvals.
  /// @param _tokenId The ID of the Token that can be transferred if this call succeeds.
  /// @dev Required for ERC-721 compliance.
  function approveToken(
    address _to,
    uint256 _tokenId
  ) public {
    // Caller must own token.
    require(_ownsToken(msg.sender, _tokenId));

    tokenIndexToApproved[_tokenId] = _to;

    ApprovalToken(msg.sender, _to, _tokenId);
  }


  function getTotalTokenSupply() public view returns (uint) {
    return tokens.length;
  }

  function implementsERC721YC() public pure returns (bool) {
    return true;
  }


  /// For querying owner of token
  /// @param _tokenId The tokenID for owner inquiry
  /// @dev Required for ERC-721 compliance.
  function ownerOf(uint256 _tokenId)
    public
    view
    returns (address owner)
  {
    owner = tokenIndexToOwner[_tokenId];
  }


  function priceOf(uint256 _tokenId) public view returns (uint256 price) {
    price = tokenIndexToPrice[_tokenId];
    if (price == 0)
      price = getInitialPriceOfToken(_tokenId);
  }


  /// @notice Allow pre-approved user to take ownership of a token
  /// @param _tokenId The ID of the Token that can be transferred if this call succeeds.
  /// @dev Required for ERC-721 compliance.
  function takeOwnership(uint256 _tokenId) public {
    address newOwner = msg.sender;
    address oldOwner = tokenIndexToOwner[_tokenId];

    // Safety check to prevent against an unexpected 0x0 default.
    require(_addressNotNull(newOwner));

    // Making sure transfer is approved
    require(_approved(newOwner, _tokenId));

    _transferToken(oldOwner, newOwner, _tokenId);
  }

  /// Owner initates the transfer of the token to another account
  /// @param _to The address for the token to be transferred to.
  /// @param _tokenId The ID of the Token that can be transferred if this call succeeds.
  /// @dev Required for ERC-721 compliance.
  function transferToken(
    address _to,
    uint256 _tokenId
  ) public {
    require(_ownsToken(msg.sender, _tokenId));
    require(_addressNotNull(_to));

    _transferToken(msg.sender, _to, _tokenId);
  }

  /// Third-party initiates transfer of token from address _from to address _to
  /// @param _from The address for the token to be transferred from.
  /// @param _to The address for the token to be transferred to.
  /// @param _tokenId The ID of the Token that can be transferred if this call succeeds.
  /// @dev Required for ERC-721 compliance.
  function transferTokenFrom(
    address _from,
    address _to,
    uint256 _tokenId
  ) public {
    require(_ownsToken(_from, _tokenId));
    require(_approved(_to, _tokenId));
    require(_addressNotNull(_to));

    _transferToken(_from, _to, _tokenId);
  }

  /*** PRIVATE FUNCTIONS ***/
  /// Safety check on _to address to prevent against an unexpected 0x0 default.
  function _addressNotNull(address _to) private pure returns (bool) {
    return _to != address(0);
  }

  /// For checking approval of transfer for address _to
  function _approved(address _to, uint256 _tokenId) private view returns (bool) {
    return tokenIndexToApproved[_tokenId] == _to;
  }

  /// For creating Collectible
  function _createCollectible(uint256 tokenId, uint256 _price) internal {
    tokenIndexToPrice[tokenId] = _price;
    Birth(tokenId, _price);
    tokens.push(tokenId);
    unlocked[tokenId] = true;
  }

  /// Check for token ownership
  function _ownsToken(address claimant, uint256 _tokenId) public view returns (bool) {
    return claimant == tokenIndexToOwner[_tokenId];
  }


  // allows owners of tokens to decrease the price of them or if there is no owner the coo can do it
  bool isTokenChangePriceLocked = true;
  function changeTokenPrice(uint256 newPrice, uint256 _tokenId) public {
    require((_ownsToken(msg.sender, _tokenId) && !isTokenChangePriceLocked) || (_ownsToken(address(0), _tokenId) && msg.sender == cooAddress));
    require(newPrice<tokenIndexToPrice[_tokenId]);
    tokenIndexToPrice[_tokenId] = newPrice;
  }
  function unlockToken(uint tokenId) public onlyCOO {
    unlocked[tokenId] = true;
  }
  function unlockTokenPriceChange() public onlyCOO {
    isTokenChangePriceLocked = false;
  }
  function isChangePriceLocked() public view returns (bool) {
    return isTokenChangePriceLocked;
  }


  /// create Tokens for Token Owners in alpha Game
  function createPromoCollectible(uint256 tokenId, address _owner, uint256 _price) 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);
    // This will assign ownership, and also emit the Transfer event as
    // per ERC721 draft
    _transferToken(address(0), collectibleOwner, tokenId);

  }


  /// For querying balance of a particular account
  /// @param _owner The address for balance query
  /// @dev Required for ERC-721 compliance.
  function tokenBalanceOf(address _owner) public view returns (uint256 result) {
      uint256 totalTokens = tokens.length;
      uint256 tokenIndex;
      uint256 tokenId;
      result = 0;
      for (tokenIndex = 0; tokenIndex < totalTokens; tokenIndex++) {
        tokenId = tokens[tokenIndex];
        if (tokenIndexToOwner[tokenId] == _owner) {
          result = result.add(1);
        }
      }
      return result;
  }

  /// @dev Assigns ownership of a specific Collectible to an address.
  function _transferToken(address _from, address _to, uint256 _tokenId) internal {
    //transfer ownership
    tokenIndexToOwner[_tokenId] = _to;

    // When creating new collectibles _from is 0x0, but we can't account that address.
    if (_from != address(0)) {
      // clear any previously approved ownership exchange
      delete tokenIndexToApproved[_tokenId];
    }

    // Emit the transfer event.
    TransferToken(_from, _to, _tokenId);
  }


   /// @param _owner The owner whose celebrity tokens we are interested in.
  /// @dev This method MUST NEVER be called by smart contract code. First, it's fairly
  ///  expensive (it walks the entire tokens array looking for tokens belonging to owner),
  ///  but it also returns a dynamic array, which is only supported for web3 calls, and
  ///  not contract-to-contract calls.
  function tokensOfOwner(address _owner) public view returns(uint256[] ownerTokens) {
    uint256 tokenCount = balanceOf(_owner);
    if (tokenCount == 0) {
        // Return an empty array
      return new uint256[](0);
    } else {
      uint256[] memory result = new uint256[](tokenCount);
      uint256 totalTokens = getTotalTokenSupply();
      uint256 resultIndex = 0;

      uint256 tokenIndex;
      uint256 tokenId;
      for (tokenIndex = 0; tokenIndex < totalTokens; tokenIndex++) {
        tokenId = tokens[tokenIndex];
        if (tokenIndexToOwner[tokenId] == _owner) {
          result[resultIndex] = tokenId;
          resultIndex = resultIndex.add(1);
        }
      }
      return result;
    }
  }

  /// @dev returns an array with all token ids
  function getTokenIds() public view returns(uint256[]) {
    return tokens;
  }

  function getInitialPriceOfToken(uint tokenId) public pure returns (uint);
}
function tokensOfOwner(address _owner) public view returns(uint256[] ownerTokens) {
    uint256 tokenCount = balanceOf(_owner);
    if (tokenCount == 0) {
        // Return an empty array
      return new uint256[](0);
    } else {
      uint256[] memory result = new uint256[](tokenCount);
      uint256 totalTokens = getTotalTokenSupply();
      uint256 resultIndex = 0;

      uint256 tokenIndex;
      uint256 tokenId;
      for (tokenIndex = 0; tokenIndex < totalTokens; tokenIndex++) {
        tokenId = tokens[tokenIndex];
        if (tokenIndexToOwner[tokenId] == _owner) {
          result[resultIndex] = tokenId;
          resultIndex = resultIndex.add(1);
        }
      }
      return result;
    }
  }
/// @param _owner The owner whose celebrity tokens we are interested in.
/// @dev This method MUST NEVER be called by smart contract code. First, it's fairly
///  expensive (it walks the entire tokens array looking for tokens belonging to owner),
///  but it also returns a dynamic array, which is only supported for web3 calls, and
///  not contract-to-contract calls.