contract GoldenPoo is ERC721Enumerable, Ownable, PaymentSplitter {

    using Strings for uint256;

    string public _baseTokenURI = 'https://ipfs.io/ipfs/QmRkzTZoER5hxZVtxt8TwaHX2vGVDtoc8wjGg3SyfKyb8p/';

    string public POO_PROVENANCE = '';
    
    uint256 public startingIndexBlock;

    uint256 public startingIndex;

    uint256 private _price = 0.05 ether;

    uint256 public MAX_TOKENS_PER_TRANSACTION = 20;

    uint256 public MAX_SUPPLY = 1000;

    bool public _paused = true;

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

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

    mapping(uint => string) private _owners;

    event licenseisLocked(string _licenseText);

    // Withdrawal addresses
    address t1 = 0xda73C4DFa2F04B189A7f8EafB586501b4D0B73dC;
    address t2 = 0xD02bf9b3DA78BEc791014EB3cEecA65990cb046F;
    address t3 = 0xE3167f4698862cf2EB9615b6021459C2463349f1;
    address t4 = 0xDa58B66835129C3855fFBe8A01A6733BE391CD74;

    address[] addressList = [t1, t2, t3, t4];
    uint256[] shareList = [30, 40, 40, 40];

    constructor()
    ERC721("Golden Poos", "POO")
    PaymentSplitter(addressList, shareList)  {}

    function mint(uint256 num) public payable {
        uint256 supply = totalSupply();
        require( !_paused,                          "Sale paused" );
        require( num <= MAX_TOKENS_PER_TRANSACTION, "You can mint a maximum of 5 Poos per transation" );
        require( supply + num <= MAX_SUPPLY,        "Exceeds maximum Poo supply" );
        require( msg.value >= _price * num,         "Ether sent is not correct" );

        for(uint256 i; i < num; i++){
            _safeMint( msg.sender, supply + i );
        }

        // If we haven't set the starting index and this is either:
        // 1) the last saleable token or
        // 2) the first token to be sold after the end of pre-sale
        // Set the starting index block
        if (startingIndexBlock == 0 && (totalSupply() == MAX_SUPPLY)) {
            startingIndexBlock = block.number;
        } 
    }

    // Just in case Eth does some crazy stuff
    function setPrice(uint256 _newPrice) public onlyOwner {
        _price = _newPrice;
    }

    function _baseURI() internal view virtual override returns (string memory) {
        return _baseTokenURI;
    }

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

    function getPrice() public view returns (uint256){
        return _price;
    }

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

    function setProvenanceHash(string memory provenanceHash) public onlyOwner {
        POO_PROVENANCE = provenanceHash;
    }
    
    // Returns the license for tokens
    function tokenLicense(uint _id) public view returns(string memory) {
        require(_id < totalSupply(), "Invalid ID");
        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 walletOfOwner(address _owner) public view returns(uint256[] memory) {
        uint256 tokenCount = balanceOf(_owner);

        uint256[] memory tokensId = new uint256[](tokenCount);
        for(uint256 i; i < tokenCount; i++){
            tokensId[i] = tokenOfOwnerByIndex(_owner, i);
        }
        return tokensId;
    }

    /**
     * Set the starting index for the collection
     */
    function setStartingIndex() public {
        require(startingIndex == 0, "Starting index is already set");
        require(startingIndexBlock != 0, "Starting index block must be set");
        
        startingIndex = uint(blockhash(startingIndexBlock)) % MAX_SUPPLY;
        // Just a sanity case in the worst case if this function is called late (EVM only stores last 256 block hashes)
        if ((block.number - startingIndexBlock) > 255) {
            startingIndex = uint(blockhash(block.number - 1)) % MAX_SUPPLY;
        }
    }

    /**
     * Set the starting index block for the collection, essentially unblocking
     * setting starting index
     */
    function emergencySetStartingIndexBlock() public onlyOwner {
        require(startingIndex == 0, "Starting index is already set");
        
        startingIndexBlock = block.number;
    }
}
function lockLicense() public onlyOwner {
    licenseLocked =  true;
    emit licenseisLocked(LICENSE_TEXT);
}

// Locks the license to prevent further changes 