package org.commcare.android.database;

import org.commcare.android.database.user.models.ACase;
import org.commcare.android.util.SessionUnavailableException;
import org.commcare.cases.ledger.Ledger;
import org.commcare.cases.model.Case;
import org.commcare.core.interfaces.UserDataInterface;
import org.commcare.dalvik.application.CommCareApplication;
import org.javarosa.core.model.User;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.services.storage.IStorageUtilityIndexed;

/**
 * Basically just a useful abstraction that allows us to use an Android
 * CommCareApplication as a UserDataInterface
 *
 * Created by wpride1 on 8/11/15.
 */
public class AndroidSandbox implements UserDataInterface {

    CommCareApplication app;
    String syncToken;

    public AndroidSandbox(CommCareApplication app){
        this.app = app;
    }

    @Override
    public IStorageUtilityIndexed<Case> getCaseStorage() {
        return app.getUserStorage(ACase.STORAGE_KEY, ACase.class);
    }

    @Override
    public IStorageUtilityIndexed<Ledger> getLedgerStorage() {
        return app.getUserStorage(Ledger.STORAGE_KEY, Ledger.class);
    }

    @Override
    public IStorageUtilityIndexed<User> getUserStorage() {
        return app.getUserStorage("USER", User.class);
    }

    @Override
    public IStorageUtilityIndexed<FormInstance> getUserFixtureStorage() {
        return app.getUserStorage("fixture", FormInstance.class);
    }

    @Override
    public IStorageUtilityIndexed<FormInstance> getAppFixtureStorage() {
        return app.getAppStorage("fixture", FormInstance.class);
    }

    @Override
    public User getLoggedInUser() {
        try {
            return app.getSession().getLoggedInUser();
        } catch (SessionUnavailableException e) {
            return null;
        }
    }

    @Override
    public void setLoggedInUser(User user) {

    }

    @Override
    public void setSyncToken(String syncToken) {
        this.syncToken = syncToken;
    }

    @Override
    public String getSyncToken() {
        return syncToken;
    }
}

@@ -266,33 +266,35 @@
 erfaces.
-UserDataInterface
+AbstractUserSandbox
 ;%0Aimport
@@ -700,36 +700,35 @@
 box 
-implements UserDataInterface
+extends AbstractUserSandbox
  %7B%0A%0A
@@ -759,30 +759,8 @@
 app;
-%0A    String syncToken;
 %0A%0A  
@@ -1875,195 +1875,7 @@
    %7D
-%0A%0A    @Override%0A    public void setSyncToken(String syncToken) %7B%0A        this.syncToken = syncToken;%0A    %7D%0A%0A    @Override%0A    public String getSyncToken() %7B%0A        return syncToken;%0A    %7D
 %0A%7D%0A

use abstract sandbox