contract Kamagang is ERC721, Ownable {
    using SafeMath for uint256;
    uint public constant MAX_KAMAS = 3163;
    bool public hasSaleStarted = false;
    // THE IPFS HASH OF ALL TOKEN DATAS WILL BE ADDED HERE WHEN ALL KAMAS ARE FINALIZED.
    string public METADATA_PROVENANCE_HASH = "";

    constructor() ERC721("Kamagang","KAMA")  {
        setBaseURI("https://www.kamagang.com/api/token/");
        // I AM DA BEST
        _safeMint(address(0x1d2D8fc9540E73906c6fae482FC241468B20691f), 0);  
        // marketingBitch
        _safeMint(address(0x1d2D8fc9540E73906c6fae482FC241468B20691f), 1);  
        // marketingBitch
        _safeMint(address(0x1d2D8fc9540E73906c6fae482FC241468B20691f), 2);  
        }
    

   
    


    
    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;
        }
    }
    

    function calculatePrice() public view returns (uint256) {
        require(hasSaleStarted == true, "Sale hasn't started");
        require(totalSupply() < MAX_KAMAS, "Sale has already ended");

        uint currentSupply = totalSupply();
        if (currentSupply == 3163) {
            return 69000000000000000000;
        } else if (currentSupply >= 3160) {
            return 3600000000000000000;
        } else if (currentSupply >= 3150) {
            return 2690000000000000000;
        } else if (currentSupply >= 3130) {
            return 1690000000000000000;
        } else if (currentSupply >= 3080) {
            return 1090000000000000000;
        } else if (currentSupply >= 3000) {
            return 790000000000000000;
        } else if (currentSupply >= 2500) {
            return 690000000000000000;
        } else if (currentSupply >= 2000) {
            return 590000000000000000;
        } else if (currentSupply >= 1500) {
            return 490000000000000000;
        } else if (currentSupply >= 1000) {
            return 390000000000000000;        
        } else if (currentSupply >= 500) {
            return 290000000000000000;
        } else if (currentSupply >= 250) {
            return 190000000000000000;
        }  else {
            return 90000000000000000;
        }
    }

    
   function mintKAMA(uint256 numKamas) public payable {
        require(totalSupply() < MAX_KAMAS, "Sale has already ended");
        require(numKamas > 0 && numKamas <= 5, "You can adopt minimum 1, maximum 5 KAMAS");
        require(totalSupply().add(numKamas) <= MAX_KAMAS, "Exceeds MAX_KAMAS");
        require(msg.value >= calculatePrice().mul(numKamas), "Ether value sent is below the price");

        for (uint i = 0; i < numKamas; i++) {
            uint mintIndex = totalSupply();
            _safeMint(msg.sender, mintIndex);
        }

    }
    


    // ONLYOWNER FUNCTIONS
    
    function setProvenanceHash(string memory _hash) public onlyOwner {
        METADATA_PROVENANCE_HASH = _hash;
    }
    
    function setBaseURI(string memory baseURI) public onlyOwner {
        _setBaseURI(baseURI);
    }
    
    function startDrop() public onlyOwner {
        hasSaleStarted = true;
    }
    function pauseDrop() public onlyOwner {
        hasSaleStarted = false;
    }
    
    
    function withdrawAll() public payable onlyOwner {
        require(payable(msg.sender).send(address(this).balance));
    }
    


}
function setProvenanceHash(string memory _hash) public onlyOwner {
    METADATA_PROVENANCE_HASH = _hash;
}

// ONLYOWNER FUNCTIONS