//    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.AppLocal;
import com.unicenta.pos.util.StringParser;
import java.awt.Component;

/**
 *
 * @author adrian
 */
public class ParametersPrinter extends javax.swing.JPanel implements ParametersConfig {

    private String othersizename = "standard";

    /** Creates new form ParametersPrinter
     * @param printernames */
    public ParametersPrinter(String [] printernames) {
        initComponents();
        
        jPrinters.addItem("(Default)");
        jPrinters.addItem("(Show dialog)");
        for (String name : printernames) {
            jPrinters.addItem(name);
        }
    }

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

    /**
     *
     * @param dirty
     */
    public void addDirtyManager(DirtyManager dirty) {
        jPrinters.addActionListener(dirty);
        jReceiptPrinter.addActionListener(dirty);
    }

    /**
     *
     * @param p
     */
    public void setParameters(StringParser p) {
        jPrinters.setSelectedItem(p.nextToken(','));
        String sizename = p.nextToken(',');
        jReceiptPrinter.setSelected("receipt".equals(sizename));
        othersizename = "receipt".equals(sizename) ? "standard" : sizename;
    }

    /**
     *
     * @return
     */
    public String getParameters() {
        return comboValue(jPrinters.getSelectedItem()) + "," + boolValue(jReceiptPrinter.isSelected());
    }

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

    private String boolValue(boolean value) {
        return value ? "receipt" : othersizename;
    }

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

        jPrinters = new javax.swing.JComboBox();
        jReceiptPrinter = new javax.swing.JCheckBox();

        setOpaque(false);

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

        jReceiptPrinter.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jReceiptPrinter.setSelected(true);
        jReceiptPrinter.setText(AppLocal.getIntString("label.receiptprinter")); // NOI18N
        jReceiptPrinter.setOpaque(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPrinters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jReceiptPrinter)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPrinters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jReceiptPrinter, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jPrintersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPrintersActionPerformed

    }//GEN-LAST:event_jPrintersActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jPrinters;
    private javax.swing.JCheckBox jReceiptPrinter;
    // End of variables declaration//GEN-END:variables

}
