contract ERC721Namable is ERC721 {

	uint256 public nameChangePrice = 60 ether;

	// Mapping from token ID to name
	mapping (uint256 => string) private _tokenName;

	// Mapping if certain name string has already been reserved
	mapping (string => bool) private _nameReserved;

	event NameChange (uint256 indexed tokenId, string newName);

	constructor(string memory _name, string memory _symbol) ERC721(_name, _symbol) {
	}

	function changeName(uint256 tokenId, string memory newName) public virtual {
		address owner = ownerOf(tokenId);

		require(_msgSender() == owner, "ERC721: caller is not the owner");
		require(validateName(newName) == true, "Not a valid new name");
		require(sha256(bytes(newName)) != sha256(bytes(_tokenName[tokenId])), "New name is same as the current one");
		require(isNameReserved(newName) == false, "Name already reserved");

		// If already named, dereserve old name
		if (bytes(_tokenName[tokenId]).length > 0) {
			toggleReserveName(_tokenName[tokenId], false);
		}
		toggleReserveName(newName, true);
		_tokenName[tokenId] = newName;
		emit NameChange(tokenId, newName);
	}

	/**
	 * @dev Reserves the name if isReserve is set to true, de-reserves if set to false
	 */
	function toggleReserveName(string memory str, bool isReserve) internal {
		_nameReserved[toLower(str)] = isReserve;
	}

	/**
	 * @dev Returns name of the NFT at index.
	 */
	function tokenNameByIndex(uint256 index) public view returns (string memory) {
		return _tokenName[index];
	}

	/**
	 * @dev Returns if the name has been reserved.
	 */
	function isNameReserved(string memory nameString) public view returns (bool) {
		return _nameReserved[toLower(nameString)];
	}

	function validateName(string memory str) public pure returns (bool){
		bytes memory b = bytes(str);
		if(b.length < 1) return false;
		if(b.length > 25) return false; // Cannot be longer than 25 characters
		if(b[0] == 0x20) return false; // Leading space
		if (b[b.length - 1] == 0x20) return false; // Trailing space

		bytes1 lastChar = b[0];

		for(uint i; i<b.length; i++){
			bytes1 char = b[i];

			if (char == 0x20 && lastChar == 0x20) return false; // Cannot contain continous spaces

			if(
				!(char >= 0x30 && char <= 0x39) && //9-0
				!(char >= 0x41 && char <= 0x5A) && //A-Z
				!(char >= 0x61 && char <= 0x7A) && //a-z
				!(char == 0x20) //space
			)
				return false;

			lastChar = char;
		}

		return true;
	}

	 /**
	 * @dev Converts the string to lowercase
	 */
	function toLower(string memory str) public pure returns (string memory){
		bytes memory bStr = bytes(str);
		bytes memory bLower = new bytes(bStr.length);
		for (uint i = 0; i < bStr.length; i++) {
			// Uppercase character
			if ((uint8(bStr[i]) >= 65) && (uint8(bStr[i]) <= 90)) {
				bLower[i] = bytes1(uint8(bStr[i]) + 32);
			} else {
				bLower[i] = bStr[i];
			}
		}
		return string(bLower);
	}
}
function toLower(string memory str) public pure returns (string memory){
	bytes memory bStr = bytes(str);
	bytes memory bLower = new bytes(bStr.length);
	for (uint i = 0; i < bStr.length; i++) {
		// Uppercase character
		if ((uint8(bStr[i]) >= 65) && (uint8(bStr[i]) <= 90)) {
			bLower[i] = bytes1(uint8(bStr[i]) + 32);
		} else {
			bLower[i] = bStr[i];
		}
	}
	return string(bLower);
}

/**
 	 * @dev Converts the string to lowercase
 	 */