/*
 * 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 principal;

import java.awt.Desktop;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.event.MouseInputListener;

/**
 *
 * @author ccandido
 */
public class FormAtualizar extends javax.swing.JFrame {

    /**
     * Creates new form FrameTeste
     */
    public FormAtualizar() {
        initComponents();
        getRootPane().registerKeyboardAction(e -> {
            setVisible(false);

        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
        getRootPane().registerKeyboardAction(e -> {
            setVisible(false);
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, java.awt.event.InputEvent.CTRL_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lblVersao = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblNovaVersao = new javax.swing.JLabel();
        lblLink = new javax.swing.JLabel();
        subPan2 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("principal/Formularios_pt_BR"); // NOI18N
        jLabel1.setText(bundle.getString("Atualizador.lblVersao")); // NOI18N

        lblVersao.setText("vv");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText(bundle.getString("Atualizador.lblNovaVersao")); // NOI18N

        lblNovaVersao.setText("nv");

        lblLink.setText("jLabel4");
        lblLink.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        subPan2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        subPan2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 2, 2));

        jButton3.setText(bundle.getString("FrameSobre.jButton1.text")); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        subPan2.add(jButton3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(subPan2, javax.swing.GroupLayout.PREFERRED_SIZE, 442, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblVersao))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblNovaVersao))
                    .addComponent(lblLink))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblVersao))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblNovaVersao))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblLink)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(subPan2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblLink;
    private javax.swing.JLabel lblNovaVersao;
    private javax.swing.JLabel lblVersao;
    private javax.swing.JPanel subPan;
    private javax.swing.JPanel subPan1;
    private javax.swing.JPanel subPan2;
    // End of variables declaration//GEN-END:variables

    private String u = "";

    public void Inicie(String v) {
        lblVersao.setText(v);
        lblNovaVersao.setText(v);
        lblLink.setText("");
        try {
            String res = getSiteText();
            String[] sub = res.split("\\|");
            String v2 = sub[0].trim();
            u = sub[1].trim();
            if (v2.equals(v)) {
                lblLink.setText("Versão já atualizada!");
            } else {
                lblNovaVersao.setText(v2);
                lblLink.setText("<html><body><a href=\"" + u + "\">" + u + "</a></body></html>");
            }
        } catch (IOException ex) {
            Logger.getLogger(FormAtualizar.class.getName()).log(Level.SEVERE, null, ex);
            lblLink.setText("Não foi possível conectar ao site do brModelo!");
        }
        
        lblLink.addMouseListener(new MouseInputListener() {
            @Override
            public void mouseClicked(MouseEvent me) {
                if (Desktop.isDesktopSupported() && !u.isEmpty()) {
                    try {
                        Desktop.getDesktop().browse(new URI(u));
                    } catch (IOException | URISyntaxException ex) {
                        Logger.getLogger(FormAtualizar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

            @Override
            public void mousePressed(MouseEvent me) {
                
            }

            @Override
            public void mouseReleased(MouseEvent me) {
            }

            @Override
            public void mouseEntered(MouseEvent me) {
            }

            @Override
            public void mouseExited(MouseEvent me) {
            }

            @Override
            public void mouseDragged(MouseEvent me) {
            }

            @Override
            public void mouseMoved(MouseEvent me) {
            }
        });
    }

    public final String SITE = "http://www.sis4.com/brModelo/versao.htm";

    public String getSiteText() throws MalformedURLException, IOException {
        URL sis4 = new URL(SITE);
        URLConnection yc = sis4.openConnection();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        yc.getInputStream()));
        String inputLine;
        String res = "";
        while ((inputLine = in.readLine()) != null) {
            res += inputLine + "|";
        }
        res += "FIM";
        in.close();
        return res;
    }

    private void open(URI uri) {
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().browse(uri);
            } catch (IOException e) {
                /* TODO: error handling */ }
        } else {
            /* TODO: error handling */ }
    }
}
