import java.io.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
 *
 * @author Tomáš Dlouhý
 * 
 * Program prida do INPUTFILE dalsi polozku
 */
public class manipdom {

	private static String INPUTFILE = "data.xml";
	private static String OUTPUTFILE = "data-zmena.xml";    
    
    public static void main(String[] args) {
        /*for (int i=0;i<args.length;i++) {
            //System.out.println("argument "+ i + " is " + args[i]);
            
        }*/
        
        if (args.length < 2) {
           ShowHelp(); 
        } else {
           String input=args[0];
           String output="null";
           if (args.length >3) { output = args[2]; }
           
           if ( input.startsWith("-in")) {
                File fl = new File(args[1]);
               if (fl.exists() == true)  {
                  INPUTFILE = fl.getName();
                    if (output.startsWith("null")) {
                        RunProgram(false);
                    } else if (output.startsWith("-out")) {
                        OUTPUTFILE = args[3];
                        RunProgram(true);
                        System.out.println("its works");                        
                    } else RunProgram(false);
               } else { System.err.println("Error: Manipdom: iputfile file no found!");}
           }
        }
    }
    
    public static void ShowHelp() {
        System.out.println("Manipdom: No valid syntax");
        System.out.println("    Required format: ");
        System.out.println("        manipdom -in <inputfile.xml>");
        System.out.println("    or");
        System.out.println("        manipdom -in <inputfile.xml> -out <outputfile>");
    }
    
    public static void RunProgram(Boolean isSetOutput) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(false);
            DocumentBuilder builder = dbf.newDocumentBuilder();
            Document doc = builder.parse(INPUTFILE);
            processTree(doc);
            TransformerFactory tf = TransformerFactory.newInstance();			
            Transformer writer = tf.newTransformer();
            writer.setOutputProperty(OutputKeys.ENCODING, "windows-1250");
            if (isSetOutput == true ) {
                writer.transform(new DOMSource(doc), new StreamResult(new File(OUTPUTFILE)));
            }
//            writer.transform(new DOMSource(doc), Syste);
        } catch (Exception e) {
            e.printStackTrace();
	}                
    }

    public static void processTree(Document doc) {
                Scanner scan=new Scanner(System.in); 
                Node data = doc.getDocumentElement();
                
                NodeList nl = doc.getElementsByTagName("movie");
                int ic = nl.getLength()+1;
                Node data1 = nl.item(ic);
                
		Text CRLF = doc.createTextNode("\n  ");
                
                System.out.println("Vlozit do XML pomoci DOM");
                System.out.println("-------------------------");
                
                System.out.println("Zadejte jmeno filmu");
                Text tin = doc.createTextNode(scan.nextLine());
                System.out.println("Zadejte rok uvedeni filmu");
                Text tiy = doc.createTextNode(scan.nextLine());
                System.out.println("Zadejte zanr filmu");
                Text tit = doc.createTextNode(scan.nextLine());
                
                System.out.println("Zadejte jmeno rezisera");
                Text tpd = doc.createTextNode(scan.nextLine());                                
                
		Element movie = doc.createElement("movie");
                Element info = doc.createElement("info");
                Element iname = doc.createElement("name");
                Element iyear = doc.createElement("year");
                Element itype = doc.createElement("type");

                Element per = doc.createElement("personal");
                Element director = doc.createElement("director");
                Element actors = doc.createElement("actors");
                Element actor = doc.createElement("actor");
                Element aname = doc.createElement("name");
                Element aplayed = doc.createElement("played");
                Element aphoto = doc.createElement("photo");

                Element desc = doc.createElement("desc");

                data.appendChild(movie);
                movie.appendChild(CRLF);
                movie.setAttribute("id", String.valueOf(ic));
                movie.appendChild(CRLF);
                
                movie.appendChild(info);
                
                    info.appendChild(iname);
                    iname.appendChild(tin);
                    iname.appendChild(CRLF);
                
                    info.appendChild(iyear);
                    iyear.appendChild(tiy);
                    iyear.appendChild(CRLF);

                    info.appendChild(itype);
                    itype.appendChild(tit);
                    itype.appendChild(CRLF);

                    
                movie.appendChild(per);
                    per.appendChild(director);
                    director.appendChild(tpd);

                    per.appendChild(actors);
                    System.out.println("Pridat dalsiho herce? pro ukonceni napiste: ne");
                    String next = scan.nextLine();
                    while (!next.startsWith("ne")) {
                        
                        System.out.println("Zadejte jmeno herce");
                        Text tan = doc.createTextNode(scan.nextLine());
                        System.out.println("Zadejte jmeno postavy, kterou hral");
                        Text tap = doc.createTextNode(scan.nextLine());
                        System.out.println("Zadejte cestu k obrazku");
                        Text taph = doc.createTextNode(scan.nextLine());
                        
                        actors.appendChild(actor);
                            actor.appendChild(aname);
                            aname.appendChild(tan);
                            aname.appendChild(CRLF);

                            actor.appendChild(aplayed);
                            aplayed.appendChild(tap);
                            aplayed.appendChild(CRLF);

                            actor.appendChild(aphoto);
                            aphoto.appendChild(taph);
                            aphoto.appendChild(CRLF);
                            
                        System.out.println("Pridat dalsiho herce? pro ukonceni napiste: ne");
                        next = scan.nextLine();
                    }
                System.out.println("Zadejte popis filmu");
                Text td = doc.createTextNode(scan.nextLine());
                movie.appendChild(desc);
                desc.appendChild(td);
                desc.appendChild(CRLF);
                data.appendChild(CRLF);
    }
}
