contract MetaJoseon is ERC721Enumerable, Ownable {
  using Strings for uint256;

  string public baseURI;
  string public baseExtension = ".json";
  uint256 public saleState = 1;
  string public notRevealedUri;
  uint256 public ogMaxSupply = 250;
  uint256 public wlMaxSupply = 3250;
  uint256 public pMaxSupply = 8941;
  bool public revealed = false;
  address[] public ogWhitelistedAddresses;
  address[] public wlWhitelistedAddresses;
  uint256 public ogCost = 0 ether;
  uint256 public wlCost = 0.06 ether;
  uint256 public pCost = 0.08 ether;
  uint256 public ogNftPerAddressLimit = 1;
  uint256 public wlNftPerAddressLimit = 3;
  uint256 public pNftPerAddressLimit = 5;
  mapping(address => uint256) public addressMintedBalance;

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri,
    address[] memory _ogWhitelistedAddresses,
    address[] memory _wlWhitelistedAddresses
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
    _setOgwhitelistUsers(_ogWhitelistedAddresses);
    _setWlwhitelistUsers(_wlWhitelistedAddresses);
  }

  // internal
  function _setOgwhitelistUsers(address[] memory _users) private onlyOwner {
    delete ogWhitelistedAddresses;
    ogWhitelistedAddresses = _users;
  }

  function _setWlwhitelistUsers(address[] memory _users) private onlyOwner {
    delete wlWhitelistedAddresses;
    wlWhitelistedAddresses = _users;
  } 
  
  function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

  // public
  function mint(uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    uint256 ownerMintedCount = addressMintedBalance[msg.sender];
    require(_mintAmount > 0, "need to mint at least 1 NFT");

    if(msg.sender != owner()){
        //Stop
        require(saleState != 0, "the contract is paused");

        //og
        if(saleState == 1){
        require(isOgWhitelisted(msg.sender), "user is not whitelisted");
        require(ownerMintedCount + _mintAmount <= ogNftPerAddressLimit, "max NFT per address exceeded");
        require(msg.value >= ogCost * _mintAmount, "insufficient funds");
        require(supply + _mintAmount <= ogMaxSupply, "max NFT limit exceeded");
        //wl
        }else if(saleState == 2){
        if(isOgWhitelisted(msg.sender)){
            require(ownerMintedCount + _mintAmount <= ogNftPerAddressLimit + wlNftPerAddressLimit, "max NFT per address exceeded");
        }else{
            require(isWlWhitelisted(msg.sender), "user is not whitelisted");
            require(ownerMintedCount + _mintAmount <= wlNftPerAddressLimit, "max NFT per address exceeded");
        }
        require(msg.value >= wlCost * _mintAmount, "insufficient funds");
        require(supply + _mintAmount <= wlMaxSupply, "max NFT limit exceeded");

        //public
        }else if(saleState == 3){
        if(isOgWhitelisted(msg.sender)){
            require(ownerMintedCount + _mintAmount <= ogNftPerAddressLimit + wlNftPerAddressLimit + pNftPerAddressLimit, "max NFT per address exceeded");
        }else if(isWlWhitelisted(msg.sender)){
            require(ownerMintedCount + _mintAmount <= wlNftPerAddressLimit + pNftPerAddressLimit, "max NFT per address exceeded");
        }else{
            require(ownerMintedCount + _mintAmount <= pNftPerAddressLimit, "max NFT per address exceeded");
        }
        require(msg.value >= pCost * _mintAmount, "insufficient funds");
        require(supply + _mintAmount <= pMaxSupply, "max NFT limit exceeded");
        }
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      addressMintedBalance[msg.sender]++;
      _safeMint(msg.sender, supply + i);
    }
  }
  
  function isOgWhitelisted(address _user) public view returns (bool) {
    for (uint i = 0; i < ogWhitelistedAddresses.length; i++) {
      if (ogWhitelistedAddresses[i] == _user) {
          return true;
      }
    }
    return false;
  }

  function isWlWhitelisted(address _user) public view returns (bool) {
    for (uint i = 0; i < wlWhitelistedAddresses.length; i++) {
      if (wlWhitelistedAddresses[i] == _user) {
          return true;
      }
    }
    return false;
  }

  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"
    );
    
    if(revealed == false) {
        return notRevealedUri;
    }

    string memory currentBaseURI = _baseURI();
    return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
        : "";
  }

  //only owner
  function reveal() public onlyOwner {
      revealed = true;
  }

  function setOgwhitelistUsers(address[] calldata _users) public onlyOwner {
    delete ogWhitelistedAddresses;
    ogWhitelistedAddresses = _users;
  }

  function setWlwhitelistUsers(address[] calldata _users) public onlyOwner {
    delete wlWhitelistedAddresses;
    wlWhitelistedAddresses = _users;
  }

  function setOgCost(uint256 _newCost) public onlyOwner {
    ogCost = _newCost;
  }

  function setWlCost(uint256 _newCost) public onlyOwner {
    wlCost = _newCost;
  }

  function setPCost(uint256 _newCost) public onlyOwner {
    pCost = _newCost;
  }

  function setOgNftPerAddressLimit(uint256 _limit) public onlyOwner {
    ogNftPerAddressLimit = _limit;
  }

  function setWlNftPerAddressLimit(uint256 _limit) public onlyOwner {
    wlNftPerAddressLimit = _limit;
  }

  function setPNftPerAddressLimit(uint256 _limit) public onlyOwner {
    pNftPerAddressLimit = _limit;
  }

  function setSaleState(uint256 _state) public onlyOwner {
    saleState = _state;
  }

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

  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }
  
  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  } 
 
  function withdraw() public payable onlyOwner {
    (bool os, ) = payable(owner()).call{value: address(this).balance}("");
    require(os);
  }
}
function _setOgwhitelistUsers(address[] memory _users) private onlyOwner {
    delete ogWhitelistedAddresses;
    ogWhitelistedAddresses = _users;
  }
// internal