package org.commcare.util.mocks;

import java.io.IOException;
import java.util.NoSuchElementException;

import org.commcare.data.xml.TransactionParser;
import org.javarosa.core.model.utils.DateUtils;
import org.javarosa.core.services.storage.IStorageUtilityIndexed;
import org.javarosa.core.services.storage.StorageFullException;
import org.javarosa.xml.util.InvalidStructureException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 * Mock copied from Android app.
 *
 * TODO:unify into CommCare Global Project
 *
 * @author ctsims
 */
public class UserXmlParser extends TransactionParser<User> {

    IStorageUtilityIndexed<User> storage;
    byte[] wrappedKey;


    public UserXmlParser(KXmlParser parser, IStorageUtilityIndexed<User> storage) {
        super(parser);
        this.storage = storage;
    }

    public User parse() throws InvalidStructureException, IOException, XmlPullParserException {
        this.checkNode("registration");

        //parse (with verification) the next tag
        this.nextTag("username");
        String username = parser.nextText();

        this.nextTag("password");
        String passwordHash = parser.nextText();

        this.nextTag("uuid");
        String uuid = parser.nextText();

        this.nextTag("date");
        String dateModified = parser.nextText();
        DateUtils.parseDateTime(dateModified);

        User u = retrieve(uuid);

        if (u == null) {
            u = new User(username, passwordHash, uuid);
        } else {
            if (passwordHash != null && !passwordHash.equals(u.getPassword())) {
                u.setPassword(passwordHash);
            }
        }

        //Now look for optional components
        while (this.nextTagInBlock("registration")) {

            String tag = parser.getName().toLowerCase();

            if (tag.equals("registering_phone_id")) {
                parser.nextText();
            } else if (tag.equals("token")) {
                parser.nextText();
            } else if (tag.equals("user_data")) {
                while (this.nextTagInBlock("user_data")) {
                    this.checkNode("data");

                    String key = this.parser.getAttributeValue(null, "key");
                    String value = this.parser.nextText();

                    u.setProperty(key, value);
                }

                //This should be the last block in the registration stuff...
                break;
            } else {
                throw new InvalidStructureException("Unrecognized tag in user registraiton data: " + tag, parser);
            }
        }

        commit(u);
        return u;
    }

    public void commit(User parsed) throws IOException {
        try {
            storage().write(parsed);
        } catch (StorageFullException e) {
            e.printStackTrace();
            throw new IOException("Storage full while writing case!");
        }
    }

    public User retrieve(String entityId) {
        IStorageUtilityIndexed storage = storage();
        try {
            return (User)storage.getRecordForValue(User.META_UID, entityId);
        } catch (NoSuchElementException nsee) {
            return null;
        }
    }

    public IStorageUtilityIndexed storage() {
        return storage;
    }
}

@@ -38,114 +38,82 @@
 ort 
-java.io.IOException;%0Aimport java.util.NoSuchElementException;%0A%0Aimport org.commcare.data.xml.TransactionPar
+org.commcare.data.xml.TransactionParser;%0Aimport org.commcare.suite.model.U
 ser;
@@ -426,16 +426,86 @@
 ption;%0A%0A
+import java.io.IOException;%0Aimport java.util.NoSuchElementException;%0A%0A
 /**%0A * M

use new user