contract CuddleCacti is ERC721, Ownable {
    
    using SafeMath for uint256;

    string public CACTI_PROVENANCE = ""; // IPFS URL WILL BE ADDED WHEN CACTI 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 cactiPrice = 30000000000000000; // 0.03 ETH

    uint public constant maxCactiPurchase = 20;

    uint256 public constant MAX_CACTI = 4444;
    
    uint public constant maxSignatures = 144;
    
    uint public existingSignatures = 0;

    bool public saleIsActive = false;
    
    mapping(uint => string) public cactusNames;
    
    // Reserve 300 Cacti for team - Giveaways/Prizes etc
    uint public cactiReserve = 300;
    
    event cactusNameChange(address _by, uint _tokenId, string _name);
    
    event licenseisLocked(string _licenseText);

    constructor() ERC721("Cuddle Cacti", "CDDL") { }
    
    function withdraw() public onlyOwner {
        uint balance = address(this).balance;
        msg.sender.transfer(balance);
    }
    
    function reserveCacti(address _to, uint256 _reserveAmount) public onlyOwner {        
        uint reserveIndex = (totalSupply() - existingSignatures);
        require(_reserveAmount > 0 && _reserveAmount <= cactiReserve && totalSupply() < MAX_CACTI + _reserveAmount, "Not enough reserve left for team or Max Cacti Reached");
        for (uint i = 0; i < _reserveAmount; i++) {
            _safeMint(_to, reserveIndex + i);
        }
        cactiReserve = cactiReserve.sub(_reserveAmount);
    }


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

    function setBaseURI(string memory baseURI) public onlyOwner {
        _setBaseURI(baseURI);
    }
    function addSignatureTokens(address _to, uint256 numberOfSignatures) public onlyOwner{
        require(existingSignatures + numberOfSignatures < maxSignatures);
        for (uint i = 0; i < numberOfSignatures; i++) {
            existingSignatures++;
            _safeMint(_to, MAX_CACTI + existingSignatures);
        }
    }

    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 CACTUS 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 mintCuddleCactus(uint numberOfTokens) public payable {
        require(saleIsActive, "Sale must be active to mint Cactus");
        require(numberOfTokens > 0 && numberOfTokens <= maxCactiPurchase, "Can only mint 20 cacti at a time");
        require(totalSupply().add(numberOfTokens) - existingSignatures <= MAX_CACTI, "Purchase would exceed max supply of cacti");
        require(msg.value >= cactiPrice.mul(numberOfTokens), "Ether value sent is not correct");
        
        for(uint i = 0; i < numberOfTokens; i++) {
            uint mintIndex = (totalSupply() - existingSignatures);
            if (totalSupply() - existingSignatures < MAX_CACTI) {
                _safeMint(msg.sender, mintIndex);
            }
        }

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