/*
 * Apocalypse Laboratories
 * Open Source License
 *
 * Source code can be used for any purpose, as long as:
 *  - Compiled binaries are rebranded and trademarks are not
 *    visible by the end user at any time, except to give
 *    credit to Apocalypse Laboratories, such as by showing
 *    "Based on <product> by Apocalypse Laboratories" or a
 *    similar notice;
 *  - You do not use the code for evil;
 *  - Rebranded compiled applications have significant
 *    differences in functionality;
 *  - and you provide your modified source code for download,
 *    under the terms of the GNU LGPL v3 or a comparable
 *    license.
 *
 * Compiled binaries cannot be redistributed or mirrored,
 * unless:
 *  - You have written permission from Apocalypse Laboratories;
 *  - Downloads are not available from Apocalypse Laboratories,
 *    not even behind a paywall or other blocking mechanism;
 *  - or you have received a multi-computer license, in which
 *    case you should take measures to prevent unauthorized
 *    downloads, such as preventing download access from the
 *    Internet.
 */
package net.apocalypselabs.symat;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import static net.apocalypselabs.symat.MainGUI.API_URL;

/**
 *
 * @author Skylar
 */
public class FirstRun extends javax.swing.JInternalFrame {

    /**
     * Creates new form FirstRun
     */
    public FirstRun() {
        initComponents();
    }

    /**
     * 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() {

        grp1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        singLicSel = new javax.swing.JRadioButton();
        orgLicSel = new javax.swing.JRadioButton();
        noLicSel = new javax.swing.JRadioButton();
        emailBox = new javax.swing.JTextField();
        emailLbl = new javax.swing.JLabel();
        contBtn = new javax.swing.JButton();
        exitBtn = new javax.swing.JButton();

        setTitle("Welcome");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/net/apocalypselabs/symat/icon16.png"))); // NOI18N
        setMaximumSize(new java.awt.Dimension(435, 350));
        setMinimumSize(new java.awt.Dimension(435, 350));
        setPreferredSize(new java.awt.Dimension(435, 350));

        jLabel1.setText("<html>\n<h1>Welcome to SyMAT!</h1>\n<p>It would be a good idea to get this copy fully activated\n<br />before you go any further.</p>\n<br />\n<p>Do you have a license?</p>");

        grp1.add(singLicSel);
        singLicSel.setText("Yes, I have a single-user license");
        singLicSel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                singLicSelActionPerformed(evt);
            }
        });

        grp1.add(orgLicSel);
        orgLicSel.setText("No, but my school or work might have one");
        orgLicSel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                orgLicSelActionPerformed(evt);
            }
        });

        grp1.add(noLicSel);
        noLicSel.setSelected(true);
        noLicSel.setText("No, I don't have a license");
        noLicSel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noLicSelActionPerformed(evt);
            }
        });

        emailBox.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        emailBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emailBoxActionPerformed(evt);
            }
        });
        emailBox.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                emailBoxKeyPressed(evt);
            }
        });

        emailLbl.setText("Great!  Enter your email address and we'll get things set up.");

        contBtn.setText("Continue");
        contBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contBtnActionPerformed(evt);
            }
        });

        exitBtn.setText("Exit");
        exitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(emailBox, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(noLicSel)
                    .addComponent(orgLicSel)
                    .addComponent(singLicSel)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(emailLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(contBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(exitBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(singLicSel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(orgLicSel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(noLicSel)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailLbl)
                    .addComponent(exitBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(contBtn))
                .addGap(20, 20, 20))
        );

        emailBox.setVisible(false);
        emailLbl.setVisible(false);

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

    private void singLicSelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_singLicSelActionPerformed
        emailLbl.setVisible(true);
        emailBox.setVisible(true);
        emailLbl.setText("Great!  Now just enter your email address.");
    }//GEN-LAST:event_singLicSelActionPerformed

    private void orgLicSelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orgLicSelActionPerformed
        emailLbl.setVisible(true);
        emailBox.setVisible(true);
        emailLbl.setText("OK, enter your school or work email address.");
    }//GEN-LAST:event_orgLicSelActionPerformed

    private void noLicSelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noLicSelActionPerformed
        emailLbl.setVisible(false);
        emailBox.setVisible(false);
    }//GEN-LAST:event_noLicSelActionPerformed

    private void openShop() {
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().browse(new URI(MainGUI.API_URL + "shoplink.php"));
            } catch (IOException | URISyntaxException ex) {
                JOptionPane.showInternalMessageDialog(this, "Please go to symatapp.com to purchase a license.");
            }
        } else {
            JOptionPane.showInternalMessageDialog(this, "Please go to symatapp.com to purchase a license.");
        }
    }

    private void contBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contBtnActionPerformed
        if (noLicSel.isSelected()) {
            if (PrefStorage.getSetting("licensetype").equals("demo") 
                    && PrefStorage.getSetting("licensedemo").equals("used")) {
                int ans = JOptionPane.showInternalConfirmDialog(this,
                        "You have already used a trial license."
                        + "\nWould you like to purchase a license?",
                        "Expired",
                        JOptionPane.YES_NO_OPTION);
                if (ans == JOptionPane.NO_OPTION) {
                    
                } else {
                    openShop();
                }
            } else {
                int ans = JOptionPane.showInternalConfirmDialog(this,
                        "Would you like to open the shop and"
                        + " purchase a license?",
                        "Open Browser",
                        JOptionPane.YES_NO_OPTION);
                if (ans == JOptionPane.NO_OPTION) {
                    Calendar c = Calendar.getInstance();
                    c.setTime(new Date());
                    c.add(Calendar.DATE, 15); // Adds 15 days

                    PrefStorage.saveSetting("licensetype", "demo");
                    PrefStorage.saveSetting("license", String.valueOf(c.getTimeInMillis()));
                    PrefStorage.saveSetting("licensedemo", "used");
                    JOptionPane.showInternalMessageDialog(this,
                            "A trial license of 15 days has been activated.",
                            "Activated",
                            JOptionPane.INFORMATION_MESSAGE);
                    exit();
                } else {
                    openShop();
                }
            }
        } else {
            if (checkRegex()) {
                new CheckThread(emailBox.getText()).start();
            }
        }
    }//GEN-LAST:event_contBtnActionPerformed

    private void emailBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emailBoxActionPerformed
        checkRegex();
    }//GEN-LAST:event_emailBoxActionPerformed

    private void emailBoxKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailBoxKeyPressed
        checkRegex();
    }//GEN-LAST:event_emailBoxKeyPressed

    private void exitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitBtnActionPerformed
        int result = JOptionPane.showInternalConfirmDialog(this,
                "Are you sure you want to exit SyMAT?",
                "Quit",
                JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }//GEN-LAST:event_exitBtnActionPerformed

    private boolean checkRegex() {
        String regex = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]+$";
        if (emailBox.getText().matches(regex)) {
            contBtn.setEnabled(true);
            return true;
        } else {
            contBtn.setEnabled(false);
            return false;
        }
    }

    public void exit() {
        MainGUI.updateNamemark(); // Make sure it displays trial or not
        dispose();
    }

    /**
     * Check the email address for a license.
     */
    private class CheckThread extends Thread {

