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

    string public PROVENANCE_HASH = ""; // PROVENANCE HASH WILL BE ADDED WHEN NFTS ARE ALL SOLD OUT
    
    string public LICENSE_TEXT = ""; 
    
    bool licenseLocked = false; 

    uint256 public constant price = 66600000000000000; // 0.0666 ETH

    uint public constant maxPurchase = 20;
    
    uint256 public constant MAX_SUPPLY = 7666;

    uint256 public constant PRESALE_SUPPLY = 1000;

    bool public saleIsActive = false;

    bool public presaleIsActive = false;
    
    // Reserve 75 for giveaways
    uint public reserveSupply = 100;
    
    //payout wallets

    address private constant ADDR1_14 = 0xe9794038c402Cc38C90c39769434Dc0e55E96A63;
    address private constant ADDR2_14 = 0xbc78E58c65a58C04B42BCb1e35a1C07f2057Bc04;
    address private constant ADDR3_18 = 0x6e38cAf9484d1544F7dAa781eCC899Bbf5e10E27;
    address private constant ADDR4_12 = 0xC27f2F8Adf67f8bce6b3a1dB5192fF3Ac845cAc0;
    address private constant ADDR5_9 = 0x1E82E5fF137D4e0ecf3589e9fB03428155Ffbfd5;
    address private constant ADDR6_26 = 0x94B595db6368df0CDe0a087E319c9FED897F0187;
    address private constant ADDR7_7 = 0x572F9139Cfc5B3CF0664778bcB5fC9D3828aC78B;

    event licenseisLocked(string _licenseText);

    constructor() ERC721("CryptoClowns", "CLOWN") { }
    
    function withdraw() public onlyOwner {
        uint balance = address(this).balance;
        msg.sender.transfer(balance);
    }
    
    function withdrawAll() public payable onlyOwner {
        uint256 balance = address(this).balance;
        require(balance > 0);
        _widthdraw(ADDR1_14, balance.mul(14).div(100));
        _widthdraw(ADDR2_14, balance.mul(14).div(100));
        _widthdraw(ADDR3_18, balance.mul(18).div(100));
        _widthdraw(ADDR4_12, balance.mul(12).div(100));
        _widthdraw(ADDR5_9, balance.mul(9).div(100));
        _widthdraw(ADDR6_26, balance.mul(26).div(100));
        _widthdraw(ADDR7_7, balance.mul(7).div(100));
    }

    function _widthdraw(address _address, uint256 _amount) private {
        (bool success,) = _address.call{value : _amount}("");
        require(success, "Transfer failed.");
    }
    
    function reserve(address _to, uint256 _reserveAmount) public onlyOwner {        
        uint supply = totalSupply();
        require(_reserveAmount > 0 && _reserveAmount <= reserveSupply, "Not enough reserve left for team");
        for (uint i = 0; i < _reserveAmount; i++) {
            _safeMint(_to, supply + i);
        }
        reserveSupply = reserveSupply.sub(_reserveAmount);
    }

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

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

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

    function flipPresaleState() public onlyOwner {
        presaleIsActive = !presaleIsActive;
    }
    
        
    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 TOKEN 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(uint numberOfTokens) public payable {
        require(saleIsActive, "Sale must be active to mint");
        require(numberOfTokens > 0 && numberOfTokens <= maxPurchase, "Can only mint 20 tokens at a time");
        require(totalSupply().add(numberOfTokens) <= MAX_SUPPLY, "Purchase would exceed max supply");
        require(msg.value >= price.mul(numberOfTokens), "Ether value sent is not correct");
        
        for(uint i = 0; i < numberOfTokens; i++) {
            uint mintIndex = totalSupply();
            if (totalSupply() < MAX_SUPPLY) {
                _safeMint(msg.sender, mintIndex);
            }
        }
    }

    function mintPresale(uint numberOfTokens) public payable {
        require(presaleIsActive, "Presale must be active to mint");
        require(numberOfTokens > 0 && numberOfTokens <= maxPurchase, "Can only mint 20 tokens at a time");
        require(totalSupply().add(numberOfTokens) <= PRESALE_SUPPLY, "Purchase would exceed presale supply");
        require(msg.value >= price.mul(numberOfTokens), "Ether value sent is not correct");
        
        for(uint i = 0; i < numberOfTokens; i++) {
            uint mintIndex = totalSupply();
            if (totalSupply() < PRESALE_SUPPLY) {
                _safeMint(msg.sender, mintIndex);
            }
        }
    }
}
function changeLicense(string memory _license) public onlyOwner {
        require(licenseLocked == false, "License already locked");
        LICENSE_TEXT = _license;
    }
// Change the license