/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package adgsyncms.repository;

import java.util.List;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author amartinez
 */
public class repository {

    public List<Servers> lstservidores = new ArrayList<>();
    public String Company;

    public HashMap<String, String> readSetting() {

        HashMap<String, String> _hashMap = new HashMap<>();

        try {

            File _file = new File("setting.xml");

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(_file);

            doc.getDocumentElement().normalize();

            //get list node webservices
            NodeList nList = doc.getElementsByTagName("database");
            //get first node from list
            Node nNode = nList.item(0);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                _hashMap.put("db_host_name", eElement.getElementsByTagName("db_host_name").item(0).getTextContent());
                _hashMap.put("db_name", eElement.getElementsByTagName("db_name").item(0).getTextContent());
                _hashMap.put("db_port", eElement.getElementsByTagName("db_port").item(0).getTextContent());
                _hashMap.put("db_user", eElement.getElementsByTagName("db_user").item(0).getTextContent());
                _hashMap.put("db_secret", eElement.getElementsByTagName("db_secret").item(0).getTextContent());
                _hashMap.put("company", eElement.getElementsByTagName("company").item(0).getTextContent());
                Company = eElement.getElementsByTagName("company").item(0).getTextContent();
            }

            //get notify email list node
            nList = doc.getElementsByTagName("email_notify");
            //get first node from list
            nNode = nList.item(0);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                _hashMap.put("to", eElement.getElementsByTagName("to").item(0).getTextContent());
                _hashMap.put("notify_alive_minutes", eElement.getElementsByTagName("notify_alive_minutes").item(0).getTextContent());
                _hashMap.put("notify_alive_mail", eElement.getElementsByTagName("notify_alive_mail").item(0).getTextContent());

            }

            //get list node database
            nList = doc.getElementsByTagName("working_schedule");
            //get first node from list
            nNode = nList.item(0);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                _hashMap.put("work_in_hour", eElement.getElementsByTagName("work_in_hour").item(0).getTextContent());
                _hashMap.put("work_in_minutes", eElement.getElementsByTagName("work_in_minutes").item(0).getTextContent());
                _hashMap.put("work_out_hour", eElement.getElementsByTagName("work_out_hour").item(0).getTextContent());
                _hashMap.put("work_out_minutes", eElement.getElementsByTagName("work_out_minutes").item(0).getTextContent());
                _hashMap.put("ws_host_minute", eElement.getElementsByTagName("ws_host_minute").item(0).getTextContent());
            }
//get list node database
            nList = doc.getElementsByTagName("sales_sync");
            //get first node from list
            nNode = nList.item(0);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                _hashMap.put("start_hour", eElement.getElementsByTagName("start_hour").item(0).getTextContent());
                _hashMap.put("start_minute", eElement.getElementsByTagName("start_minute").item(0).getTextContent());
                _hashMap.put("end_hour", eElement.getElementsByTagName("end_hour").item(0).getTextContent());
                _hashMap.put("end_minute", eElement.getElementsByTagName("end_minute").item(0).getTextContent());
            }

            nList = doc.getElementsByTagName("servidores");
            for (int temp = 0; temp < nList.getLength(); temp++) {

                nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    Servers servidor = new Servers();
                    servidor.host_name = eElement.getAttribute("host_name");
                    servidor.database_name = eElement.getAttribute("database_name");
                    servidor.port_number = eElement.getAttribute("port_number");
                    servidor.user_name = eElement.getAttribute("user_name");
                    servidor.user_pwd = eElement.getAttribute("user_pwd");
                    servidor.cajas = eElement.getAttribute("caja");
                    servidor.alias = eElement.getAttribute("alias");
                    lstservidores.add(servidor);
                }
            }
        } catch (Exception e) {
            message.show(e.getMessage());
        }

        //return hashMap
        return _hashMap;
    }

    // convert from UTF-8 -> internal Java String format
    public static String convertFromUTF8(String s) {
        String out = null;
        try {
            out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            return null;
        }
        return out;
    }

    // convert from internal Java String format -> UTF-8
    public static String convertToUTF8(String s) {
        String out = null;
        try {
            //prepare parameters
            out = URLEncoder.encode(s.toString(), "UTF-8");
        } catch (UnsupportedEncodingException ex) {

        }
        return out;
    }

    public static String removeLastChar(String str) {
        if (str.length() > 0 && str.charAt(str.length() - 1) == ',') {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }

    public static Date GetLastDayofMonth(Integer ano, Integer mes) {

        Date _UltimoDia = new Date();
        Calendar oCalendar = Calendar.getInstance();
        oCalendar.set(ano, mes - 1, 1);

//        oCalendar.set(Calendar.YEAR,ano);
//        oCalendar.set(Calendar.MONTH, mes );
//        oCalendar.set(Calendar.DAY_OF_MONTH, 1);
        oCalendar.set(Calendar.DATE, oCalendar.getActualMaximum(oCalendar.DAY_OF_MONTH));
        _UltimoDia = oCalendar.getTime();

        return _UltimoDia;
    }
}
