/*
 * 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.service;

import static adgsyncms.repository.db._connection;
import static adgsyncms.repository.db.openConnection;
import adgsyncms.repository.repository;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.HashMap;

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

    boolean b_result = true;
    public static String Username = "adgsyncapp", Password = "kcvdhsfrrwzfcjtm";
    public String Subject, Mensaje, To, company_id;
    String s_Compania, _SQLQuery, s_NombreCompania;

    public Email() {
        repository repo1 = new repository();
        HashMap hashMap = repo1.readSetting();

        this.To = hashMap.get("to").toString();
        this.company_id = hashMap.get("company").toString();
    }

    public String GetCompany() {
        PreparedStatement ps;
        ResultSet rs;
        String Nombre_compania = "";
        _SQLQuery = "select cia_nombre from sis_companias where cia_codigo = ?::varchar ";
        try {
            if (_connection == null || _connection.isClosed()) {
                openConnection();
            }

            ps = _connection.prepareStatement(_SQLQuery);
            ps.setString(1, company_id);
            rs = ps.executeQuery();
            while (rs.next()) {
                Nombre_compania = rs.getString("cia_nombre");
            }
        } catch (SQLException ex) {
            String MsgError = ex.getMessage();
        }
        return Nombre_compania;
    }

    public boolean EnviarCorreo() {

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(Username, Password);
                    }
                });

        try {
            s_NombreCompania = GetCompany();
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(Username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(To));
            message.setSubject(s_NombreCompania + " - " + Subject);
            message.setText(Mensaje);

            Transport.send(message);

        } catch (MessagingException e) {
            this.b_result = false;
//            throw new RuntimeException(e);

        }

        return this.b_result;
    }

    public String getSubject() {
        return Subject;
    }

    public void setSubject(String Subject) {
        this.Subject = Subject;
    }

    public String getMensaje() {
        return Mensaje;
    }

    public void setMensaje(String Mensaje) {
        this.Mensaje = Mensaje;
    }

    public String getTo() {
        return To;
    }

    public void setTo(String To) {
        this.To = To;
    }

}
