/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2011.
 *
 * ASTRID Research
 * Headquaters: Csapo utca 42. Debrecen H-4029 HUNGARY
 * Phone: +36-52-533-173        Fax: +36-52-533-174
 * Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DebGen Biobank Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.patientmanagement.domain.modules.requests;

import elements.Functions;
import hu.astrid.debgen.patientmanagement.domain.sample.Sample;
import hu.astrid.debgen.patientmanagement.domain.sample.SampleType;
import hu.astrid.xmltools.beans.XmlConvertable;
import hu.astrid.xmltools.beantoxml.XmlToBeanConverter;

import java.util.*;
import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author inagy
 */
public class LabelList implements XmlConvertable {

    private HashMap<String, Integer> labels = new HashMap<String, Integer>();

    //TODO: default konstruktor peldanyositja az osztalyt, viszont az initializeLabels allitja be.
    //ez inkonzisztens allapotot eredmenyezhet.
    public void initializeLabels(HttpServletRequest request, String patientCode, List<SampleType> types) {
        for (SampleType sample : types) {
            try {
                String start = Functions.encodeString(request.getParameter("start" + sample.getOneLetterCode()));
                String number = Functions.encodeString(request.getParameter("number" + sample.getOneLetterCode()));
                String value = Functions.encodeString(request.getParameter("copies" + sample.getOneLetterCode()));
                int amount = Integer.parseInt(value);
                int min = Integer.parseInt(start);
                int max = Integer.parseInt(number) + min - 1;
                java.text.DecimalFormat nft = new java.text.DecimalFormat("#000.###");
                nft.setDecimalSeparatorAlwaysShown(false);
                for (int i = min; i <= max; i++) {
                    if (amount > 0) {
                        labels.put(sample.getOneLetterCode() + patientCode + nft.format(i), amount);
                    }
                }
            } catch (Exception ex) {
                //IGNORED
            }
        }
    }

    private int patientLabels = 0;

    public HashMap<String, Integer> getLabels() {
        return labels;
    }

    public void setLabels(HashMap<String, Integer> labels) {
        this.labels = labels;
    }

    public int getPatientLabels() {
        return patientLabels;
    }

    public void setPatientLabels(int patientLabels) {
        this.patientLabels = patientLabels;
    }

    // <editor-fold defaultstate="collapsed" desc="XML conversion">

    public void buildFromXml(Node root) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void buildFromXml(Node root, String nameSpace) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Node toXml(Document doc) throws Exception {
        return toXml(doc, "");
    }

    public Node toXml(Document doc, String nameSpace) throws Exception {
        String prefix = nameSpace == null || nameSpace.isEmpty() ? "" : nameSpace + ":";
        Node label = doc.createElement(prefix + "labels");
        List<String> list = new ArrayList<String>(labels.keySet());
        Collections.sort(list);
        for (String key : list) {
            Element sc = doc.createElement(prefix + "sampleCode");
            Element code = doc.createElement(prefix + "codevalue");
            sc.setAttribute("copies", "" + labels.get(key));
            code.appendChild(doc.createTextNode(key));
            sc.appendChild(code);
            label.appendChild(sc);
        }
//        Don't needed anymore, because of the solution of the ReportPrintLibs bug
//        Element sc = doc.createElement(prefix + "sampleCode");
//        Element code = doc.createElement(prefix + "codevalue");
//        sc.setAttribute("copies", "" + 0);
//        sc.appendChild(code);
//        label.appendChild(sc);
        return label;
    }

    public XmlToBeanConverter getXmlConverter() throws Exception {
        return new XmlToBeanConverter();
    }// </editor-fold>

    public Set<String> getCodeSet() {
        return this.getLabels().keySet();
    }

    public void buildFromSampleSet(Set<Sample> samples) {
        for (Sample s : samples) {
            labels.put(s.getSampleCode(), 0);
        }
    }
}
