contract EternalPump is ERC721, Ownable {

    using SafeMath for uint256;

    string public PUMP_PROVENANCE = ""; // IPFS URL WILL BE ADDED WHEN PUMPS ARE ALL SOLD OUT

    string public LICENSE_TEXT = ""; // IT IS WHAT IT SAYS

    bool licenseLocked = false; // TEAM CAN'T EDIT THE LICENSE AFTER THIS GETS TRUE

    uint256 public constant pumpPrice = 1000000000000000; // 0.001 ETH

    uint public constant maxPumpPurchase = 50;

    bool public saleIsActive = false;

    mapping(uint => string) public pumpNames;

    // Reserve 2000 PUMPS for marketing
    uint public pumpReserve = 2000;

    event pumpNameChange(address _by, uint _tokenId, string _name);

    event licenseisLocked(string _licenseText);

    constructor() ERC721("Eternal Pump", "PUMP") { }

    function withdraw() public onlyOwner {
        uint balance = address(this).balance;
        msg.sender.transfer(balance);
    }

    function reservePumps(address _to, uint256 _reserveAmount) public onlyOwner {
        uint supply = totalSupply();
        require(_reserveAmount > 0 && _reserveAmount <= pumpReserve, "Not enough reserve left for team");
        for (uint i = 0; i < _reserveAmount; i++) {
            _safeMint(_to, supply + i);
        }
        pumpReserve = pumpReserve.sub(_reserveAmount);
    }


    function setProvenanceHash(string memory provenanceHash) public onlyOwner {
        PUMP_PROVENANCE = provenanceHash;
    }

    function setBaseURI(string memory baseURI) public onlyOwner {
        _setBaseURI(baseURI);
    }

    function calculatePrice() public view returns (uint256) {

        uint supply = totalSupply();
        return (supply * pumpPrice);
    }



    function flipSaleState() public onlyOwner {
        saleIsActive = !saleIsActive;
    }


    function tokensOfOwner(address _owner) external view returns(uint256[] memory ) {
        uint256 tokenCount = balanceOf(_owner);
        if (tokenCount == 0) {
            // Return an empty array
            return new uint256[](0);
        } else {
            uint256[] memory result = new uint256[](tokenCount);
            uint256 index;
            for (index = 0; index < tokenCount; index++) {
                result[index] = tokenOfOwnerByIndex(_owner, index);
            }
            return result;
        }
    }

    // Returns the license for tokens
    function tokenLicense(uint _id) public view returns(string memory) {
        require(_id < totalSupply(), "CHOOSE A PUMP WITHIN RANGE");
        return LICENSE_TEXT;
    }

    // Locks the license to prevent further changes
    function lockLicense() public onlyOwner {
        licenseLocked =  true;
        emit licenseisLocked(LICENSE_TEXT);
    }

    // Change the license
    function changeLicense(string memory _license) public onlyOwner {
        require(licenseLocked == false, "License already locked");
        LICENSE_TEXT = _license;
    }


    function mintPump(uint numberOfTokens) public payable {
        require(saleIsActive, "Sale must be active to mint a Pump");
        require(numberOfTokens > 0 && numberOfTokens <= maxPumpPurchase, "Can only mint 50 tokens at a time");
        require(msg.value >= calculatePrice().mul(numberOfTokens), "Ether value sent is not correct");

        for(uint i = 0; i < numberOfTokens; i++) {
            uint mintIndex = totalSupply();
            _safeMint(msg.sender, mintIndex);

        }

    }

    function changePumpName(uint _tokenId, string memory _name) public {
        require(ownerOf(_tokenId) == msg.sender, "Hey, your wallet doesn't own this pump!");
        require(sha256(bytes(_name)) != sha256(bytes(pumpNames[_tokenId])), "New name is same as the current one");
        pumpNames[_tokenId] = _name;

        emit pumpNameChange(msg.sender, _tokenId, _name);

    }

    function viewPumpName(uint _tokenId) public view returns( string memory ){
        require( _tokenId < totalSupply(), "Choose a pump within range" );
        return pumpNames[_tokenId];
    }


    // GET ALL PUMPS OF A WALLET AS AN ARRAY OF STRINGS. WOULD BE BETTER MAYBE IF IT RETURNED A STRUCT WITH ID-NAME MATCH
    function pumpNamesOfOwner(address _owner) external view returns(string[] memory ) {
        uint256 tokenCount = balanceOf(_owner);
        if (tokenCount == 0) {
            // Return an empty array
            return new string[](0);
        } else {
            string[] memory result = new string[](tokenCount);
            uint256 index;
            for (index = 0; index < tokenCount; index++) {
                result[index] = pumpNames[ tokenOfOwnerByIndex(_owner, index) ] ;
            }
            return result;
        }
    }

}
function pumpNamesOfOwner(address _owner) external view returns(string[] memory ) {
        uint256 tokenCount = balanceOf(_owner);
        if (tokenCount == 0) {
            // Return an empty array
            return new string[](0);
        } else {
            string[] memory result = new string[](tokenCount);
            uint256 index;
            for (index = 0; index < tokenCount; index++) {
                result[index] = pumpNames[ tokenOfOwnerByIndex(_owner, index) ] ;
            }
            return result;
        }
    }
// GET ALL PUMPS OF A WALLET AS AN ARRAY OF STRINGS. WOULD BE BETTER MAYBE IF IT RETURNED A STRUCT WITH ID-NAME MATCH