/*
 * Copyright (C) 2005 - 2019 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */
package global.namespace.truelicense.swing;

import global.namespace.truelicense.api.License;
import global.namespace.truelicense.ui.LicenseWizardMessage;
import global.namespace.truelicense.api.LicenseValidationException;

import javax.swing.*;
import java.util.Date;

final class DisplayPanel extends LicensePanel {

    private static final long serialVersionUID = 1L;

    private License license;

    DisplayPanel(final LicenseManagementWizard wizard) {
        super(wizard);
        initComponents();
    }

    /** Updates the view with the contents of the license and verifies it. */
    @Override public void onAfterStateSwitch() {
        assert SwingUtilities.isEventDispatchThread();
        assert isVisible();
        try {
            license = manager().load();
            onLicenseChange();
            manager().verify();
        } catch (final Exception failure) {
            JOptionPane.showMessageDialog(
                    this,
                    failure instanceof LicenseValidationException
                        ? failure.getLocalizedMessage()
                        : format(LicenseWizardMessage.display_failure),
                    format(LicenseWizardMessage.failure_title),
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private void onLicenseChange() {
        if (null == license) return;
        subjectComponent.setText(nonNullOrEmptyString(license.getSubject()));
        holderComponent.setText(nonNullOrEmptyString(license.getHolder()));
        issuerComponent.setText(nonNullOrEmptyString(license.getIssuer()));
        issuedComponent.setText(format(license.getIssued()));
        notBeforeComponent.setText(format(license.getNotBefore()));
        notAfterComponent.setText(format(license.getNotAfter()));
        consumerComponent.setText(LicenseWizardMessage.display_consumerFormat.format(
                subject(),
                license.getConsumerType(),
                license.getConsumerAmount()).toString());
        infoComponent.setText(nonNullOrEmptyString(license.getInfo()));
    }

    private static String nonNullOrEmptyString(Object obj) {
        return null != obj ? obj.toString() : "";
    }

    private String format(Date date) {
        return LicenseWizardMessage.display_dateTimeFormat(subject(), date);
    }

    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        javax.swing.JLabel subjectLabel = new javax.swing.JLabel();
        subjectComponent = new javax.swing.JTextArea();
        javax.swing.JLabel holderLabel = new javax.swing.JLabel();
        javax.swing.JScrollPane holderScrollPane = new javax.swing.JScrollPane();
        holderComponent = new javax.swing.JTextArea();
        javax.swing.JLabel issuerLabel = new javax.swing.JLabel();
        javax.swing.JScrollPane issuerScrollPane = new javax.swing.JScrollPane();
        issuerComponent = new javax.swing.JTextArea();
        javax.swing.JLabel issuedLabel = new javax.swing.JLabel();
        issuedComponent = new javax.swing.JTextArea();
        javax.swing.JLabel notBeforeLabel = new javax.swing.JLabel();
        notBeforeComponent = new javax.swing.JTextArea();
        javax.swing.JLabel notAfterLabel = new javax.swing.JLabel();
        notAfterComponent = new javax.swing.JTextArea();
        javax.swing.JLabel consumerLabel = new javax.swing.JLabel();
        consumerComponent = new javax.swing.JTextArea();
        javax.swing.JLabel infoLabel = new javax.swing.JLabel();
        javax.swing.JScrollPane infoScrollPane = new javax.swing.JScrollPane();
        infoComponent = new javax.swing.JTextArea();

        setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createTitledBorder(format(LicenseWizardMessage.display_title)), javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10))); // NOI18N
        setName(DisplayPanel.class.getSimpleName());
        setLayout(new java.awt.GridBagLayout());

