// File: Free23.sol



// SPDX-License-Identifier: CC0


/*
 /$$$$$$$$ /$$$$$$$  /$$$$$$$$ /$$$$$$$$        /$$$$$$   /$$$$$$
| $$_____/| $$__  $$| $$_____/| $$_____/       /$$__  $$ /$$__  $$
| $$      | $$  \ $$| $$      | $$            |__/  \ $$|__/  \ $$
| $$$$$   | $$$$$$$/| $$$$$   | $$$$$           /$$$$$$/   /$$$$$/
| $$__/   | $$__  $$| $$__/   | $$__/          /$$____/   |___  $$
| $$      | $$  \ $$| $$      | $$            | $$       /$$  \ $$
| $$      | $$  | $$| $$$$$$$$| $$$$$$$$      | $$$$$$$$|  $$$$$$/
|__/      |__/  |__/|________/|________/      |________/ \______/



 /$$
| $$
| $$$$$$$  /$$   /$$
| $$__  $$| $$  | $$
| $$  \ $$| $$  | $$
| $$  | $$| $$  | $$
| $$$$$$$/|  $$$$$$$
|_______/  \____  $$
           /$$  | $$
          |  $$$$$$/
           \______/
  /$$$$$$  /$$$$$$$$ /$$$$$$$$ /$$    /$$ /$$$$$$ /$$$$$$$$ /$$$$$$$
 /$$__  $$|__  $$__/| $$_____/| $$   | $$|_  $$_/| $$_____/| $$__  $$
| $$  \__/   | $$   | $$      | $$   | $$  | $$  | $$      | $$  \ $$
|  $$$$$$    | $$   | $$$$$   |  $$ / $$/  | $$  | $$$$$   | $$$$$$$/
 \____  $$   | $$   | $$__/    \  $$ $$/   | $$  | $$__/   | $$____/
 /$$  \ $$   | $$   | $$        \  $$$/    | $$  | $$      | $$
|  $$$$$$/   | $$   | $$$$$$$$   \  $/    /$$$$$$| $$$$$$$$| $$
 \______/    |__/   |________/    \_/    |______/|________/|__/


CC0 2023
*/


pragma solidity ^0.8.23;


import "./FreeChecker.sol";


interface DancingMan {
  function balanceOf(address, uint256) external returns (uint256);
  function safeTransferFrom(
    address from,
    address to,
    uint256 id,
    uint256 amount,
    bytes memory data
  ) external;
}

contract Free23 is FreeChecker {
  DancingMan public dancingMan = DancingMan(0xC8D1a7814194aa6355727098448C7EE48f2a1e1C);

  uint256 public totalDancingMen;
  mapping(address => uint256) public ownerToStakedDancingMen;
  mapping(uint256 => bool) public free22Used;

  function onERC1155Received(
    address,
    address from,
    uint256 id,
    uint256 amount,
    bytes calldata
  ) external returns (bytes4) {
    require(
      msg.sender == address(dancingMan) && id == 1,
      'Token must be Dancing Man'
    );

    ownerToStakedDancingMen[from] += amount;

    return this.onERC1155Received.selector;
  }

  function withdrawDancingMan(uint256 amount) external {
    require(amount <= ownerToStakedDancingMen[msg.sender], 'Dancing Man withdrawl too large');
    ownerToStakedDancingMen[msg.sender] -= amount;
    dancingMan.safeTransferFrom(address(this), msg.sender, 1, amount, '');
  }


  function claim(uint256 free0TokenId, uint256 free22TokenId) external {
    preCheck(free0TokenId, '23');

    require(dancingMan.balanceOf(address(this), 1) >= 5, 'Dancin Man balance not >= 5');
    checkFreeToken(free22TokenId, 22);
    require(!free22Used[free22TokenId], 'Free22 already used');
    free22Used[free22TokenId] = true;


    postCheck(free0TokenId, 23, '23');
  }

}
// File: FreeChecker.sol




// SPDX-License-Identifier: CC0


/*
CC0 2023
*/


pragma solidity ^0.8.23;

interface IFree {
  function totalSupply() external  view returns (uint256);
  function balanceOf(address) external  view returns (uint256);
  function ownerOf(uint256 tokenId) external view returns (address owner);
  function tokenIdToCollectionId(uint256 tokenId) external view returns (uint256 collectionId);
  function collectionSupply(uint256 collectionId) external view returns (uint256);
  function collectionIdToMinter(uint256 collectionId) external view returns (address);
  function mint(uint256 collectionId, address to) external;
  function appendAttributeToToken(uint256 tokenId, string memory attrKey, string memory attrValue) external;
  function safeTransferFrom(address from, address to, uint256 tokenId) external;
}

abstract contract FreeChecker {
  mapping(uint256 => bool) public free0TokenIdUsed;
  IFree public immutable free = IFree(0x30b541f1182ef19c56a39634B2fdACa5a0F2A741);

  function preCheck(uint256 free0TokenId, string memory freeStr) internal view {
    require(free.tokenIdToCollectionId(free0TokenId) == 0, 'Invalid Free0');
    require(!free0TokenIdUsed[free0TokenId],
      string(abi.encodePacked('This Free0 has already been used to mint a Free', freeStr))
    );
    require(free.ownerOf(free0TokenId) == msg.sender, 'You must be the owner of this Free0');

  }

  function postCheck(uint256 free0TokenId, uint256 freeNumber, string memory freeStr) internal {
    free0TokenIdUsed[free0TokenId] = true;
    free.appendAttributeToToken(free0TokenId,
      string(abi.encodePacked('Used For Free', freeStr, ' Mint')),
      'true'
    );
    free.mint(freeNumber, msg.sender);
  }

  function checkFreeToken(uint256 freeTokenId, uint256 collectionId) internal view {
    require(free.ownerOf(freeTokenId) == msg.sender, 'Not owner of token');
    require(free.tokenIdToCollectionId(freeTokenId) == collectionId, 'Token collection mismatch');
  }
}