contract MotleyDs is ERC721Enumerable, Ownable{
    using Address for address;
    using Strings for uint256;

/**  
* @notice Meta-realist Collectibles, 
* these assets are created with the intention of using art to 
* explore that which is not possible to delve into with the constraints 
* of traditional mechanics, in a reality where fuzziness is a rule, 
* our reality is determined by what we decide to describe and build
* narratives to serve it.  Motley Ds are here to take over narratives 
* and defy the realm of the possible, and build a community while having fun 
* in the process.
*/
    /// Motley Ds are here for the long run, and focused on utility.

    uint256 public constant motleyPrice = 0.1 ether;
    uint public constant maxMotleyPurchase = 15;
    bool public activeSale = false;
    uint256 public _maxMotleys = 10000;
    
    
    address private constant dTreasury = 0xb5713725B6E5E8B719596a3Db218011B1286885c;
    address private constant dArt= 0x1462d5cBFa31733650CF84150136e3aE1573bbb1;
    address private constant dOps = 0x4D166881A15db2E8c1CE2B058424A55234932892;
    
    
    constructor(uint256 totalMotleys) ERC721("Motley Ds", "MOTD"){
        _maxMotleys = totalMotleys;    
    }

 /*
 * With much less we filled San Miguel de Allende with art, brought the Earth Harp to the parroquia.
 * and pretty much blew people's minds.
 * 30% of the proceeds will go to the D-Treasury. 
 * 
 */

    function disburse()public onlyOwner{
        uint balance = address(this).balance;
        payable(msg.sender).transfer(balance*30/100);
        payable(dTreasury).transfer(balance*30/100);
        payable(dArt).transfer(balance*20/100);
        payable(dOps).transfer(balance*20/100);
    }

    function setBaseURI(string memory uri) public onlyOwner {
        _tokenURI = uri;
    }

// The Ds are coming!  the Ds are coming!

    function setSaleState(bool isActive) public onlyOwner {
        activeSale = isActive;
    }


/** 
* Meta-reality is not accessible to all, 
* but the Motley D community is.  
* We are gathering a crew of Motleys
* that will help us have strong footings
* to build our community.
*/

    function setAsideDs() public onlyOwner {
        uint supply = totalSupply();
        uint i;
        for(i = 0; i < 30; i++){
            _safeMint(dTreasury, supply + i);
        }
    }

/** 
*  @notice this: First there is "Geometric Unity", (see Eric Weinstein)
*  then came sound as the first disruption of the base field, 
*  from there light was perceived, that formed matter, 
*  matter requires a sense of self, since it needs to be observed.
*  Self creates narratives, and those narratives create institutions.
*  Wanna crack reality?  Reverse engineer it!
*/

    function releaseTheD(uint numberOfMotleys) public payable{
        require(activeSale, "Sale is not yet open.  Come back later.");
        require(numberOfMotleys <= maxMotleyPurchase, "Reality does everything with 15 particles, you can do with 15 Ds!");
        require(totalSupply() + numberOfMotleys <= _maxMotleys, "Purchase would exceed the total supply of Ds");
        require(motleyPrice*numberOfMotleys<= msg.value, "Wrong amount of Ether sent");
        
        for(uint i = 0; i < numberOfMotleys; i++){
            uint mintIndex = totalSupply();
            if(totalSupply() < _maxMotleys){
                _safeMint(msg.sender, mintIndex);
            }
        }
    }

/// Yeah, only those with a D may enter. 

function verifyDegeneracy(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;
}

/**
* Mimetic Desire is expressed by René Girard as the force that shapes our wants.
* In today's narrative landscape, where very few people have a sense of "self" we
* are constantly trying to find ourselves by gazing towards others.
* Motley Ds Mission is to open the gates of the self by the creation of transformational
* experiences, a large percentage of the funds raised will be destined to that purpose, 
* and 5% of proceeds of secondary sales will also be destined to enhance the value of holding
* a Motley D.
*/

    function contractURI()public pure returns(string memory){
      return "https://ipfs.io/ipfs/bafkreiaxcy7jbtezihcwyb7gq4awp4i26kaxeui7zbog63ngv5lx2ribje";
    }
}
/// @title Motley Ds
/// @author Sergio Martell
/// @notice This work is a compilation of what I considered best practices, "Talent borrows, genious..."
/// @dev Feel free to build on top of this work, we are just midgets sitting in the shoulders of giants. 
/// Special shoutout to Shahzain, thank you brother for helping out!  
function verifyDegeneracy(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;
}
/// Yeah, only those with a D may enter. 