package org.commcare.android.resource.installers;

import org.commcare.CommCareApplication;
import org.commcare.resources.model.Resource;
import org.commcare.resources.model.ResourceInitializationException;
import org.commcare.resources.model.UnresolvedResourceException;
import org.commcare.suite.model.OfflineUserRestore;
import org.commcare.utils.AndroidCommCarePlatform;
import org.javarosa.core.reference.Reference;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
import org.javarosa.core.util.externalizable.PrototypeFactory;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * @author Phillip Mates (pmates@dimagi.com)
 */
public class OfflineUserRestoreAndroidInstaller extends FileSystemInstaller {

    @SuppressWarnings("unused")
    public OfflineUserRestoreAndroidInstaller() {
        // for externalization
    }

    public OfflineUserRestoreAndroidInstaller(String localDestination, String upgradeDestination) {
        super(localDestination, upgradeDestination);
    }

    @Override
    public boolean initialize(AndroidCommCarePlatform instance, boolean isUpgrade) throws ResourceInitializationException {
        if (localLocation == null) {
            throw new ResourceInitializationException("The user restore file location is null!");
        }
        OfflineUserRestore offlineUserRestore = new OfflineUserRestore(localLocation);
        instance.registerDemoUserRestore(offlineUserRestore);
        return true;
    }

    @Override
    protected int customInstall(Resource r, Reference local, boolean upgrade) throws IOException, UnresolvedResourceException {
        return upgrade ? Resource.RESOURCE_STATUS_UPGRADE : Resource.RESOURCE_STATUS_INSTALLED;
    }

    @Override
    public boolean requiresRuntimeInitialization() {
        return true;
    }

    @Override
    public void readExternal(DataInputStream in, PrototypeFactory pf)
            throws IOException, DeserializationException {
        super.readExternal(in, pf);
    }

    @Override
    public void writeExternal(DataOutputStream out) throws IOException {
        super.writeExternal(out);
    }
}

@@ -81,24 +81,192 @@
 pplication;%0A
+import org.commcare.android.database.app.models.UserKeyRecord;%0Aimport org.commcare.android.logging.ReportingUtils;%0Aimport org.commcare.models.encryption.ByteEncrypter;%0A
 import org.c
@@ -295,24 +295,24 @@
 l.Resource;%0A
-
 import org.c
@@ -657,62 +657,8 @@
 on;%0A
-import org.javarosa.core.util.externalizable.ExtUtil;%0A
 impo
@@ -1506,16 +1506,102 @@
       %7D%0A
+        if (isUpgrade) %7B%0A            clearDataForCurrentDemoUser(instance);%0A        %7D%0A
         
@@ -1737,24 +1737,24 @@
 erRestore);%0A
-
         retu
@@ -1761,32 +1761,727 @@
 rn true;%0A    %7D%0A%0A
+    private void clearDataForCurrentDemoUser(AndroidCommCarePlatform instance) %7B%0A        OfflineUserRestore current = instance.getDemoUserRestore();%0A%0A        if (!current.getUsername().equals(ReportingUtils.getUser())) %7B%0A            UserKeyRecord ukr = UserKeyRecord.getCurrentValidRecordByPassword(%0A                    CommCareApplication._().getCurrentApp(), current.getUsername(),%0A                    current.getPassword(), true);%0A            CommCareApplication._().startUserSession(%0A                    ByteEncrypter.unwrapByteArrayWithString(ukr.getEncryptedKey(), current.getPassword()),%0A                    ukr, false);%0A        %7D%0A%0A        CommCareApplication._().clearUserData();%0A    %7D%0A%0A
     @Override%0A  

clear user data on update is working for 1 demo app, but code doesn't seem to run for another app