// File: seaavatars.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

interface SeaInterface {
    function ownerOf(uint256 tokenId) external view returns (address owner);
    function tokenOfOwnerByIndex(address owner, uint256 index)
        external
        view
        returns (uint256 tokenId);
}

contract SeaAvatars is ERC721, ERC721Enumerable, Ownable {
    bool public saleIsActive = false;
    bool public burned = false;
    address public SeaAddress = 0xccB6E4a1c42F4892cdE27A8bC2e50bbA0b43d224;
    SeaInterface SeaContract = SeaInterface(SeaAddress);
    
    constructor() ERC721("SeaAvatars", "SEAV") {
    }
     

    function _beforeTokenTransfer(address from, address to, uint256 tokenId)
        internal
        override(ERC721, ERC721Enumerable)
    {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
    
    function random(string memory input) internal pure returns (uint256) {
        return uint256(keccak256(abi.encodePacked(input)));
    }
    
    function toHashCode(uint256 value) internal pure returns (string memory) {
        uint256 i;

        bytes memory buffer = "000000";
        for(i=6;i>0;i--) {
            if (value % 16 < 10)
                buffer[i-1] = bytes1(uint8(48 + uint256(value % 16)));
            else
                buffer[i-1] = bytes1(uint8(55 + uint256(value % 16)));

            value /= 16;
        }
        return string(buffer);
    }

    function toString(uint256 value) internal pure returns (string memory) {
        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (value != 0) {
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
            value /= 10;
        }
        return string(buffer);
    }
    
    function getPolygon(uint256 num, string memory color, string memory op) internal pure returns (string memory) {
        uint256[180]memory xtypes =[
            4814428123379440877112789653708048739232650899662072877855038155387987431215,
            134090543,
            3213740837251487480304978222492611373843034917926543139373749086079167363288,
            3665448596379561495266568050510573499255299605025824809279735116735304552088,
            205484395792065916098301401717170874304911595874256154464498320,
            4978496874583640837093842564837635911112988777710182783287306338547366718674,
            2847423534786952485829505879377973299768746716665431702559283142857412987502,
            35140135382753,
            2990030449756037583316377534683801160820975666322594,
            633147047751871463082639347446,
            633250043510582197225664685096,
            783342371240660400615497362662867695169835591505009495153,
            3015605336097770010832060948773439899320263180355111271447081940574438532253,
            1699410383171683060484640923082407994735009912351540698902523680158858963667,
            2067439483755009076537239878613967120688171739225650558832839415147437291640,
            2110285635025633850782162448438909787314249243868635101044081548977791378060,
            1813537826634847775588271020849619009466237369690820983365919405283580390552,
            1433165981178011733008463800384150766019695303333840710976987787175893725835,
            1347834300580146900593882219877353796923237396140869764780512560971696623306,
            1588291142145332360308884532736805401102486146533600217899801854658095932103,
            1630640689404733378642505262896486732354314158877471864006247217340780175548,
            1926975200976366162727790765948535744022368363806555952276809889580033045183,
            2195729526611169200073088264115405134266050171528327914526689757427595025581,
            2266183416249769109687683253195421600011544355208010500246994655874713467054,
            1942021118171089200927872160880968143467387698704835635209143544230560220841,
            2026692120635480416902954766191778704951595844413918284067846871351766098126,
            2252931742901068372445672611749832774842961514843407512141469648329648975563,
            2295116187662235110121913666276726987468444668928175066731688914204036246718,
            2592194794286804437698157144159526339621523043225693230464121704378048799436,
            2535270053559044811589912991644676175666664223951765667507672669824291794632,
            2676810635330829446627987247531416732227338364894945326305024218158728837315,
            2676507767503271220336019071355421318369328191959582692781556145107637730497,
            2860260185232810836874932161100098492177796172019734599621174229889736083643,
            2704583530546838355578889545793450502804454913237751059045251025304634622638,
            2732605051757189469738771911402128307348469092831311049021474868234958504110,
            2689896882341260570152089198804438177341639211772304451963188924944556062881,
            2874339369183394173601732921210730487262146816407635719706621996970437346462,
            134063278,
            11401402070285144550100871971896250052146431669,
            11401442479798540347354564667579758730284697744,
            2010405393385468769754390500839040943260313875155174100691674881837603800274,
            53852718675364590822474638051961946119679634989037219218611160268927,
            2873152535253068203115307214512935299514021674337530122812293448446001082573,
            632967951439172729344733328545,
            2703590214691199062276704278272662481619631230524746364075007852563519075527,
            3142100272699584422061276135492595947566116804089604574541184736671832567449,
            2989437538194262125706135904879775746659906037333659,
            2732882416327165001845161696611165857660612391574224505373154795372861555376,
            511,
            2407752739435277468182135965268740961167067564356323244993211865623229981385,
            2718635756548816077094781045246157570894954467225593574078410329624519202986,
            205426318550813624678543263541887184093338878840179569528765611,
            783755749980388114187626910565273421423263977749007664810,
            2802230349547862087207944134484874423122711422731942543155255483070880492751,
            4033286330948968342802066104321959696267657300291049256998759985275710840448,
            2989426410341552234246661906532089360279329427827888,
            2703203069435399854974180652086783188110879736547100033428855629723775884484,
            3326681766720166569802217709619400280322460235055753081581802918103831117954,
            3736343546743753260211845024466212803851390170907039772609456547994386951852,
            205409056410469613002004354563412766279958914452316885766240416,
            1971616190158006219705968645019532586535831259808819755347658143630318153833,
            11399522681441707448144466669239173253050471147,
            1914414298019550117063508162120147874407892009683649735561803866455306738423,
            35134101066472,
            1205906187159667043478116698855989167391587791278914760228820903963052253383,
            9208186077033047238,
            1108535604507764213683166814513984193935977038616812534071790246938547877087,
            511,
            165898217922643309084526052680454256,
            1276275803054313493599806570260748153354263436358400765083857487700048243417,
            1178822342572881599758773592983319479796300992156451348075390679526609297106,
            9207483898066677967,
            740975772651252457144836420278673397610254157186597222250099236134980751977,
            2414117941576320369311410,
            632708923678708594065717615261,
            1458925500838540507418926026561993567301703685253672786264170589214896329935,
            1362215544816844226291935252053426049015218768525540613182698930612617390203,
            205358079224472907756417989573029084098844703882026101090076869,
            2405738554033416632426971367221281326733416003252598059526135764720845880057,
            205367565356852077129926225204268878140292779083361308042405991,
            2094799621356768638422789955858255696415450532913632148607982003334359841527,
            216385665523705520674157202371983031175832580637483984829524597222897451627,
            2414056423330995164691159,
            11397700635544049811140143993956400775937410739,
            1953753321857745697879799949839981476218506366288294591805006875595925160150,
            2414313723860810707829366,
            2413834197074852568406235,
            1007024817493647344034072868464098635223782697202574437333574124131465880276,
            978311935401717486461471199063926706147737078905916884262441267563763117176,
            1744463536117297771199500556841754719200382694378654872668579678780200630368,
            511,
            14112961311574784031995581858176719375774695975355569236051599162882701011,
            1897711303285596482765067278580384626287392383020712652244311076941978193104,
            134015680,
            53838512057721440562939736802025788259291144916640581635750671998583,
            53828861982961135492201427184879149672978060633158020975671450566856,
            1218549703504418858426162283162138827211025846478484793807547462127761523912,
            1178049449980952191072349915198487915111597729517606511759043444728495120489,
            1248613335570268749676909298256317029060994509812092552730532908634266442967,
            1009593031495819252828700569240846868741021889479975996220684736809035147948,
            511,
            2237333844498111748883215477507890751800256493258715525511056416630703471762,
            511,
            165929294059383718597136186708933768,
            165928665183444790299169218008267404,
            53845519538819855508960808442933283326529873565053054911387792911572,
            632953462932985502401346219733,
            11402537257086157492956168853500386529649441493,
            2338127278722570274363906040942801375633166264814527435739863412231291358938,
            632992172374068595235072070355,
            14116035176210511671108551609314867430245309605471104878804150874868631768,
            2989198070966187183905653156820517155486041008851669,
            1914165240659878124278093455124795799975637481046506062551521715814255628505,
            2239183573178341868630490435769242119124342703048732488176563775529780318423,
            783516542217681384326802153090181251758094015190434920149,
            205392594202743270705599732363469054786985168948493990009572567,
            2196696207197523970848110246220850787567375659661879630802303424358746760918,
            43496064942646834154564841992264090467026,
            2451344281097134209035305689338887225410243436567307749947473686735818932439,
            134038231,
            9210896242334838485,
            11403234498813872975814317136142915991533607641,
            2052946140791500114938153887393848933442548125195317854590544344108166871666,
            134024309,
            2414284504605938271190647,
            2414432961303085847740591,
            1840619951669604201410879954770232798770635410344762643704857699299414045884,
            2054740440345882338413182726751774799923504795098535273396345647260901121653,
            11401099814810876640669013063882522770837151408,
            2251164459629472805890764632980280277423766955907349854903606843989721679987,
            35133399578745,
            632965160288698604399670134412,
            632963006881331930588495022277,
            1786344573953401262130609349327547713072556579231105641242862738580471876815,
            2038589805006897792509112007954350735573250959223795959103614935773248814209,
            53842228122476702495773295789384926848390152018530179498957340490361,
            11401924403712846923563882592647883891447504561,
            165908440879873426276575088097426107,
            632895368158899369863413300935,
            1758268323525362595923532786314480328080018599735810783059475054768039464143,
            205391794314900404883493199233542293351979380310701105445663417,
            53851282851944209321420284640220371633171315667141036238171496339675,
            205423955338156824854771464258264672819344281274335154421196500,
            2577729327169094013913866036448034673223125940098247934601313183565024680666,
            11403452706877142101024583031628030006125753017,
            2989425896204408496798268877956759112715740736746173,
            2989368449265634237443441350612458333462874654075053,
            2747678523175029219050907904201247527951368373004693689322273898694758724798,
            9211915013402753212,
            3018393708817140741526826604469395450011717245499418109813008810463571847958,
            511,
            2578005993306738766272720493549290003641129654788059437799834430105042922263,
            205453891193239864721000310941609329333251635029516111844828853,
            14118791738016870176898609723966653060633868239941088994990407440431952147,
            43502367761593881992589959363221389672163,
            2564532975842800516717576589205430082896313342638360674066970560767747778788,
            2662345006453239351655121122284717271256484617132755706888769186188301527742,
            2535048710605151100156159681989605707947025469919326540865784635456694350004,
            633040586241434255942783689906,
            2662538093696171203523322518377730080565180711373728926410531419261132958943,
            35140541447393,
            165915365962175250832669694316052105,
            165910991072664731647969066886692541,
            165915364714563768096814076148908681,
            632905030222108565907283182269,
            165941773779047896865379588025428813,
            783636964467172242015624329953870136005131318572805084494,
            53851123756109542786880701376471183343334073926133237164143885237585,
            11404551990931083991178740329624445415109531480,
            11404377256599313706827886264374972514872168274,
            14118043174159213750512275332842345089131422252097273049880944104801145683,
            2879060666130226223903190413161288011004202157635526868141497339872999740254,
            134058846,
            1348437942946537774187992213237890796509601516299775525587402884344614759289,
            205303390594931779347596621228099093120336219627248210526632204,
            325906032892669080226535334041476325337775906709561789896225250520937317938,
            783278824392957924854232793030235970878250007250455810227,
            
            39501981766050205511763645938889413926815084847014363323784499591316483998465,
            63116260013956854871348073892089032591383598474601552283817845072970876017752,
            105184833074588551822140444288569933265217461830097108150028045964
            ];
            
            
        uint256 i;
        uint256 first_index;
        uint256 cur_num;
        uint256 k;
        uint256 pos;
        uint256 part;
        string memory res;
        uint256 result;
        
        part = (xtypes[177+(num/32)] / (256**(num%32)));
        first_index = part%256;
        res = '';
    
        for(i = first_index-1; i<=250; i++) {
            if (xtypes[i] == 0)
                break;
    
            cur_num= xtypes[i];    
            k=0;
            for(pos=0;pos<=27;pos++) {
                result = (cur_num /  (512 ** pos)) % 512;
                if (result == 511)
                    break;
    
                if (k%2 == 0) {
                        res = string(abi.encodePacked(res, toString(result*3), ','));

                }
                else {
                    res = string(abi.encodePacked(res, toString(result*3), ' '));

                }
                k++;
             }   
            if (result == 511)
                break;
        }

        return string(abi.encodePacked('<polygon opacity="',op,'" points="', res, '" fill="#',color,'" />'));
    
    }
    
    function tokenURI(uint256 tokenId) pure public override(ERC721)  returns (string memory) {
        uint256[21] memory xtypes;
        string[6] memory colors;

        string[40] memory parts;
        uint256[12] memory params;

        uint256 pos;
        uint256 i;

        uint256 rand = random(string(abi.encodePacked('SeaMan:4,413.70',toString(tokenId))));

        params[0] = 1 + (rand % 35); // pallette=
        params[1] = 1 + ((rand/100) % 4);// beard
        params[2] = 1 + ((rand/1000) % 7); // cap
        params[3] = 1 + ((rand/10000) % 4); // ear
        params[4] = 1 + ((rand/100000) % 3); // glass
        params[5] = 1 + ((rand/1000000) % 4); // tube
        params[6] = 1 + ((rand/10000000) % 3); // mount
        params[7] = 1 + ((rand/100000000) % 4); // ship
        params[8] = 1 + ((rand/1000000000) % 3); // palm


        xtypes[0] = 207574965379110640608216022545122747804297713611180780071335701350711199;
        xtypes[1] = 1761542274015381829561806442388166782076133553027523683407153450284725839;
        xtypes[2] = 1318910288827824010265619304243609725711044680336380489173166130084184176;
        xtypes[3] = 724683447042801548696569560081030497164719783953138574072045560454446233;
        xtypes[4] = 4043991994606567853415936212959853079208569424781326127295573659918631;
        xtypes[5] = 3299620775196419612115186638262901643665550060006145227261506178449515;
        xtypes[6] = 1379064599592568033484279104016888586992032077745986354509927570994841;
        xtypes[7] = 1135013754671932960620571453940853222564543354936625869472113926285806993;
        xtypes[8] = 57540304388934554663713641863205016852965651449422231770389600610037335;
        xtypes[9] = 427915806210161317796546874658782169965398785064314928279652029687658137;
        xtypes[10] = 1645042025731165747020947949356891617690279996847601383616229449670826306;
        xtypes[11] = 2684402825716469350937603156675849781186432913394870831023569145709657;
        xtypes[12] = 948653233187084859464575317686592139222615901473770245914362476756190;
        xtypes[13] = 1062866615187437586058037191382177644371875915535598913465368644642651782;
        xtypes[14] = 369124523636997250166839484954653248850396559059904420057491494716833881;
        xtypes[15] = 442037650556033164066111039525968221351328081129885970071961277633331199;
        xtypes[16] = 1759957955803961638370133336600426903615700431543302529351008072247517534;
        xtypes[17] = 1409708756458103978798054840171071616358807503498064799802928769236271104;
        xtypes[18] = 311015873917387065279390940900677905795687522088524788509100013598527838;
        xtypes[19] = 1762344858914181531007580128366350777462090319317585386749708457335562658;
        xtypes[20] = 568162619401703612682951583545078045287018669724037195709696111106719797;

    
        for(i=0;i<=5;i++) {
            pos = (params[0]-1) * 6 + i;
            colors[i] = toHashCode(xtypes[pos/10] / (16777216 ** (pos%10)) % 16777216);
        }

        parts[0] = string(abi.encodePacked('<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" width="1000px" height="1000px" viewBox="0 0 1000 1000"><linearGradient id="g" gradientUnits="userSpaceOnUse" x1="500" y1="1000" x2="500" y2="0"><stop offset="0.5" style="stop-color:#',colors[1],'"/><stop offset="1" style="stop-color:#',colors[3],'"/></linearGradient><rect fill="url(#g)" width="1000" height="1000"/>'));
        
        
        parts[0] = string(abi.encodePacked(parts[0],'<radialGradient id="s" cx="676" cy="326" r="100" gradientUnits="userSpaceOnUse"><stop  offset="0.5" style="stop-color:#FFFF9E"/><stop offset="1" style="stop-color:#',colors[1],'"/></radialGradient><circle opacity="0.5" fill="url(#s)" cx="676" cy="326" r="100"/><g>'));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(83 +params[7], colors[2], '0.7'),'<animateMotion path="m 0 0 h -5000" dur="1500s" repeatCount="indefinite" /></g>'));
        if (params[8] > 1 ) {
            parts[0] = string(abi.encodePacked(parts[0], '<g>',getPolygon(params[8] == 2 ? 88: 89, colors[3], '1'),'<animateMotion path="M 0 0 H ',(params[8] == 2 ? '15' : '-13'),' Z" dur="5s" repeatCount="indefinite"/></g>'));
        }

        parts[0] = string(abi.encodePacked(parts[0], '<rect x="0" y="532" opacity="0.2" fill="#',colors[2],'" width="1000" height="469"/><rect x="0" y="608" opacity="0.3" fill="#',colors[2],'" width="1000" height="397"/><rect x="0" y="707" opacity="0.75" fill="#',colors[2],'" width="1000" height="500"><animateMotion path="M 0 0 V 20 Z" dur="10s" repeatCount="indefinite" /></rect><rect x="0" y="837" fill="#',colors[2],'" width="1000" height="163.167"><animateMotion path="M 0 0 V 60 Z" dur="10s" repeatCount="indefinite" /></rect>'));

        parts[0] = string(abi.encodePacked(parts[0], getPolygon(80+params[6], colors[2], '0.1')));

        parts[0] = string(abi.encodePacked(parts[0], getPolygon(0, colors[3], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(1, colors[5], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(2, colors[3], '0.3')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(3, colors[3], '0.3')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(4, colors[0], '0.2')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(5, colors[0], '0.2')));
        
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(6, colors[4], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(7, colors[2], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(8, 'FFFFE6', '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(9, 'FFFFE6', '1')));

        parts[0] = string(abi.encodePacked(parts[0],'<circle fill="#',colors[2],'" cx="566" cy="406" r="7.3"/><circle opacity="0.66" fill="#FFFFFF" cx="570" cy="404" r="3"/><circle fill="#',colors[2],'" cx="414" cy="407" r="7.3"/><circle opacity="0.66" fill="#FFFFFF" cx="418" cy="404" r="3"/><g>'));
        
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(77, colors[2],'1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(78, colors[2], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(79, colors[4], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(80, colors[4], '1')));

        parts[0] = string(abi.encodePacked(parts[0],'<animate attributeName="opacity" values="0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;0" dur="4s" repeatCount="indefinite" begin="0s"/></g>'));
        
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(9 +params[1]*2-1, colors[2], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(9 +params[1]*2, colors[3], '0.66')));
        
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(40 +params[3]*3-2, colors[3], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(40 +params[3]*3-1, colors[0], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(40 +params[3]*3, colors[0], '1')));

        parts[0] = string(abi.encodePacked(parts[0], getPolygon(52 +params[4]*4-3, colors[2], '0.85')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(52 +params[4]*4-2, colors[0], '0.35')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(52 +params[4]*4-1, colors[0], '0.35')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(52 +params[4]*4, colors[3], '1')));
        
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(17 +params[2]*3-2, colors[3], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(17 +params[2]*3-1, colors[5], '0.6')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(17 +params[2]*3, colors[5], '0.6')));
                
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(64 +params[5]*3-2, colors[5], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(64 +params[5]*3-1, colors[3], '1')));
        parts[0] = string(abi.encodePacked(parts[0], getPolygon(64 +params[5]*3, colors[3], '1')));
        
        parts[1] = string(abi.encodePacked('</svg> '));

        string memory output = string(abi.encodePacked(parts[0],parts[1]));
        

        
        parts[0] = '[{ "trait_type": "Palette", "value": "';
        parts[1] = toString(params[0]);
        parts[2] = '" }, { "trait_type": "Beard", "value": "';
        parts[3] = toString(params[1]);
        parts[4] = '" }, { "trait_type": "Cap", "value": "';
        parts[5] = toString(params[2]);
        parts[6] = '" }, { "trait_type": "Ear", "value": "';
        parts[7] = toString(params[3]);
        parts[8] = '" }, { "trait_type": "Glasses", "value": "';
        parts[9] = toString(params[4]);
        parts[10] = '" }, { "trait_type": "Tube", "value": "';
        parts[11] = toString(params[5]);
        parts[12] = '" }, { "trait_type": "Mountains", "value": "';
        parts[13] = toString(params[6]);
        parts[14] = '" }, { "trait_type": "Ship", "value": "';
        parts[15] = toString(params[7]);
        if (params[8] > 1) {
            parts[15] = string(abi.encodePacked(parts[15], 
             '" }, { "trait_type": "Palm", "value": "',toString(params[8]-1)));
        }
        parts[16] = '" }]';
        
        string memory strparams = string(abi.encodePacked(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]));
        strparams = string(abi.encodePacked(strparams, parts[6], parts[7], parts[8], parts[9], parts[10]));
        strparams = string(abi.encodePacked(strparams, parts[11], parts[12], parts[13], parts[14], parts[15], parts[16]));



        string memory json = Base64.encode(bytes(string(abi.encodePacked('{"name": "Onchain Seaman", "description": "Onchain Seaman - beautiful avatar, completely generated OnChain","attributes":', strparams, ', "image": "data:image/svg+xml;base64,', Base64.encode(bytes(output)), '"}'))));
        output = string(abi.encodePacked('data:application/json;base64,', json));

        return output;
    }
    
    function flipSaleState() public onlyOwner {
        saleIsActive = !saleIsActive;
    }
    
    function burnAll() public onlyOwner {
        burned = true;
    }
    
    function directMint(address to, uint256 tokenId) public onlyOwner {
        require(!burned, "Burned!");
        _safeMint(to, tokenId);
    }
    
    function mintMany(uint32[] memory ids) public {
        require(!burned, "Burned!");
        require(saleIsActive, "Sale must be active to mint tokens");

        for (uint i = 0; i < ids.length; i++) {
            require(SeaContract.ownerOf(ids[i]) == msg.sender, "Must own a Sea to mint token");
            _safeMint(msg.sender, ids[i]);
        }
    }
    
    function mintToken(uint tokenId) public {
        require(!burned, "Burned!");
        require(saleIsActive, "Sale must be active to mint tokens");
        require(SeaContract.ownerOf(tokenId) == msg.sender, "Must own a Sea to mint token");

        _safeMint(msg.sender, tokenId);
    }

    
}




/// [MIT License]
/// @title Base64
/// @notice Provides a function for encoding some bytes in base64
/// @author Brecht Devos <brecht@loopring.org>
library Base64 {
    bytes internal constant TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    /// @notice Encodes some bytes to the base64 representation
    function encode(bytes memory data) internal pure returns (string memory) {
        uint256 len = data.length;
        if (len == 0) return "";

        // multiply by 4/3 rounded up
        uint256 encodedLen = 4 * ((len + 2) / 3);

        // Add some extra buffer at the end
        bytes memory result = new bytes(encodedLen + 32);

        bytes memory table = TABLE;

        assembly {
            let tablePtr := add(table, 1)
            let resultPtr := add(result, 32)

            for {
                let i := 0
            } lt(i, len) {

            } {
                i := add(i, 3)
                let input := and(mload(add(data, i)), 0xffffff)

                let out := mload(add(tablePtr, and(shr(18, input), 0x3F)))
                out := shl(8, out)
                out := add(out, and(mload(add(tablePtr, and(shr(12, input), 0x3F))), 0xFF))
                out := shl(8, out)
                out := add(out, and(mload(add(tablePtr, and(shr(6, input), 0x3F))), 0xFF))
                out := shl(8, out)
                out := add(out, and(mload(add(tablePtr, and(input, 0x3F))), 0xFF))
                out := shl(224, out)

                mstore(resultPtr, out)

                resultPtr := add(resultPtr, 4)
            }

            switch mod(len, 3)
            case 1 {
                mstore(sub(resultPtr, 2), shl(240, 0x3d3d))
            }
            case 2 {
                mstore(sub(resultPtr, 1), shl(248, 0x3d))
            }

            mstore(result, encodedLen)
        }

        return string(result);
    }
}

// File: @openzeppelin/contracts/utils/Counters.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @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;
    }
}

// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @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);
    }
}

// File: @openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../ERC721.sol";
import "./IERC721Enumerable.sol";

/**
 * @dev This implements an optional extension of {ERC721} defined in the EIP that adds
 * enumerability of all the token ids in the contract as well as all token ids owned by each
 * account.
 */
abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
    // Mapping from owner to list of owned token IDs
    mapping(address => mapping(uint256 => uint256)) private _ownedTokens;

    // Mapping from token ID to index of the owner tokens list
    mapping(uint256 => uint256) private _ownedTokensIndex;

    // Array with all token ids, used for enumeration
    uint256[] private _allTokens;

    // Mapping from token id to position in the allTokens array
    mapping(uint256 => uint256) private _allTokensIndex;

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC721) returns (bool) {
        return interfaceId == type(IERC721Enumerable).interfaceId || super.supportsInterface(interfaceId);
    }

    /**
     * @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
        require(index < ERC721.balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
        return _ownedTokens[owner][index];
    }

    /**
     * @dev See {IERC721Enumerable-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _allTokens.length;
    }

    /**
     * @dev See {IERC721Enumerable-tokenByIndex}.
     */
    function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
        require(index < ERC721Enumerable.totalSupply(), "ERC721Enumerable: global index out of bounds");
        return _allTokens[index];
    }

    /**
     * @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` cannot be the zero address.
     * - `to` cannot be the zero address.
     *
     * 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 override {
        super._beforeTokenTransfer(from, to, tokenId);

        if (from == address(0)) {
            _addTokenToAllTokensEnumeration(tokenId);
        } else if (from != to) {
            _removeTokenFromOwnerEnumeration(from, tokenId);
        }
        if (to == address(0)) {
            _removeTokenFromAllTokensEnumeration(tokenId);
        } else if (to != from) {
            _addTokenToOwnerEnumeration(to, tokenId);
        }
    }

    /**
     * @dev Private function to add a token to this extension's ownership-tracking data structures.
     * @param to address representing the new owner of the given token ID
     * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
     */
    function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
        uint256 length = ERC721.balanceOf(to);
        _ownedTokens[to][length] = tokenId;
        _ownedTokensIndex[tokenId] = length;
    }

    /**
     * @dev Private function to add a token to this extension's token tracking data structures.
     * @param tokenId uint256 ID of the token to be added to the tokens list
     */
    function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
        _allTokensIndex[tokenId] = _allTokens.length;
        _allTokens.push(tokenId);
    }

    /**
     * @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that
     * while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for
     * gas optimizations e.g. when performing a transfer operation (avoiding double writes).
     * This has O(1) time complexity, but alters the order of the _ownedTokens array.
     * @param from address representing the previous owner of the given token ID
     * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
     */
    function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
        // To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
        // then delete the last slot (swap and pop).

        uint256 lastTokenIndex = ERC721.balanceOf(from) - 1;
        uint256 tokenIndex = _ownedTokensIndex[tokenId];

        // When the token to delete is the last token, the swap operation is unnecessary
        if (tokenIndex != lastTokenIndex) {
            uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];

            _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
            _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
        }

        // This also deletes the contents at the last position of the array
        delete _ownedTokensIndex[tokenId];
        delete _ownedTokens[from][lastTokenIndex];
    }

    /**
     * @dev Private function to remove a token from this extension's token tracking data structures.
     * This has O(1) time complexity, but alters the order of the _allTokens array.
     * @param tokenId uint256 ID of the token to be removed from the tokens list
     */
    function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
        // To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and
        // then delete the last slot (swap and pop).

        uint256 lastTokenIndex = _allTokens.length - 1;
        uint256 tokenIndex = _allTokensIndex[tokenId];

        // When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so
        // rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding
        // an 'if' statement (like in _removeTokenFromOwnerEnumeration)
        uint256 lastTokenId = _allTokens[lastTokenIndex];

        _allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
        _allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index

        // This also deletes the contents at the last position of the array
        delete _allTokensIndex[tokenId];
        _allTokens.pop();
    }
}

