//    uniCenta oPOS  - Touch Friendly Point Of Sale
//    Copyright (c) 2009-2018 uniCenta & previous Openbravo POS works
//    https://unicenta.com
//
//    This file is part of uniCenta oPOS
//
//    uniCenta oPOS 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.
//
//   uniCenta oPOS 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 uniCenta oPOS.  If not, see <http://www.gnu.org/licenses/>.

package com.unicenta.pos.config;

import com.unicenta.data.user.DirtyManager;
import com.unicenta.pos.forms.AppConfig;
import com.unicenta.pos.forms.AppLocal;
import com.unicenta.pos.payment.ConfigPaymentPanelEmpty;
import com.unicenta.pos.payment.PaymentConfiguration;
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author adrianromero
 * @author Mikel Irurita
 */
public class JPanelConfigPayment extends javax.swing.JPanel implements PanelConfig {

    private final DirtyManager dirty = new DirtyManager();
    private final Map<String, PaymentConfiguration> paymentsName = new HashMap<>();
    private PaymentConfiguration paymentConfiguration;

    /** Creates new form JPanelConfigPayment */
    public JPanelConfigPayment() {

        initComponents();

        // dirty manager
        jcboCardReader.addActionListener(dirty);
        jcboPaymentGateway.addActionListener(dirty);

        // Payment Provider
        initPayments("Not defined", new ConfigPaymentPanelEmpty());
        initPayments("external", new ConfigPaymentPanelEmpty());
        initPayments("PaymentSense",  new ConfigPaymentPanelEmpty());
        initPayments("Teya",  new ConfigPaymentPanelEmpty());
        initPayments("MediaPayments",  new ConfigPaymentPanelEmpty());

        // Lector de tarjetas.
        jcboCardReader.addItem("Not defined");
        jcboCardReader.addItem("EMV");
        jcboCardReader.addItem("External");
        jcboCardReader.addItem("Generic");
        jcboCardReader.addItem("Intelligent");
        jcboCardReader.addItem("Keyboard");
        jcboCardReader.addItem("NFC/RFID");
    }

    /**
     *
     * @return
     */
    @Override
    public boolean hasChanged() {
        return dirty.isDirty();
    }

    /**
     *
     * @return
     */
    @Override
    public Component getConfigComponent() {
        return this;
    }

    /**
     *
     * @param config
     */
    @Override
    public void loadProperties(AppConfig config) {

        jcboCardReader.setSelectedItem(config.getProperty("payment.magcardreader"));
        jcboPaymentGateway.setSelectedItem(config.getProperty("payment.gateway"));

        String gateway = comboValue(jcboPaymentGateway.getSelectedItem());
        if ("MediaPayments".equals(gateway)) {
            urlValue.setText(config.getProperty("media-payments.pdq"));
            deviceIdValue.setText(config.getProperty("media-payments.api-key"));
            securityKeyValue.setText(config.getProperty("media-payments.secret"));
        } else {
            urlValue.setText(config.getProperty("payment.URL"));
            deviceIdValue.setText(config.getProperty("payment.device-id"));
            securityKeyValue.setText(config.getProperty("payment.security-key"));
        }
        paymentConfiguration.loadProperties(config);
        dirty.setDirty(false);
    }

    /**
     *
     * @param config
     */
    @Override
    public void saveProperties(AppConfig config) {

        config.setProperty("payment.magcardreader", comboValue(jcboCardReader.getSelectedItem()));
        config.setProperty("payment.gateway", comboValue(jcboPaymentGateway.getSelectedItem()));

        String gateway = comboValue(jcboPaymentGateway.getSelectedItem());
        if ("MediaPayments".equals(gateway)) {
            config.setProperty("media-payments.pdq", urlValue.getText());
            config.setProperty("media-payments.api-key", deviceIdValue.getText());
            config.setProperty("media-payments.secret", securityKeyValue.getText());
        } else {
            config.setProperty("payment.URL", urlValue.getText());
            config.setProperty("payment.device-id", deviceIdValue.getText());
            config.setProperty("payment.security-key", securityKeyValue.getText());
        }
        paymentConfiguration.saveProperties(config);
        dirty.setDirty(false);
    }

    private void initPayments(String name, PaymentConfiguration pc) {
        jcboPaymentGateway.addItem(name);
        paymentsName.put(name, pc);
    }

    private String comboValue(Object value) {
        return value == null ? "" : value.toString();
    }

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

