#include <bu/file.h>
#include <bu/myriadfs.h>
#include <bu/myriadstream.h>
#include <bu/sio.h>

class Obstore
{
public:
    Obstore() :
        fStore("test.mfs", Bu::File::WriteNew|Bu::File::Read ),
        fsOb( fStore, 1024 )
    {
    }

    virtual ~Obstore()
    {
    }

    Bu::String getObject( const Bu::String &sPath )
    {
        try
        {
            Bu::MyriadStream s = fsOb.open(
                sPath,
                Bu::MyriadFs::Read
                );
            return s.readAll();
        }
        catch( Bu::ExceptionBase &e )
        {
            Bu::println("Exception opening file: %1").arg( e.what() );
            return Bu::String();
        }
    }

    void storeObject( const Bu::String &sPath, const Bu::String &sData )
    {
        for( Bu::String::const_iterator i = sPath.begin()+1; i; i++ )
        {
            if( *i == '/' )
            {
                Bu::String sChunk( sPath.begin(), i );
                fsOb.mkDir( sChunk, 0664 );
            }
        }
        try
        {
            Bu::MyriadStream s = fsOb.open(
                sPath,
                Bu::MyriadFs::Write|Bu::MyriadFs::Create|Bu::MyriadFs::Truncate
                );
            s.write( sData );
        }
        catch(Bu::ExceptionBase &e )
        {
            Bu::println("Exception opening file: %1").arg( e.what() );
        }
    }

    Bu::File fStore;
    Bu::MyriadFs fsOb;
};

int main( int, char *[] )
{
    Obstore os;

    os.storeObject("/killTeamState.json", "This is a json file.");
    os.storeObject("/appCredentials/local", "App credentials");
    os.storeObject("/appCredentials/inckybot", "App credentials");
    os.storeObject("/appCredentials/playwith", "App credentials");
    os.storeObject("/restreamIo/users", "User data");
    os.storeObject("/youTube/users", "User data");
    os.storeObject("/topTippers.json", "{\"people\": [\"sam\", \"joe\", \"bob\"]}");
    os.storeObject("/wh40kDecks.json", "");

    Bu::MyriadStream s = os.fsOb.open(
        "/appCredentials/inckybot",
        Bu::MyriadFs::Write|Bu::MyriadFs::Create|Bu::MyriadFs::Truncate
        );
    s.write("Some new credentials, this is a bigger bit of data.");

    os.storeObject("/wh40kDecks.json", "{                    \"op\": \"replaceDecks\",                    \"decks\": {                        \"lev\": {                            \"name\": \"Leviathan\",                            \"primary\": {                                \"pt\": {\"name\": \"Priority Targets\"},                                \"vg\": {\"name\": \"Vital Ground\"},                                \"se\": {\"name\": \"Scorched Earth\"},                                \"pf\": {\"name\": \"Purge the Foe\"},                                \"th\": {\"name\": \"Take and Hold\"},                                \"sd\": {\"name\": \"Supply Drop\"},                                \"tr\": {\"name\": \"The Ritual\"},                                \"ds\": {\"name\": \"Deploy Servo-Skulls\"},                                \"sp\": {\"name\": \"Sites of Power\"},                            },                            \"secondary\": {                                \"tt\": {\"name\": \"A Tempting Target\"},                                \"ad\": {\"name\": \"Area Denial\"},                                \"as\": {\"name\": \"Assassination\"},                                \"be\": {\"name\": \"Behind Enemy Lines\"},                                \"bd\": {\"name\": \"Bring It Down\"},                                \"ce\": {\"name\": \"Capture Enemy Outpost\"},                                \"cl\": {\"name\": \"Cleanse\"},                                \"ds\": {\"name\": \"Defend Stronghold\"},                                \"dt\": {\"name\": \"Deploy Teleport Homer\"},                                \"ef\": {\"name\": \"Engage On All Fronts\"},                                \"eb\": {\"name\": \"Extend Battle Lines\"},                                \"is\": {\"name\": \"Investigate Signals\"},                                \"np\": {\"name\": \"No Prisoners\"},                                \"of\": {\"name\": \"Overwhelming Force\"},                                \"sl\": {\"name\": \"Secure No Man’s Land\"},                                \"sh\": {\"name\": \"Storm Hostile Objective\"},                            },                            \"gambit\": {                                \"dt\": {\"name\": \"Delaying Tactics\"},                                \"ee\": {\"name\": \"Emergency Evacuation\"},                                \"oc\": {\"name\": \"Orbital Strike Coordinates\"},                                \"pp\": {\"name\": \"Proceed As Planned\"},                            },                            \"missionRule\": {                                \"cr\": {\"name\": \"Chilling Rain\"},                                \"sc\": {\"name\": \"Sweep and Clear\"},                                \"hs\": {\"name\": \"Hidden Supplies\"},                                \"mi\": {\"name\": \"Minefields\"},                                \"to\": {\"name\": \"Targets of Opportunity\"},                                \"sf\": {\"name\": \"Scrambler Fields\"},                                \"dr\": {\"name\": \"Delayed Reserves\"},                                \"cb\": {\"name\": \"Chosen Battlefield\"},                                \"mb\": {\"name\": \"Maelstrom of Battle\"},                                \"sl\": {\"name\": \"Supply Lines\"},                                \"si\": {\"name\": \"Secret Intel\"},                                \"vs\": {\"name\": \"Vox Static\"},                            },                        },                    }                }");
    os.storeObject("/wh40kState.json", "{\"cards\": \"hello\"}");

    return 0;
}