// File: @openzeppelin/contracts/token/ERC721/ERC721.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IERC721.sol";
import "./IERC721Receiver.sol";
import "./extensions/IERC721Metadata.sol";
import "../../utils/Address.sol";
import "../../utils/Context.sol";
import "../../utils/Strings.sol";
import "../../utils/introspection/ERC165.sol";

/**
 * @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including
 * the Metadata extension, but not including the Enumerable extension, which is available separately as
 * {ERC721Enumerable}.
 */
contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
    using Address for address;
    using Strings for uint256;

    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    // Mapping from token ID to owner address
    mapping(uint256 => address) private _owners;

    // Mapping owner address to token count
    mapping(address => uint256) private _balances;

    // Mapping from token ID to approved address
    mapping(uint256 => address) private _tokenApprovals;

    // Mapping from owner to operator approvals
    mapping(address => mapping(address => bool)) private _operatorApprovals;

    /**
     * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
     */
    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
        return
            interfaceId == type(IERC721).interfaceId ||
            interfaceId == type(IERC721Metadata).interfaceId ||
            super.supportsInterface(interfaceId);
    }

    /**
     * @dev See {IERC721-balanceOf}.
     */
    function balanceOf(address owner) public view virtual override returns (uint256) {
        require(owner != address(0), "ERC721: balance query for the zero address");
        return _balances[owner];
    }

    /**
     * @dev See {IERC721-ownerOf}.
     */
    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "ERC721: owner query for nonexistent token");
        return owner;
    }

    /**
     * @dev See {IERC721Metadata-name}.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev See {IERC721Metadata-symbol}.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

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

    /**
     * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
     * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
     * by default, can be overriden in child contracts.
     */
    function _baseURI() internal view virtual returns (string memory) {
        return "";
    }

    /**
     * @dev See {IERC721-approve}.
     */
    function approve(address to, uint256 tokenId) public virtual override {
        address owner = ERC721.ownerOf(tokenId);
        require(to != owner, "ERC721: approval to current owner");

        require(
            _msgSender() == owner || isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not owner nor approved for all"
        );

        _approve(to, tokenId);
    }

    /**
     * @dev See {IERC721-getApproved}.
     */
    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        require(_exists(tokenId), "ERC721: approved query for nonexistent token");

        return _tokenApprovals[tokenId];
    }

    /**
     * @dev See {IERC721-setApprovalForAll}.
     */
    function setApprovalForAll(address operator, bool approved) public virtual override {
        require(operator != _msgSender(), "ERC721: approve to caller");

        _operatorApprovals[_msgSender()][operator] = approved;
        emit ApprovalForAll(_msgSender(), operator, approved);
    }

    /**
     * @dev See {IERC721-isApprovedForAll}.
     */
    function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[owner][operator];
    }

    /**
     * @dev See {IERC721-transferFrom}.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");

        _transfer(from, to, tokenId);
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
        _safeTransfer(from, to, tokenId, _data);
    }

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * `_data` is additional data, it has no specified format and it is sent in call to `to`.
     *
     * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
     * implement alternative mechanisms to perform token transfer, such as signature-based.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeTransfer(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) internal virtual {
        _transfer(from, to, tokenId);
        require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * @dev Returns whether `tokenId` exists.
     *
     * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
     *
     * Tokens start existing when they are minted (`_mint`),
     * and stop existing when they are burned (`_burn`).
     */
    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return _owners[tokenId] != address(0);
    }

    /**
     * @dev Returns whether `spender` is allowed to manage `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        require(_exists(tokenId), "ERC721: operator query for nonexistent token");
        address owner = ERC721.ownerOf(tokenId);
        return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
    }

    /**
     * @dev Safely mints `tokenId` and transfers it to `to`.
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }

    /**
     * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
     * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
     */
    function _safeMint(
        address to,
        uint256 tokenId,
        bytes memory _data
    ) internal virtual {
        _mint(to, tokenId);
        require(
            _checkOnERC721Received(address(0), to, tokenId, _data),
            "ERC721: transfer to non ERC721Receiver implementer"
        );
    }

    /**
     * @dev Mints `tokenId` and transfers it to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - `to` cannot be the zero address.
     *
     * Emits a {Transfer} event.
     */
    function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(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 {}
}

