package org.commcare.suite.model;

import org.commcare.suite.model.graph.DisplayData;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
import org.javarosa.core.util.externalizable.Externalizable;
import org.javarosa.core.util.externalizable.PrototypeFactory;

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

/**
 * <p>A display unit element contains text and a set of potential image/audio
 * references for menus or other UI elements</p>
 *
 * @author ctsims
 */
public class DisplayUnit implements Externalizable, DetailTemplate {

    Text name;
    Text imageReference;
    Text audioReference;

    /**
     * Serialization only!!!
     */
    public DisplayUnit() {

    }


    public DisplayUnit(Text name, Text imageReference, Text audioReference) {
        this.name = name;
        this.imageReference = imageReference;
        this.audioReference = audioReference;
    }

    public DisplayData evaluate(){
        return evaluate(null);
    }

    public DisplayData evaluate(EvaluationContext ec){
        String imageRef = imageReference == null ? null : imageReference.evaluate(ec);
        String audioRef = audioReference == null ? null : audioReference.evaluate(ec);
        return new DisplayData(name.evaluate(ec),
                imageRef,
                audioRef);
    }

    /**
     * @return A Text which should be displayed to the user as
     * the action which will display this menu.
     */
    public Text getText() {
        return name;
    }

    public Text getImageURI() {
        return imageReference;
    }

    public Text getAudioURI() {
        return audioReference;
    }

    /* (non-Javadoc)
     * @see org.javarosa.core.util.externalizable.Externalizable#readExternal(java.io.DataInputStream, org.javarosa.core.util.externalizable.PrototypeFactory)
     */
    public void readExternal(DataInputStream in, PrototypeFactory pf)
            throws IOException, DeserializationException {
        name = (Text)ExtUtil.read(in, Text.class, pf);
        imageReference = (Text)ExtUtil.read(in, Text.class, pf);
        audioReference = (Text)ExtUtil.read(in, Text.class, pf);
    }

    /* (non-Javadoc)
     * @see org.javarosa.core.util.externalizable.Externalizable#writeExternal(java.io.DataOutputStream)
     */
    public void writeExternal(DataOutputStream out) throws IOException {
        ExtUtil.write(out, name);
        ExtUtil.write(out, imageReference);
        ExtUtil.write(out, audioReference);
    }

}

@@ -264,16 +264,78 @@
 xtUtil;%0A
+import org.javarosa.core.util.externalizable.ExtWrapNullable;%0A
 import o
@@ -2203,32 +2203,52 @@
 xtUtil.read(in, 
+new ExtWrapNullable(
 Text.class, pf);
@@ -2233,36 +2233,33 @@
 lable(Text.class
-, pf
+)
 );%0A        image
@@ -2285,32 +2285,52 @@
 xtUtil.read(in, 
+new ExtWrapNullable(
 Text.class, pf);
@@ -2319,28 +2319,25 @@
 e(Text.class
-, pf
+)
 );%0A        a
@@ -2375,16 +2375,36 @@
 ead(in, 
+new ExtWrapNullable(
 Text.cla
@@ -2409,12 +2409,9 @@
 lass
-, pf
+)
 );%0A 
@@ -2654,13 +2654,34 @@
 ut, 
+new ExtWrapNullable(
 name)
+)
 ;%0A  
@@ -2705,16 +2705,36 @@
 te(out, 
+new ExtWrapNullable(
 imageRef
@@ -2736,24 +2736,25 @@
 geReference)
+)
 ;%0A        Ex
@@ -2770,16 +2770,36 @@
 te(out, 
+new ExtWrapNullable(
 audioRef
@@ -2805,16 +2805,17 @@
 ference)
+)
 ;%0A    %7D%0A

fix nullables in displayunit