contract KnightsOfChain is ERC721Enumerable, Ownable {
  using Strings for uint256;
  using SafeMath for uint256;

  string private baseURI;
  uint256 public cost = 0.10 ether;
  uint256 public maxSupply = 9999;
  uint256 public maxMintAmount = 2;
  uint256 public nftPerAddressLimit = 2;
  uint256 public nftGiveawayLimit = 1;
  bool public paused = false;
  bool public revealed = false;
  bool public onlyWhitelisted = true;

  mapping(address => uint256) public addressMintedBalance;
  mapping(address=>bool) public whitelistedAddresses;
  mapping(address=>bool) public giveawayAddresses;

  constructor(
    string memory _name,
    string memory _symbol
  ) ERC721(_name, _symbol) {
  }

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

  function mint(uint256 _mintAmount) external payable {
    require(!paused, "the contract is paused");
    uint256 supply = totalSupply();
    uint256 differCost = cost;

    require(_mintAmount > 0, "Need to mint at least 1 NFT");
    require(_mintAmount <= maxMintAmount, "Maximum mint amount per session exceeded - please try again.");
    require(supply.add(_mintAmount) <= maxSupply, "All NFTs have been minted.");

    if (msg.sender != owner()) {

        uint256 ownerMintedCount = addressMintedBalance[msg.sender];
        require(ownerMintedCount.add(_mintAmount) <= nftPerAddressLimit, "You have reached the maximum number of NFTs allowed.");
        
        if(onlyWhitelisted) {
            require(isWhitelisted(msg.sender), "This wallet ID has not been whitelisted");
           
        }

        if(giveawayList(msg.sender) ){
            require(_mintAmount <= nftGiveawayLimit, "We only offer 1 free giveaway per address.");
            giveawayAddresses[msg.sender] = false;
            differCost = 0;
        }

        uint256 totalCost = differCost.mul(_mintAmount);
        require(msg.value >= totalCost, "insufficient funds");
        //return dust eth to the sender
        if(msg.value > totalCost){
          uint256 dustEth = msg.value - totalCost;
          (bool success,) = msg.sender.call{value: dustEth}("");
          require(success);
        }
    }

    addressMintedBalance[msg.sender] = addressMintedBalance[msg.sender].add(_mintAmount);
    
    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(msg.sender, supply.add(i));
    }

  }
  

  
  
  function isWhitelisted(address _user) public view returns (bool) {
      return whitelistedAddresses[_user];
  }
  
  function giveawayList(address _user) public view returns (bool) {
      return giveawayAddresses[_user];
  }

  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)
    external
    view
    virtual
    override
    returns (string memory)
  {
    require(
      _exists(tokenId),
      "ERC721Metadata: URI query for nonexistent token"
    );
    
    if(revealed == false) {
        
        return "ipfs://QmRrYSUH8TZUvkPqxXyMcpAcnLNb6sYtPLbmMp3TtnZMU9/hidden.json";
    }

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

  //only owner
  function reveal() external onlyOwner {
      revealed = true;
  }
  
  function setNftPerAddressLimit(uint256 _limit) external onlyOwner {
    nftPerAddressLimit = _limit;
  }
  
  function setCost(uint256 _newCost) external onlyOwner {
    cost = _newCost;
  }

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

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

  function pause() external onlyOwner {
    paused = !paused;
  }
  
  function setOnlyWhitelisted() external onlyOwner {
    onlyWhitelisted = !onlyWhitelisted;
  }
  
  function addWhitelistUsers(address[] calldata _users) external onlyOwner {
    for(uint i=0; i<_users.length; i++){
        whitelistedAddresses[_users[i]] = true;
    }
  }
  
  function addGiveawayUsers(address[] calldata _users) external onlyOwner {
    for (uint i = 0; i < _users.length; i++) {
      giveawayAddresses[_users[i]] = true;
    }
  }

 
  function withdraw() external payable onlyOwner {
    (bool os, ) = payable(owner()).call{value: address(this).balance}("");
    require(os);
  }
}
function reveal() external onlyOwner {
    revealed = true;
}

//only owner