// File: @openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Enumerable is IERC721 {
    /**
     * @dev Returns the total amount of tokens stored by the contract.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns a token ID owned by `owner` at a given `index` of its token list.
     * Use along with {balanceOf} to enumerate all of ``owner``'s tokens.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId);

    /**
     * @dev Returns a token ID at a given `index` of all the tokens stored by the contract.
     * Use along with {totalSupply} to enumerate all tokens.
     */
    function tokenByIndex(uint256 index) external view returns (uint256);
}

// File: @openzeppelin/contracts/utils/introspection/ERC165.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IERC165.sol";

/**
 * @dev Implementation of the {IERC165} interface.
 *
 * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
 * for the additional interface id that will be supported. For example:
 *
 * ```solidity
 * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
 *     return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
 * }
 * ```
 *
 * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
 */
abstract contract ERC165 is IERC165 {
    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IERC165).interfaceId;
    }
}

// File: @openzeppelin/contracts/utils/Strings.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev String operations.
 */
library Strings {
    bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        // Inspired by OraclizeAPI's implementation - MIT licence
        // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (value != 0) {
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
            value /= 10;
        }
        return string(buffer);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) internal pure returns (string memory) {
        if (value == 0) {
            return "0x00";
        }
        uint256 temp = value;
        uint256 length = 0;
        while (temp != 0) {
            length++;
            temp >>= 8;
        }
        return toHexString(value, length);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = _HEX_SYMBOLS[value & 0xf];
            value >>= 4;
        }
        require(value == 0, "Strings: hex length insufficient");
        return string(buffer);
    }
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// File: @openzeppelin/contracts/utils/Address.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        assembly {
            size := extcodesize(account)
        }
        return size > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly

                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

// File: @openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional metadata extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Metadata is IERC721 {
    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the token collection symbol.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);
}

// File: @openzeppelin/contracts/token/ERC721/IERC721Receiver.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

// File: @openzeppelin/contracts/token/ERC721/IERC721.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;
}

// File: @openzeppelin/contracts/utils/introspection/IERC165.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}