contract MidgetMonsters is ERC721Enumerable, Ownable {
    PixelDoorsv2 bridge = PixelDoorsv2(0x9fBe628683527bc7Be4C31472BCB0c2c6eC2dCCa);
    
    //
     using Strings for uint256;

    string public baseURI;
    string public baseExtension = ".json";
    uint256 public cost = 0.04 ether;
    uint256 public maxSupply = 10000;
    uint256 public maxMintAmount = 20;
    uint256 public nftPerAddressLimit = 100;
    bool public paused = false;
    mapping(address => uint256) public addressMintedBalance;

    constructor(
        string memory _name,
        string memory _symbol,
        string memory _initBaseURI
     )  ERC721(_name, _symbol) {
        setBaseURI(_initBaseURI);
        mint(msg.sender, 1);
        Reward_Special_PixelDoors_Owners ();
        paused = true;
  }
    
    function IsSenderPixelDoorOwner (address _user) private view returns (bool) {
        uint256[] memory values = bridge.walletOfOwner(_user);
        if (values.length > 0) { 
            return true;
        }
        else {
            return false;
        }
    }

    function IsThisAdressAPixelDoorOwner (address Provided_Address_To_Check) public view returns (bool) {
        return IsSenderPixelDoorOwner(Provided_Address_To_Check);
    }
    
    function Reward_Special_PixelDoors_Owners () private { //Airdropping special doors as promised (only 2.5k minted at time of launch)
        mint(bridge.ownerOf(1),1);
        mint(bridge.ownerOf(1000),1);
        mint(bridge.ownerOf(2000),1);
        mint(bridge.ownerOf(2325),1); //Community helper
    }
  // internal
  function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

  // public
  function mint(address _user, uint256 _mintAmount) public payable {
    require(!paused, "The contract is paused!");
    uint256 supply = totalSupply();
    if (IsSenderPixelDoorOwner(_user)) {
        if (supply <= 200) {
            cost = 0 ether;
            maxMintAmount = 1;
            nftPerAddressLimit = 3;
        }
        else {
        cost = 0.02 ether;
        maxMintAmount = 20;
        nftPerAddressLimit = 100;
        }
    }
    else {
    cost = 0.04  ether;
    maxMintAmount = 20;
    nftPerAddressLimit = 100;
    }
    require(_mintAmount > 0, "Need to mint at least 1 NFT");
    require(_mintAmount <= maxMintAmount, "Max mint amount per session exceeded");
    require(supply + _mintAmount <= maxSupply, "Max NFT limit exceeded");

    if (msg.sender != owner()) {
        uint256 ownerMintedCount = addressMintedBalance[_user];
        require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "Max NFT per address exceeded");
        require(msg.value >= cost * _mintAmount, "Insufficient funds");
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      addressMintedBalance[_user]++;
      _safeMint(_user, supply + i);
    }
  }

  function walletOfOwner(address _owner)
    public
    view
    returns (uint256[] memory)
  {
    uint256 ownerTokenCount = balanceOf(_owner);
    uint256[] memory tokenIds = new uint256[](ownerTokenCount);
    for (uint256 i; i < ownerTokenCount; i++) {
      tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
    }
    return tokenIds;
  }

  function tokenURI(uint256 tokenId)
    public
    view
    virtual
    override
    returns (string memory)
  {
    require(
      _exists(tokenId),
      "ERC721Metadata: URI query for nonexistent token"
    );
    
    string memory currentBaseURI = _baseURI();
    return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
        : "";
  }

  //only owner
  function setNftPerAddressLimit(uint256 _limit) public onlyOwner() {
    nftPerAddressLimit = _limit;
  }
  
  function setCost(uint256 _newCost) public onlyOwner() {
    cost = _newCost;
  }

  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner() {
    maxMintAmount = _newmaxMintAmount;
  }

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

  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }
  function pause(bool _state) public onlyOwner {
    paused = _state;
  }
 
  function withdraw() public payable onlyOwner {
    (bool success, ) = payable(msg.sender).call{value: address(this).balance}("");
    require(success);
  }
}
function mint(address _user, uint256 _mintAmount) public payable {
  require(!paused, "The contract is paused!");
  uint256 supply = totalSupply();
  if (IsSenderPixelDoorOwner(_user)) {
      if (supply <= 200) {
          cost = 0 ether;
          maxMintAmount = 1;
          nftPerAddressLimit = 3;
      }
      else {
      cost = 0.02 ether;
      maxMintAmount = 20;
      nftPerAddressLimit = 100;
      }
  }
  else {
  cost = 0.04  ether;
  maxMintAmount = 20;
  nftPerAddressLimit = 100;
  }
  require(_mintAmount > 0, "Need to mint at least 1 NFT");
  require(_mintAmount <= maxMintAmount, "Max mint amount per session exceeded");
  require(supply + _mintAmount <= maxSupply, "Max NFT limit exceeded");

  if (msg.sender != owner()) {
      uint256 ownerMintedCount = addressMintedBalance[_user];
      require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "Max NFT per address exceeded");
      require(msg.value >= cost * _mintAmount, "Insufficient funds");
  }

  for (uint256 i = 1; i <= _mintAmount; i++) {
    addressMintedBalance[_user]++;
    _safeMint(_user, supply + i);
  }
}

// public