        private String email;

        public CheckThread(String useremail) {
            email = useremail;
        }

        @Override
        public void run() {
            try {
                Debug.println("Checking license...");
                URL url = new URL(API_URL + "liccheck.php?email=" + email);
                InputStream is = url.openStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));

                String line = br.readLine();
                br.close();
                is.close();

                switch (line) {
                    case "ok:single":
                        success("single");
                        break;
                    case "ok:domain":
                        success("domain");
                        break;
                    case "checkemail":
                        email();
                        break;
                    default:
                        fail();
                }
            } catch (IOException e) {
                Debug.printerr("License check failed.");
                Debug.stacktrace(e);
                ioerror();
            }
        }

        private void success(String type) {
            PrefStorage.saveSetting("license", email);
            PrefStorage.saveSetting("licensetype", type);
            JOptionPane.showInternalMessageDialog(MainGUI.mainPane,
                    "Thank you for activating SyMAT!",
                    "Success",
                    JOptionPane.INFORMATION_MESSAGE);
            exit();
        }

        private void fail() {
            JOptionPane.showInternalMessageDialog(MainGUI.mainPane,
                    "There is not a valid license for that email address.",
                    "Activation failure",
                    JOptionPane.ERROR_MESSAGE);
        }

        private void email() {
            try {
                String code = JOptionPane.showInternalInputDialog(MainGUI.mainPane,
                        "A code has been emailed to you.  "
                        + "Enter it below to verify.",
                        "Almost done!",
                        JOptionPane.QUESTION_MESSAGE);
                Debug.println("Checking license code (" + code + ")...");
                URL url = new URL(API_URL + "emailverify.php?code=" + code + "&email=" + email);
                InputStream is = url.openStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));

                String line = br.readLine();
                br.close();
                is.close();
                if (code.equals(line)) {
                    success("domain");
                } else {
                    JOptionPane.showInternalMessageDialog(MainGUI.mainPane,
                            "Code invalid.",
                            "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException ex) {
                ioerror();
            }
        }

        private void ioerror() {
            JOptionPane.showMessageDialog(MainGUI.mainPane,
                    "An error occured while verifying your license.  "
                    + "Ensure you have an Internet connection and "
                    + "try again later.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton contBtn;
    private javax.swing.JTextField emailBox;
    private javax.swing.JLabel emailLbl;
    private javax.swing.JButton exitBtn;
    private javax.swing.ButtonGroup grp1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton noLicSel;
    private javax.swing.JRadioButton orgLicSel;
    private javax.swing.JRadioButton singLicSel;
    // End of variables declaration//GEN-END:variables
}
