package be.msyben.vinylManager.tools.importexport;

import be.msyben.vinylManager.tools.*;
import be.msyben.vinylManager.entities.Album;
import be.msyben.vinylManager.entities.Collection;
import be.msyben.vinylManager.entities.Disque;
import be.msyben.vinylManager.entities.Image;
import be.msyben.vinylManager.entities.Titre;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Export en xml.
 * <p/>
 * @author Mathieu Syben
 */
public class XmlExport {

    /**
     * Exporte la collection courante en xml
     * <p/>
     * @param f Le fichier devant contenir l'xml
     */
    public static void exporterXML(File f, Collection collection) {

        String name = f.getName().replace(".xml", "");
        String repName = name + "_images";
        String imagesRepName = f.getParent() + System.getProperty("file.separator") + repName;
        File imagesRep = new File(imagesRepName);
        imagesRep.mkdir();

        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        sb.append("<collection>");
        sb.append("\r\n");

        int i = 1;

        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

        for (Album a : collection.getAlbums()) {
            sb.append("\t<album>");
            sb.append("\r\n");

            sb.append("\t\t<artiste>");
            sb.append(getValue(a.getArtisteForList()));
            sb.append("</artiste>");
            sb.append("\r\n");

            sb.append("\t\t<titre>");
            sb.append(getValue(a.getTitre()));
            sb.append("</titre>");
            sb.append("\r\n");

            sb.append("\t\t<compositeur>");
            sb.append(getValue(a.getCompositeur()));
            sb.append("</compositeur>");
            sb.append("\r\n");

            sb.append("\t\t<parolier>");
            sb.append(getValue(a.getParolier()));
            sb.append("</parolier>");
            sb.append("\r\n");

            sb.append("\t\t<location>");
            sb.append(getValue(a.getLocation()));
            sb.append("</location>");
            sb.append("\r\n");

            sb.append("\t\t<annee>");
            sb.append(getValue(a.getAnnee()));
            sb.append("</annee>");
            sb.append("\r\n");

            sb.append("\t\t<anneeParution>");
            sb.append(getValue(a.getAnneeParution()));
            sb.append("</anneeParution>");
            sb.append("\r\n");

            sb.append("\t\t<pays>");
            sb.append(getValue(a.getPays()));
            sb.append("</pays>");
            sb.append("\r\n");

            sb.append("\t\t<commentaire>");
            sb.append(getValue(a.getCommentaire()));
            sb.append("</commentaire>");
            sb.append("\r\n");

            sb.append("\t\t<genre>");
            sb.append(getValue(a.getGenre()));
            sb.append("</genre>");
            sb.append("\r\n");

            sb.append("\t\t<editeur>");
            sb.append(getValue(a.getEditeur()));
            sb.append("</editeur>");
            sb.append("\r\n");

            sb.append("\t\t<etat>");
            sb.append(getValue(a.getEtat()));
            sb.append("</etat>");
            sb.append("\r\n");

            sb.append("\t\t<musiciens>");
            sb.append(getValue(a.getMusiciens()));
            sb.append("</musiciens>");
            sb.append("\r\n");

            sb.append("\t\t<chef>");
            sb.append(getValue(a.getChef()));
            sb.append("</chef>");
            sb.append("\r\n");

            sb.append("\t\t<orchestre>");
            sb.append(getValue(a.getOrchestre()));
            sb.append("</orchestre>");
            sb.append("\r\n");

            sb.append("\t\t<reference>");
            sb.append(getValue(a.getReference()));
            sb.append("</reference>");
            sb.append("\r\n");

            sb.append("\t\t<cote>");
            sb.append(getValue(a.getCote(), a.getCoteDemi()));
            sb.append("</cote>");
            sb.append("\r\n");

            sb.append("\t\t<valeur>");
            sb.append(getValue(a.getValeur()));
            sb.append("</valeur>");
            sb.append("\r\n");

            sb.append("\t\t<support>");
            sb.append(getValue(a.getSupport()));
            sb.append("</support>");
            sb.append("\r\n");

            sb.append("\t\t<compilation>");
            sb.append(a.getCompliation());
            sb.append("</compilation>");
            sb.append("\r\n");

            sb.append("\t\t<dateAchat>");
            sb.append((a.getDateAchat() == null) ? "" : df.format(a.getDateAchat()));
            sb.append("</dateAchat>");
            sb.append("\r\n");

            sb.append("\t\t<possede>");
            sb.append(a.getPossede());
            sb.append("</possede>");
            sb.append("\r\n");

            sb.append("\t\t<disques>");
            sb.append("\r\n");
            for (Disque d : a.getDisques()) {
                sb.append("\t\t\t<disque>");
                sb.append("\r\n");
                sb.append("\t\t\t\t<num>");
                sb.append(getValue(d.getNum()));
                sb.append("</num>");
                sb.append("\r\n");
                sb.append("\t\t\t\t<disqueEtat>");
                sb.append(getValue(d.getEtat()));
                sb.append("</disqueEtat>");
                sb.append("\r\n");

                sb.append("\t\t\t\t<couleur>");
                sb.append(getValue(d.getCouleur()));
                sb.append("</couleur>");
                sb.append("\r\n");

                sb.append("\t\t\t\t<vitesse>");
                sb.append(getValue(d.getVitesse()));
                sb.append("</vitesse>");
                sb.append("\r\n");

                sb.append("\t\t\t\t<taille>");
                sb.append(getValue(d.getTaille()));
                sb.append("</taille>");
                sb.append("\r\n");

                sb.append("\t\t\t\t<canaux>");
                sb.append(getValue(d.getCanaux()));
                sb.append("</canaux>");
                sb.append("\r\n");

                sb.append("\t\t\t\t<faceA>");
                sb.append("\r\n");
                for (Titre t : d.getFaceA()) {
                    sb.append("\t\t\t\t\t<plage>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<duree>");
                    sb.append(getValue(t.getDuree()));
                    sb.append("</duree>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<numero>");
                    sb.append(getValue(t.getNum()));
                    sb.append("</numero>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<titre>");
                    sb.append(getValue(t.getNom()));
                    sb.append("</titre>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<artiste>");
                    sb.append(getValue(t.getArtiste()));
                    sb.append("</artiste>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<annee>");
                    sb.append(getValue(t.getAnnee()));
                    sb.append("</annee>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<paroles>");
                    sb.append(getValue(t.getLyrics()));
                    sb.append("</paroles>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t</plage>");
                    sb.append("\r\n");
                }
                sb.append("\t\t\t\t</faceA>");
                sb.append("\r\n");
                sb.append("\t\t\t\t<faceB>");
                sb.append("\r\n");
                for (Titre t : d.getFaceB()) {
                    sb.append("\t\t\t\t\t<plage>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<duree>");
                    sb.append(getValue(t.getDuree()));
                    sb.append("</duree>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<numero>");
                    sb.append(getValue(t.getNum()));
                    sb.append("</numero>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<titre>");
                    sb.append(getValue(t.getNom()));
                    sb.append("</titre>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<artiste>");
                    sb.append(getValue(t.getArtiste()));
                    sb.append("</artiste>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<annee>");
                    sb.append(getValue(t.getAnnee()));
                    sb.append("</annee>");
                    sb.append("\r\n");
                    sb.append("\t\t\t\t\t\t<paroles>");
                    sb.append(getValue(t.getLyrics()));
                    sb.append("</paroles>");
                    sb.append("\t\t\t\t\t</plage>");
                    sb.append("\r\n");
                }
                sb.append("\t\t\t\t</faceB>");
                sb.append("\r\n");

                sb.append("\t\t\t</disque>");
                sb.append("\r\n");
            }
            sb.append("\t\t</disques>");
            sb.append("\r\n");

            if (a.getImage() != null) {
                sb.append("\t\t<image>");
                sb.append("\r\n");
                sb.append("\t\t\t<nom>");
                sb.append(getValue(a.getImage().getNom()));
                sb.append("</nom>");
                sb.append("\r\n");
                sb.append("\t\t\t<fichier>");
                String fname = repName + System.getProperty("file.separator") + i + ".jpg";
                sb.append(fname);

                Tools.writeFile(new File(imagesRepName + System.getProperty("file.separator") + i + ".jpg"), a.getImage().getContenu());
                i++;
                sb.append("</fichier>");
                sb.append("\r\n");
                sb.append("\t\t</image>");
                sb.append("\r\n");
            }

            if (a.getOtherImages() != null) {
                for (Image image : a.getOtherImages()) {
                    sb.append("\t\t<image>");
                    sb.append("\r\n");
                    sb.append("\t\t\t<nom>");
                    sb.append(getValue(image.getNom()));
                    sb.append("</nom>");
                    sb.append("\r\n");
                    sb.append("\t\t\t<fichier>");
                    String fname = repName + System.getProperty("file.separator") + i + ".jpg";
                    sb.append(fname);

                    Tools.writeFile(new File(imagesRepName + System.getProperty("file.separator") + i + ".jpg"), image.getContenu());
                    i++;
                    sb.append("</fichier>");
                    sb.append("\r\n");
                    sb.append("\t\t</image>");
                    sb.append("\r\n");
                }
            }

            sb.append("\t</album>");
            sb.append("\r\n");
        }

        sb.append("</collection>");
        Tools.writeFile(f, sb.toString().getBytes(Tools.defaultCharset));
    }

    private static String getValue(String s) {
        if (s == null || s.isEmpty()) {
            return "";
        } else {
            return "<![CDATA[" + s + "]]>";
        }
    }

    private static String getValue(Integer i) {
        if (i == null) {
            return "";
        } else {
            return i.toString();
        }
    }

    /**
     * Préparation d'une cote.
     * <p/>
     * @param i    la cote
     * @param demi x.5 ?
     * @return Sa version en string.
     */
    private static String getValue(Integer i, boolean demi) {
        if (i == null) {
            return "";
        }
        return i + (demi ? ".5" : "");
    }
}