        subjectLabel.setLabelFor(subjectComponent);
        subjectLabel.setText(format(LicenseWizardMessage.display_subject)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(subjectLabel, gridBagConstraints);

        subjectComponent.setEditable(false);
        subjectComponent.setFont(getFont());
        subjectComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        subjectComponent.setName(LicenseWizardMessage.display_subject.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(subjectComponent, gridBagConstraints);

        holderLabel.setLabelFor(holderComponent);
        holderLabel.setText(format(LicenseWizardMessage.display_holder)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(holderLabel, gridBagConstraints);

        holderScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        holderScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        holderScrollPane.setPreferredSize(new java.awt.Dimension(300, 65));

        holderComponent.setEditable(false);
        holderComponent.setFont(getFont());
        holderComponent.setLineWrap(true);
        holderComponent.setWrapStyleWord(true);
        holderComponent.setBorder(null);
        holderComponent.setName(LicenseWizardMessage.display_holder.name());
        holderScrollPane.setViewportView(holderComponent);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(holderScrollPane, gridBagConstraints);

        issuerLabel.setLabelFor(issuerComponent);
        issuerLabel.setText(format(LicenseWizardMessage.display_issuer)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(issuerLabel, gridBagConstraints);

        issuerScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        issuerScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        issuerScrollPane.setPreferredSize(new java.awt.Dimension(300, 65));

        issuerComponent.setEditable(false);
        issuerComponent.setFont(getFont());
        issuerComponent.setLineWrap(true);
        issuerComponent.setWrapStyleWord(true);
        issuerComponent.setBorder(null);
        issuerComponent.setName(LicenseWizardMessage.display_issuer.name());
        issuerScrollPane.setViewportView(issuerComponent);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(issuerScrollPane, gridBagConstraints);

        issuedLabel.setLabelFor(issuedComponent);
        issuedLabel.setText(format(LicenseWizardMessage.display_issued)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(issuedLabel, gridBagConstraints);

        issuedComponent.setEditable(false);
        issuedComponent.setFont(getFont());
        issuedComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        issuedComponent.setName(LicenseWizardMessage.display_issued.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(issuedComponent, gridBagConstraints);

        notBeforeLabel.setLabelFor(notBeforeComponent);
        notBeforeLabel.setText(format(LicenseWizardMessage.display_notBefore)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(notBeforeLabel, gridBagConstraints);

        notBeforeComponent.setEditable(false);
        notBeforeComponent.setFont(getFont());
        notBeforeComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        notBeforeComponent.setName(LicenseWizardMessage.display_notBefore.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(notBeforeComponent, gridBagConstraints);

        notAfterLabel.setLabelFor(notAfterComponent);
        notAfterLabel.setText(format(LicenseWizardMessage.display_notAfter)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(notAfterLabel, gridBagConstraints);

        notAfterComponent.setEditable(false);
        notAfterComponent.setFont(getFont());
        notAfterComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        notAfterComponent.setName(LicenseWizardMessage.display_notAfter.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(notAfterComponent, gridBagConstraints);

        consumerLabel.setLabelFor(consumerComponent);
        consumerLabel.setText(format(LicenseWizardMessage.display_consumer)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(consumerLabel, gridBagConstraints);

        consumerComponent.setEditable(false);
        consumerComponent.setFont(getFont());
        consumerComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        consumerComponent.setName(LicenseWizardMessage.display_consumer.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(consumerComponent, gridBagConstraints);

        infoLabel.setLabelFor(infoComponent);
        infoLabel.setText(format(LicenseWizardMessage.display_info)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(infoLabel, gridBagConstraints);

        infoScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        infoScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        infoScrollPane.setPreferredSize(new java.awt.Dimension(300, 65));

        infoComponent.setEditable(false);
        infoComponent.setFont(getFont());
        infoComponent.setLineWrap(true);
        infoComponent.setWrapStyleWord(true);
        infoComponent.setBorder(null);
        infoComponent.setName(LicenseWizardMessage.display_info.name());
        infoScrollPane.setViewportView(infoComponent);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(infoScrollPane, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea consumerComponent;
    private javax.swing.JTextArea holderComponent;
    private javax.swing.JTextArea infoComponent;
    private javax.swing.JTextArea issuedComponent;
    private javax.swing.JTextArea issuerComponent;
    private javax.swing.JTextArea notAfterComponent;
    private javax.swing.JTextArea notBeforeComponent;
    private javax.swing.JTextArea subjectComponent;
    // End of variables declaration//GEN-END:variables
}
