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

    string public Dalmatians_PROVENANCE = "";

    string public LICENSE_TEXT = "";

    bool licenseLocked = false;

    uint256 public Price = 10000000000000000; // 0.01 ETH

    uint256 public constant MAX_Purchase = 50;

    uint256 public constant MAX_Supply = 10001;

    bool public saleIsActive = false;

    event licenseisLocked(string _licenseText);

    constructor() ERC721("Dalmatians", "Dal") {}

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

    function reserve() public onlyOwner {
        uint supply = totalSupply();
        uint i;
        for (i = 0; i < 20; i++) {
            _safeMint(msg.sender, supply + i);
            }
    }
    
    function setProvenanceHash(string memory provenanceHash) public onlyOwner {
        Dalmatians_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 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(uint256 _id) public view returns (string memory) {
        require(_id < totalSupply(), "CHOOSE A Dalmatian 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 mint(uint256 numberOfTokens) public payable {
        require(saleIsActive, "Sale must be active to mint Dalmatians");
        require(numberOfTokens > 0 && numberOfTokens <= MAX_Purchase, "Can only mint 50 tokens at a time");
        require(totalSupply().add(numberOfTokens) <= MAX_Supply, "Purchase would exceed max supply of Dalmatians");
        require(msg.value >= Price.mul(numberOfTokens), "Ether value sent is not correct");

        for (uint256 i = 0; i < numberOfTokens; i++) {
             uint256 mintIndex = totalSupply();
             if (totalSupply() < MAX_Supply) {
                 _safeMint(msg.sender, mintIndex);
             }
        }
    }

    function setPrice(uint256 newPrice) public onlyOwner {
        Price = newPrice;
    }
}
function changeLicense(string memory _license) public onlyOwner {
    require(licenseLocked == false, "License already locked");
    LICENSE_TEXT = _license;
}

// Change the license