package org.commcare.android.util;

import org.commcare.android.cases.AndroidCaseInstanceTreeElement;
import org.commcare.android.cases.AndroidLedgerInstanceTreeElement;
import org.commcare.android.database.AndroidSandbox;
import org.commcare.android.database.SqlStorage;
import org.commcare.android.database.user.models.ACase;
import org.commcare.cases.ledger.Ledger;
import org.commcare.core.process.CommCareInstanceInitializer;
import org.commcare.dalvik.application.CommCareApplication;
import org.commcare.session.CommCareSession;
import org.javarosa.core.model.instance.AbstractTreeElement;
import org.javarosa.core.model.instance.ExternalDataInstance;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.TreeElement;

/**
 * @author ctsims
 */
public class AndroidInstanceInitializer extends CommCareInstanceInitializer {

    /**
     * For testing: allows for data instanced backed evaluation when an app
     * isn't present.
     */
    public AndroidInstanceInitializer() {
        super(null);
    }

    public AndroidInstanceInitializer(CommCareSession session) {
        super(session, new AndroidSandbox(CommCareApplication._()), CommCareApplication._().getCommCarePlatform());
    }

    @Override
    protected AbstractTreeElement setupLedgerData(ExternalDataInstance instance) {
        if(stockbase == null) {
            SqlStorage<Ledger> storage = (SqlStorage<Ledger>)mSandbox.getLedgerStorage();
            stockbase =  new AndroidLedgerInstanceTreeElement(instance.getBase(), storage);
        } else {
            //re-use the existing model if it exists.
            stockbase.rebase(instance.getBase());
        }
        return stockbase;
    }

    @Override
    protected AbstractTreeElement setupCaseData(ExternalDataInstance instance) {
        if (casebase == null) {
            SqlStorage<ACase> storage = (SqlStorage<ACase>)mSandbox.getCaseStorage();
            casebase = new AndroidCaseInstanceTreeElement(instance.getBase(), storage, false);
        } else {
            //re-use the existing model if it exists.
            casebase.rebase(instance.getBase());
        }
        instance.setCacheHost((AndroidCaseInstanceTreeElement)casebase);
        return casebase;
    }

    @Override
    public String getVersionString() {
        return CommCareApplication._().getCurrentVersionString();
    }

    @Override
    public String getDeviceId() {
        String phoneId = CommCareApplication._().getPhoneId();
        if (phoneId == null) {
            return super.getDeviceId();
        }
        return phoneId;
    }

    @Override
    protected AbstractTreeElement setupMigrationData(ExternalDataInstance instance) {
        FormInstance fixture = null;

        TreeElement root = fixture.getRoot();
        root.setParent(instance.getBase());
        return root;
    }
}

@@ -655,115 +655,8 @@
 nce;
-%0Aimport org.javarosa.core.model.instance.FormInstance;%0Aimport org.javarosa.core.model.instance.TreeElement;
 %0A%0A/*
@@ -2499,263 +2499,7 @@
    %7D
-%0A%0A    @Override%0A    protected AbstractTreeElement setupMigrationData(ExternalDataInstance instance) %7B%0A        FormInstance fixture = null;%0A%0A        TreeElement root = fixture.getRoot();%0A        root.setParent(instance.getBase());%0A        return root;%0A    %7D
 %0A%7D%0A

Revert "case migration instance setup"