contract DegensFarmMetaverseItems is ERC1155, Ownable {
    using Strings for uint256;
    using SafeMath for uint256;

    string constant BASE_METADATA_URL = "https://degens.farm/meta/metaverseitems/";

    struct Inventory {
        uint256 priceDung;
        uint256 leftCount;
    }

    address public dungERC20;
    // Token id to Properties
    mapping(uint256 => Inventory) public inventoryProperties;
    uint constant MIN_ID = 6;
    uint max_id = 0;

    /**
     * @dev we use uri_ here for Compatibility with
     * https://eips.ethereum.org/EIPS/eip-1155#metadata
     *
     */
    constructor (string memory uri_, address _dungToken)
        ERC1155(uri_)
    {
        dungERC20 = _dungToken;
        _register(MIN_ID,       5e14 ether, 333);
        _register(MIN_ID + 1,   5e14 ether, 333);
        _register(MIN_ID + 2,   5e14 ether, 333);
    }

    function _register(uint256 _type, uint price, uint count) internal {
        address target = address(this);
        inventoryProperties[_type].priceDung = price;
        inventoryProperties[_type].leftCount = count;
        if (max_id < _type) {
            max_id = _type;
        }
    }

    function register(uint256 _type, uint price, uint count) external onlyOwner {
        _register(_type, price, count);
    }

    function mint(uint256 _type, uint count, address target) external onlyOwner {
        _mint(target, uint8(_type), count, "");
    }

    function dungSwap(uint8 _type) external {
        require(inventoryProperties[_type].leftCount > 0, "No items left in store");
        uint256 price = inventoryProperties[_type].priceDung;
        inventoryProperties[_type].leftCount--;
        //require(IERC20(dungERC20).balanceOf(msg.sender) >= price, "Insufficient DUNG!");
        IERC20(dungERC20).transferFrom(msg.sender, address(this), price);
        IDungBurn(dungERC20).burn(price);
        _mint(msg.sender, _type, 1, "");
    }

    /*
     * @dev this is not full standard bot common use of 1155 implementation
     */
    function uri2(uint256 _id) external view  returns (string memory) {
        return string(abi.encodePacked(BASE_METADATA_URL, _id.toString()));
    }

    function getMarketInfo() external view returns (uint[] memory result) {
        result = new uint[]((max_id - MIN_ID + 1)*3);
        uint j = 0;
        for (uint256 i = MIN_ID; i <= max_id; i++) {
            result[j*3 + 0] = i;
            result[j*3 + 1] = inventoryProperties[i].priceDung;
            result[j*3 + 2] = inventoryProperties[i].leftCount;
            j++;
        }
    }

}
function uri2(uint256 _id) external view  returns (string memory) {
    return string(abi.encodePacked(BASE_METADATA_URL, _id.toString()));
}

/*
 * @dev this is not full standard bot common use of 1155 implementation
 */