/*
 * Copyright (C) 2014 Evgeniy Egorov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ru.apertum.qsystem.client.common;

import java.awt.GridLayout;
import java.io.IOException;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.web.HTMLEditor;
import javax.imageio.ImageIO;
import ru.apertum.qsystem.client.forms.FAdmin;
import ru.apertum.qsystem.common.Uses;

/**
 * @author Evgeniy Egorov
 */
public class WysiwygDlg extends javax.swing.JDialog {

    private final JFXPanel javafxPanel;
    private HTMLEditor htmlEditor;
    private String result;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton OKbutton;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel panel;

    private WysiwygDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        try {
            setIconImage(ImageIO
                .read(FAdmin.class
                    .getResource("/ru/apertum/qsystem/client/forms/resources/admin.png")));
        } catch (IOException ex) {
            System.err.println(ex);
        }

        panel.removeAll();
        javafxPanel = new JFXPanel();
        Platform.runLater(() -> {
            htmlEditor = new HTMLEditor();
            Scene scene = new Scene(htmlEditor, 750, 500, Color.web("#666970"));
            javafxPanel.setScene(scene);
        });
        final GridLayout gl = new GridLayout(1, 1);
        panel.setLayout(gl);
        panel.add(javafxPanel);
    }

    public static String showInstance(String data) {
        final WysiwygDlg dlg = WysiwygDlgHolder.INSTANCE;
        dlg.setResult(data);
        Uses.setLocation(dlg);
        dlg.setVisible(true);
        return dlg.getResult();
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        Platform.runLater(() -> {
            htmlEditor.setHtmlText(result);
        });
        this.result = result;
    }

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

        panel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        OKbutton = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
            .getInstance(ru.apertum.qsystem.QSystem.class).getContext()
            .getResourceMap(WysiwygDlg.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        panel.setBorder(new javax.swing.border.MatteBorder(null));
        panel.setName("panel"); // NOI18N

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 394, Short.MAX_VALUE)
        );

        jPanel2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel2.setName("jPanel2"); // NOI18N

        OKbutton.setText(resourceMap.getString("OKbutton.text")); // NOI18N
        OKbutton.setName("OKbutton"); // NOI18N
        OKbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKbuttonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                    jPanel2Layout.createSequentialGroup()
                        .addContainerGap(605, Short.MAX_VALUE)
                        .addComponent(OKbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 98,
                            javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                    jPanel2Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(OKbutton)
                        .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(0, 0, 0)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE))
        );

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

    private void OKbuttonActionPerformed(
        java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKbuttonActionPerformed
        result = htmlEditor.getHtmlText();
        result = "<html> " + result.replaceAll("<.?(html).*?>", "").
            replaceAll("(<.?(head).*?>).*?(<.?(head).*?>)", "").
            replaceAll("<.?(body).*?>", "").
            replaceAll("<p\\S*?", "\n<p ").replaceAll("<(br|BR)>", "\n<br>\n")
            .replaceAll("\n{2,}", "\n");
        setVisible(false);
    }//GEN-LAST:event_OKbuttonActionPerformed

    private static class WysiwygDlgHolder {

        private static final WysiwygDlg INSTANCE = new WysiwygDlg(null, true);
    }
    // End of variables declaration//GEN-END:variables
}