/*
 *  Copyright (C) 2019-2020 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package org.akvo.flow.xml;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.gallatinsystems.survey.domain.Question;
import com.gallatinsystems.survey.domain.Translation;
import java.util.ArrayList;
import java.util.List;

/*
 * Class for working with form XML tags like this:
 *
    <help type="tip">
        <text>Outline</text>
        <altText type="translation" language="sv">Ungefärlig</altText>
    </help>

 */

public class XmlHelp {

    @JacksonXmlProperty(localName = "type", isAttribute = true)
    private String type; //Should always be "tip"
    @JacksonXmlProperty(localName = "text", isAttribute = false)
    private String text;
    @JacksonXmlElementWrapper(localName = "altText", useWrapping = false)
    private List<XmlAltText> altText;

    public XmlHelp(Question q) {
        this.text = q.getTip();
        List<Translation> translations = q.getTranslations();
        for (Translation t: translations) {
            if (Translation.ParentType.QUESTION_TIP.equals(t.getParentType())) {
                if (altText == null) {
                    altText = new ArrayList<>();
                }
                altText.add(new XmlAltText(t));
            }
        }
    }

    @Override public String toString() {
        return "help{" +
                "type='" + type +
                "',text='" + text +
                "'}";
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public List<XmlAltText> getAltText() {
        return altText;
    }

    public void setAltText(List<XmlAltText> altText) {
        this.altText = altText;
    }

}

@@ -1037,20 +1037,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0A/*%0A *
@@ -1703,21 +1703,172 @@
         
-List%3C
+// this removes the empty %3CaltText/%3E tag from the xml%0A        if (altText == null) %7B%0A            altText = new ArrayList%3C%3E();%0A        %7D%0A        Map%3CString, 
 Translat
@@ -1887,17 +1887,20 @@
 tion
-s = q.get
+Map = create
 Tran
@@ -1907,17 +1907,21 @@
 slations
-(
+Map(q
 );%0A     
@@ -1941,16 +1941,26 @@
 lation t
+ranslation
 : transl
@@ -1964,17 +1964,28 @@
 nslation
-s
+Map.values()
 ) %7B%0A    
@@ -1996,254 +1996,685 @@
     
-if (Translation.ParentType.QUESTION_TIP.equals(t.getParentType())) %7B%0A      
+altText.add(new XmlAltText(translation));%0A        %7D%0A    %7D%0A%0A    /**%0A     * This allows to filter out duplicated translations if any%0A     * @param q%0A     * @return%0A
      
+*/%0A
     
- if (altText == null) %7B%0A                    altText = new ArrayList%3C%3E();%0A                %7D%0A                altText.add(new XmlAltText(t));%0A            %7D%0A        %7D
+private Map%3CString, Translation%3E createTranslationsMap(Question q) %7B%0A        Map%3CString, Translation%3E translationMap = new HashMap%3C%3E();%0A        List%3CTranslation%3E translations = q.getTranslations();%0A        if (translations != null) %7B%0A            for (Translation t: translations) %7B%0A                if (Translation.ParentType.QUESTION_TIP.equals(t.getParentType())) %7B%0A                    translationMap.put(t.getLanguageCode(), t);%0A                %7D%0A            %7D%0A        %7D%0A        return translationMap;
 %0A   

fix translation duplicates in xml files