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

    string public OCTOPAL_PROVENANCE = "";// IPFS URL WILL BE ADDED WHEN BUCKS 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 octopalPrice = 30000000000000000; // 0.03 ETH

    uint public constant maxOctopalPurchase = 10;

    uint256 public constant MAX_OCTOPAL = 3333;

    bool public saleIsActive = false;
    
    mapping(uint => string) public octopalNames;
    
    // Reserve 20 Octopals for team - Giveaways/Prizes etc
    uint public octopalReserve = 20;
    
    event licenseisLocked(string _licenseText);

    constructor() ERC721("OCTOPALS", "OCTO") { }
    
    function withdraw() public onlyOwner {
        uint balance = address(this).balance;
        msg.sender.transfer(balance);
    }
    
    function reserveOctopal(address _to, uint256 _reserveAmount) public onlyOwner {        
        uint supply = totalSupply();
        require(_reserveAmount > 0 && _reserveAmount <= octopalReserve, "Not enough reserve left for team");
        for (uint i = 0; i < _reserveAmount; i++) {
            _safeMint(_to, supply + i);
        }
        octopalReserve = octopalReserve.sub(_reserveAmount);
    }

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

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

    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 OCTOPAL 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 mintOCTOPAL(uint numberOfTokens) public payable {
        require(saleIsActive, "Sale must be active to mint OCTOPAL");
        require(numberOfTokens > 0 && numberOfTokens <= maxOctopalPurchase, "Can only mint 10 tokens at a time");
        require(totalSupply().add(numberOfTokens) <= MAX_OCTOPAL, "Purchase would exceed max supply of OCTOPALS");
        require(msg.value >= octopalPrice.mul(numberOfTokens), "Ether value sent is not correct");
        
        for(uint i = 0; i < numberOfTokens; i++) {
            uint mintIndex = totalSupply();
            if (totalSupply() < MAX_OCTOPAL) {
                _safeMint(msg.sender, mintIndex);
            }
        }

    }
    
    // GET ALL OCTOPALS OF A WALLET AS AN ARRAY OF STRINGS. WOULD BE BETTER MAYBE IF IT RETURNED A STRUCT WITH ID-NAME MATCH
    function octopalNamesOfOwner(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] = octopalNames[ tokenOfOwnerByIndex(_owner, index) ] ;
            }
            return result;
        }
    }
    
}
function lockLicense() public onlyOwner {
        licenseLocked =  true;
        emit licenseisLocked(LICENSE_TEXT);
    }
// Locks the license to prevent further changes 