package H0526;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

import javax.xml.parsers.*;
import org.w3c.dom.*;


public class MehrToolz {
	
	public void xmlRead(int id) {
		 try {
	            // creating a constructor of file class and
	            // parsing an XML file
	            File file = new File("geeks.xml");
	          
	            // Defines a factory API that enables
	            // applications to obtain a parser that produces
	            // DOM object trees from XML documents.
	            DocumentBuilderFactory dbf
	                = DocumentBuilderFactory.newInstance();
	          
	            // we are creating an object of builder to parse
	            // the  xml file.
	            DocumentBuilder db = dbf.newDocumentBuilder();
	            Document doc = db.parse(file);

	            /*here normalize method Puts all Text nodes in
	            the full depth of the sub-tree underneath this
	            Node, including attribute nodes, into a "normal"
	            form where only structure separates
	            Text nodes, i.e., there are neither adjacent
	            Text nodes nor empty Text nodes. */
	            doc.getDocumentElement().normalize();
	            System.out.println(
	                "Root element: "
	                + doc.getDocumentElement().getNodeName());
	          
	            // Here nodeList contains all the nodes with
	            // name geek.
	            NodeList nodeList
	                = doc.getElementsByTagName("geek");
	          
	            // Iterate through all the nodes in NodeList
	            // using for loop.
	            int currentID=0;
	            for (int i = 0; i < nodeList.getLength(); ++i) {
	                Node node = nodeList.item(i);
	                if (node.getNodeType()
	                    == Node.ELEMENT_NODE) {
	                    Element tElement = (Element)node;
	                    String idString=tElement.getElementsByTagName("id").item(0).getTextContent();
	                    
	                    currentID=Integer.parseInt(idString);
	                    if(currentID==id) {
	                    System.out.println(
	                        "User id: "
	                        + tElement
	                              .getElementsByTagName("id")
	                              .item(0)
	                              .getTextContent());
	                    System.out.println(
	                        "User Name: "
	                        + tElement
	                               .getElementsByTagName(
	                                  "username")
	                              .item(0)
	                              .getTextContent());
	                    System.out.println(
	                        "Enrolled Course: "
	                        + tElement
	                              .getElementsByTagName(
	                                  "EnrolledCourse")
	                              .item(0)
	                              .getTextContent());
	                    System.out.println(
	                        "Mode: "
	                        + tElement
	                              .getElementsByTagName("mode")
	                              .item(0)
	                              .getTextContent());
	                    System.out.println(
	                        "Duration: "
	                        + tElement
	                              .getElementsByTagName(
	                                  "duration")
	                              .item(0)
	                              .getTextContent());
	                }
	                    }//END if currentID is id
	            }
	        }
	      
	        // This exception block catches all the exception
	        // raised.
	        // For example if we try to access a element by a
	        // TagName that is not there in the XML etc.
	        catch (Exception e) {
	            System.out.println(e);
	        }
		
		
	}
	
	public void tester() {
		long currentTimeMillis = System.currentTimeMillis()/1000;
        System.out.println("Current Timestamp: " + currentTimeMillis + " seconds");
        ZoneId zone = ZoneId.systemDefault(); 
        LocalDate today = LocalDate.now(zone); 
        ZonedDateTime startOfDay = today.atStartOfDay(zone); 
        System.out.println("Start of the day: " + startOfDay.toInstant().toEpochMilli()/1000);
        
        
        String tB="Datei nicht geladen.";
        try {
			FileReader fr = new FileReader("testBed.config");
			tB=Files.readString(Path.of("versuch.xml"));
			try {
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        System.out.println("Die geladene testBed.config Datei:\n"
        		+tB);
        
		Properties prop = new Properties();
		
		try {
			prop.load(new FileReader("testBed.config"));
		} catch (FileNotFoundException e) {
			System.out.println("Config File cannot be found in" +System.getProperty("user.dir").toString()+" ..so sad.. \n");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("looks like we cannot read the Config File in" +System.getProperty("user.dir").toString()+" ..dammit!.. \n");
			e.printStackTrace();
		}
		System.out.println(System.getProperty("user.dir").toString());
		if (new File("testBed.config").isFile()) {
			System.out.println("found config file, lets begin");
			System.out.println("It says: " + prop.getProperty("test.message"));
			System.out.println(prop.getProperty("test.zweitens"));
			
			Path path = Paths.get(URI.create("file:/C:/test.txt"));
			System.out.println("1 "+path.getClass());
			System.out.println("2 "+path.getFileSystem().getClass());
			Path currentDir = Paths.get(".");
			System.out.println(currentDir.toAbsolutePath());
		}

	}

}
