package compare;

import javax.xml.parsers.DocumentBuilderFactory;

import com.capgemini.framework.teamcenter.authent.AuthentPropLoader;
import lombok.val;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;



public  class Comparer_XML {

    private static String URLXmlComp = AuthentPropLoader.getString("com.capgemini.report.location.default.outputXml");;


    public static void writerFile(String entry, CriteresComparaison criteresComparaison){

        String filePath = entry;
        File xmlFile = new File(filePath);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();
            NodeList node = doc.getElementsByTagName("TestUnitaire");
            Element rap = null;
            Element plateforme;
            Element report;
            Element typeFile;
            Element UseCase;
            Element nameFileWithDate;
            Element locationFileReference;
            Element nameSheetFileReference;

            for (int i = 0; i < node.getLength(); i++) {
                rap = (Element) node.item(i);
                plateforme = doc.createElement("plateforme");
                plateforme.setAttribute("name",criteresComparaison.getPlateforme());
                report = doc.createElement("report");
                report.setAttribute("id",criteresComparaison.getReport());
                typeFile = doc.createElement("typeFile");
                typeFile.setAttribute("name",criteresComparaison.getTypeFile());
                UseCase = doc.createElement("UseCase");
                UseCase.setAttribute("id",criteresComparaison.getUseCase());
                nameFileWithDate = doc.createElement("nameFileWithDate");
                nameFileWithDate.appendChild(doc.createTextNode(criteresComparaison.getNameFileWithDate()));
                locationFileReference = doc.createElement("locationFileGenerated");
                locationFileReference.appendChild(doc.createTextNode(criteresComparaison.getLocationFileReference()));
                nameSheetFileReference = doc.createElement("nameSheetFileGenerated");
                nameSheetFileReference.appendChild(doc.createTextNode(criteresComparaison.getNameSheetFileReference()));
                UseCase.appendChild(nameFileWithDate);
                UseCase.appendChild(locationFileReference);
                UseCase.appendChild(nameSheetFileReference);
                typeFile.appendChild(UseCase);
                report.appendChild(typeFile);
                plateforme.appendChild(report);
                rap.appendChild(plateforme);

            }

            doc.getDocumentElement().normalize();
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(URLXmlComp));
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(source, result);


        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }


    public static void creerEntry(String nomEntre) {

        try {
            DocumentBuilderFactory XML_Comparaison = DocumentBuilderFactory.newInstance();
            DocumentBuilder XML_Comp = XML_Comparaison.newDocumentBuilder();
            Document XML_Document = XML_Comp.newDocument();
            Element TestUnitaire = XML_Document.createElement("TestUnitaire");
            XML_Document.appendChild(TestUnitaire);
            TransformerFactory XMLTransformeur = TransformerFactory.newInstance();
            Transformer XML_Transformeur = XMLTransformeur.newTransformer();
            DOMSource source = new DOMSource(XML_Document);
            StreamResult resultat = new StreamResult(new File(nomEntre));
            XML_Transformeur.transform(source, resultat);
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        }
    }

}
