// Contract: BigPunisher
// Target function: function createCollectible(Edition _edition, uint256 _number) external payable
Transfer(address(0), to, tokenId);

        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(address(0), to, tokenId);
    }

    /**
     * @dev Destroys `tokenId`.
     * The approval is cleared when the token is burned.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     *
     * Emits a {Transfer} event.
     */
    function _burn(uint256 tokenId) internal virtual {
        address owner = ERC721.ownerOf(tokenId);

        _beforeTokenTransfer(owner, address(0), tokenId);

        // Clear approvals
        _approve(address(0), tokenId);

        _balances[owner] -= 1;
        delete _owners[tokenId];

        emit Transfer(owner, address(0), tokenId);
    }

    /**
     * @dev Transfers `tokenId` from `from` to `to`.
     *  As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     *
     * Emits a {Transfer} event.
     */
    function _transfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {
        require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
        require(to != address(0), "ERC721: transfer to the zero address");

        _beforeTokenTransfer(from, to, tokenId);

        // Clear approvals from the previous owner
        _approve(address(0), tokenId);

        _balances[from] -= 1;
        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(from, to, tokenId);
    }

    /**
     * @dev Approve `to` to operate on `tokenId`
     *
     * Emits a {Approval} event.
     */
    function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
    }

    /**
     * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
     * The call is not executed if the target address is not a contract.
     *
     * @param from address representing the previous owner of the given token ID
     * @param to target address that will receive the tokens
     * @param tokenId uint256 ID of the token to be transferred
     * @param _data bytes optional data to send along with the call
     * @return bool whether the call correctly returned the expected magic value
     */
    function _checkOnERC721Received(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) private returns (bool) {
        if (to.isContract()) {
            try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) {
                return retval == IERC721Receiver.onERC721Received.selector;
            } catch (bytes memory reason) {
                if (reason.length == 0) {
                    revert("ERC721: transfer to non ERC721Receiver implementer");
                } else {
                    assembly {
                        revert(add(32, reason), mload(reason))
                    }
                }
            }
        } else {
            return true;
        }
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting
     * and burning.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
     * transferred to `to`.
     * - When `from` is zero, `tokenId` will be minted for `to`.
     * - When `to` is zero, ``from``'s `tokenId` will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {}
}

/**
 * @dev ERC721 token with storage based token URI management.
 */