        jLabel13 = new javax.swing.JLabel();
        jcboPaymentGateway = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jcboCardReader = new javax.swing.JComboBox();
        emvConfigPanel = new javax.swing.JPanel();
        urlLabel = new javax.swing.JLabel();
        urlValue = new javax.swing.JTextField();
        deviceIdLabel = new javax.swing.JLabel();
        deviceIdValue = new javax.swing.JTextField();
        securityKeyLabel = new javax.swing.JLabel();
        securityKeyValue = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(590, 450));

        jLabel13.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel13.setText(AppLocal.getIntString("label.paymentgateway")); // NOI18N
        jLabel13.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jLabel13.setPreferredSize(new java.awt.Dimension(150, 30));

        jcboPaymentGateway.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jcboPaymentGateway.setPreferredSize(new java.awt.Dimension(200, 30));
        jcboPaymentGateway.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcboPaymentGatewayActionPerformed(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel11.setText(AppLocal.getIntString("label.cardreader")); // NOI18N
        jLabel11.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jLabel11.setPreferredSize(new java.awt.Dimension(150, 30));

        jcboCardReader.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jcboCardReader.setPreferredSize(new java.awt.Dimension(200, 30));

        emvConfigPanel.setBackground(new java.awt.Color(255, 255, 255));

        urlLabel.setText("URL");
        urlLabel.setFont(new java.awt.Font("Arial", 0, 14));

        urlValue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                urlValueActionPerformed(evt);
            }
        });

        deviceIdLabel.setText("Device ID");
        deviceIdLabel.setFont(new java.awt.Font("Arial", 0, 14));

        securityKeyLabel.setText("Security Key");
        securityKeyLabel.setFont(new java.awt.Font("Arial", 0, 14));

        jLabel1.setFont(new java.awt.Font("Liberation Sans", 1, 18)); // NOI18N
        jLabel1.setText("Device Configuration");

        javax.swing.GroupLayout emvConfigPanelLayout = new javax.swing.GroupLayout(emvConfigPanel);
        emvConfigPanel.setLayout(emvConfigPanelLayout);
        emvConfigPanelLayout.setHorizontalGroup(
                emvConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(emvConfigPanelLayout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(emvConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(securityKeyLabel)
                                        .addComponent(deviceIdLabel)
                                        .addComponent(urlLabel))
                                .addGap(59, 59, 59)
                                .addGroup(emvConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(urlValue)
                                        .addComponent(deviceIdValue)
                                        .addComponent(securityKeyValue))
                                .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, emvConfigPanelLayout.createSequentialGroup()
                                .addContainerGap(125, Short.MAX_VALUE)
                                .addComponent(jLabel1)
                                .addGap(114, 114, 114))
        );
        emvConfigPanelLayout.setVerticalGroup(
                emvConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, emvConfigPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                                .addGroup(emvConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(urlValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(urlLabel))
                                .addGap(18, 18, 18)
                                .addGroup(emvConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(deviceIdValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(deviceIdLabel))
                                .addGap(15, 15, 15)
                                .addGroup(emvConfigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(securityKeyValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(securityKeyLabel))
                                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(149, 149, 149)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                        .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jcboCardReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jcboPaymentGateway, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(109, 109, 109)
                                                .addComponent(emvConfigPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(127, 127, 127))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jcboPaymentGateway, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jcboCardReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(64, 64, 64)
                                .addComponent(emvConfigPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(152, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void urlValueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_urlValueActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_urlValueActionPerformed

    private void jcboPaymentGatewayActionPerformed(java.awt.event.ActionEvent evt) {
        paymentConfiguration = paymentsName.get(comboValue(jcboPaymentGateway.getSelectedItem()));

        if (comboValue(jcboPaymentGateway.getSelectedItem()).equals("PaymentSense")) {
            urlLabel.setText("URL");
            deviceIdLabel.setText("Device ID");
            securityKeyLabel.setText("Security Key");
            emvConfigPanel.setVisible(Boolean.TRUE);
            jcboCardReader.setSelectedItem("EMV");
            jcboCardReader.setEnabled(Boolean.FALSE);

        }
        else if (comboValue(jcboPaymentGateway.getSelectedItem()).equals("Teya")) {
            emvConfigPanel.setVisible(Boolean.FALSE);
            jcboCardReader.setSelectedItem("EMV");
            jcboCardReader.setEnabled(Boolean.FALSE);
        }
        else if (comboValue(jcboPaymentGateway.getSelectedItem()).equals("MediaPayments")) {
            urlLabel.setText("Terminal ID");
            deviceIdLabel.setText("API Key");
            securityKeyLabel.setText("Security Key");
            emvConfigPanel.setVisible(Boolean.TRUE);
            jcboCardReader.setSelectedItem("EMV");
            jcboCardReader.setEnabled(Boolean.FALSE);
        }
        else {
            urlLabel.setText("URL");
            deviceIdLabel.setText("Device ID");
            securityKeyLabel.setText("Security Key");
            emvConfigPanel.setVisible(Boolean.FALSE);
            jcboCardReader.setEnabled(Boolean.TRUE);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel deviceIdLabel;
    private javax.swing.JTextField deviceIdValue;
    private javax.swing.JPanel emvConfigPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JComboBox jcboCardReader;
    private javax.swing.JComboBox jcboPaymentGateway;
    private javax.swing.JLabel securityKeyLabel;
    private javax.swing.JTextField securityKeyValue;
    private javax.swing.JLabel urlLabel;
    private javax.swing.JTextField urlValue;
    // End of variables declaration//GEN-END:variables

}