abstract contract ERC721URIStorage is ERC721 {
    using Strings for uint256;

    // Optional mapping for token URIs
    mapping(uint256 => string) private _tokenURIs;

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721URIStorage: URI query for nonexistent token");

        string memory _tokenURI = _tokenURIs[tokenId];
        string memory base = _baseURI();

        // If there is no base URI, return the token URI.
        if (bytes(base).length == 0) {
            return _tokenURI;
        }
        // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
        if (bytes(_tokenURI).length > 0) {
            return string(abi.encodePacked(base, _tokenURI));
        }

        return super.tokenURI(tokenId);
    }

    /**
     * @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
        require(_exists(tokenId), "ERC721URIStorage: URI set of nonexistent token");
        _tokenURIs[tokenId] = _tokenURI;
    }

    /**
     * @dev Destroys `tokenId`.
     * The approval is cleared when the token is burned.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     *
     * Emits a {Transfer} event.
     */
    function _burn(uint256 tokenId) internal virtual override {
        super._burn(tokenId);

        if (bytes(_tokenURIs[tokenId]).length != 0) {
            delete _tokenURIs[tokenId];
        }
    }
}

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        // On the first call to nonReentrant, _notEntered will be true
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;

        _;

        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }
}

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _setOwner(_msgSender());
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _setOwner(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _setOwner(newOwner);
    }

    function _setOwner(address newOwner) private {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

/**
 * @title Counters
 * @author Matt Condon (@shrugs)
 * @dev Provides counters that can only be incremented, decremented or reset. This can be used e.g. to track the number
 * of elements in a mapping, issuing ERC721 ids, or counting request ids.
 *
 * Include with `using Counters for Counters.Counter;`
 */
library Counters {
    struct Counter {
        // This variable should never be directly accessed by users of the library: interactions must be restricted to
        // the library's function. As of Solidity v0.5.2, this cannot be enforced, though there is a proposal to add
        // this feature: see https://github.com/ethereum/solidity/issues/4637
        uint256 _value; // default: 0
    }

    function current(Counter storage counter) internal view returns (uint256) {
        return counter._value;
    }

    function increment(Counter storage counter) internal {
    unchecked {
        counter._value += 1;
    }
    }

    function decrement(Counter storage counter) internal {
        uint256 value = counter._value;
        require(value > 0, "Counter: decrement overflow");
    unchecked {
        counter._value = value - 1;
    }
    }

    function reset(Counter storage counter) internal {
        counter._value = 0;
    }
}


contract BigPunisher is ERC721URIStorage, Ownable, AccessControl, ReentrancyGuard {

    using Counters for Counters.Counter;
    using Strings for uint256;
    using SafeMath for uint256;

    enum Edition{SILVER, GOLD, BLACK, DIAMOND}

    // EVENTS
    event WithdrawnToOwner(address indexed _operator, uint256 _ethWei);
    event WithdrawnToEntities(address indexed _operator, uint256 _ethWei);
    event NftMinted(address whoDone, address airdropAddress, Edition edition, uint256 number);
    event SaleChanged(bool saleOn, bool vipOnly);

    bytes32 public constant VIP_ROLE = keccak256("VIP_ROLE");
    bool public saleOn = false;
    bool public vipSaleOnly = false;
    struct AirDrop {
        address airdropAddress;
        Edition edition;
        uint256 count;
    }
    mapping(address => AirDrop[]) public airDrops;
    AirDrop[] public airDropsArray;

    Counters.Counter private silverCounter;
    Counters.Counter private goldCounter;
    Counters.Counter private blackCounter;
    Counters.Counter private diamondCounter;
    uint256 public maxMintingPerTime = 20;
    string private  _baseTokenURI;

    uint public silverNftCost = 14 * 10 ** 16;
    uint public goldNftCost = 25 * 10 ** 16;
    uint256 public silverSellingMax = 475;
    uint256 public goldSellingMax = 40;
    uint256 public silverSellingCount;
    uint256 public goldSellingCount;
    uint256 public silverAirDropCount;
    uint256 public goldAirDropCount;

    uint256 public silverTotalSupply = 500;
    uint256 public goldTotalSupply = 50;
    uint256 public blackTotalSupply = 5;
    uint256 public diamondTotalSupply = 1;

    uint256 private SILVER_FIRST_ID = 0;
    uint256 private GOLD_FIRST_ID = 500;
    uint256 private BLACK_FIRST_ID = 550;
    uint256 private DIAMOND_FIRST_ID = 555;

    //    Distribution  amount * rate / 1000
    uint256 private CELEBRITY_RATE = 400;
    uint256 private OWNER_RATE = 250;
    uint256 private PORTRAIT_RATE = 125;
    uint256 private THREE_D_RATE = 100;
    uint256 private MARKETING_AND_TECH_RATE = 100;
    uint256 private FRAME_RATE = 25;

    address payable public celebrityAddress = payable(0x0CD258756455D3Efc83B04cC7277B92769884af5);
    address payable public ownerAddress = payable(0xd6d4d7FAf57f22830d978f793d033d115E605962);
    address payable public portraitAddress = payable(0xCEfc1Aacf8579B572FB4ff84E4eD2a8963a6faf8);
    address payable public threeDimensionAddress = payable(0xBf3f449bAB21E2001bF94b105364b525022d88cE);
    address payable public marketingAndTechAddress = payable(0x07D409e34786467F335fF8b7A69e300Effe7E2cf);
    address payable public frameAddress = payable(0xB586D612DC53C9C632e3B039b4D8EdEc028daE70);


    constructor()
    ReentrancyGuard()
    ERC721("Big Punisher", "BIGPUN"){

        setBaseURI("https://ipfs.io/ipfs/QmWmfpcKams3VxJ692wEwE3fp7CKc5nEZmyoZ7Zykt6r3m/");
        _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);

    }


    // Modifiers
    modifier saleIsOn() {
        require(saleOn, "cannot purchase as the sale is off");
        _;
    }

    modifier isLimitedPerTx(uint256 _number) {
        require(_number <= maxMintingPerTime, "exceed the max minting limit per time");
        _;
    }

    modifier canMint(Edition _edition, uint256 _number){
        if (_edition == Edition.DIAMOND) {
            require(owner() == _msgSender(), "Ownable: caller is not the owner");
            require(diamondCounter.current() + _number <= diamondTotalSupply, "exceed the max supply limit for Diamond Edition");
            _;
        } else if (_edition == Edition.BLACK) {
            require(owner() == _msgSender(), "Ownable: caller is not the owner");
            require(blackCounter.current() + _number <= blackTotalSupply, "exceed the max supply limit for Black Edition");
            _;
        } else if (_edition == Edition.GOLD) {
            require(goldCounter.current() + _number <= goldTotalSupply, "exceed the max supply limit for Gold Edition");
            _;
        } else if (_edition == Edition.SILVER) {
            require(silverCounter.current() + _number <= silverTotalSupply, "exceed the max supply limit for Silver Edition");
            _;
        }
    }