/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.ui.xhtmlviewer;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xhtmlrenderer.extend.FSImage;
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.simple.XHTMLPanel;
import org.xhtmlrenderer.simple.extend.URLUTF8Encoder;
import org.xhtmlrenderer.simple.extend.XhtmlForm;
import org.xhtmlrenderer.swing.AWTFSImage;
import org.xhtmlrenderer.util.XRLog;

/**
 *
 * @author chm
 */
public class XemeliosHtmlForm extends XhtmlForm {
    private static final Logger logger = Logger.getLogger(XemeliosHtmlForm.class);
    private Map<String,ButtonGroup> buttonGroups;
    private XHTMLPanel pane;
    private UserAgentCallback uac;
    public XemeliosHtmlForm(UserAgentCallback uac,Element e,XHTMLPanel pane) {
        super(uac,e);
        this.pane=pane;
        this.uac=uac;
    }
    public UserAgentCallback getUac() { return this.uac; }
//    @Override
//    public JComponent addComponent(Element e) {
//        JComponent cc = (JComponent) components.get(e);
//        if (cc != null) {
//            return cc;
//        }
//        if (e.getNodeName().equals("input")) {
//            String type = e.getAttribute("type");
//            if (type == null || type.equals("")) {
//                type = "button";
//            }
//            String label = e.getAttribute("value");
//            if (label == null || label.equals("")) {
//                if (type.equals("reset")) {
//                    label = "Reset";//TODO: this text should be from properties
//                }
//                if (type.equals("submit")) {
//                    label = "Submit";
//                }
//            }
//            if (type.equals("button") || type.equals("reset") || type.equals("submit")) {
//                JButton button = new JButton();
//                button.setText(label);
//                cc = button;
//            } else if (type.equals("image")) {
//                JButton jb = new JButton();
//                Image im = null;
//                if (e.hasAttribute("src")) {
//                    System.out.println("pulling from here: " + e.getAttribute("src"));
//                    FSImage fsImage = uac.getImageResource(e.getAttribute("src")).getImage();
//                    if (fsImage != null) {
//                        im = ((AWTFSImage)fsImage).getImage();
//                    }
//                }
//                if (im == null) {
//                    jb = new JButton("Image unreachable. " + e.getAttribute("alt"));
//                } else {
//                    ImageIcon ii = new ImageIcon(im, e.getAttribute("alt"));
//                    jb = new JButton(ii);
//                }
//                jb.setBorder(BorderFactory.createEmptyBorder());
//                cc = jb;
//            } else if (type.equals("checkbox")) {
//                JCheckBox checkbox = new JCheckBox();
//                checkbox.setText("");
//                checkbox.setOpaque(false);
//                if (e.hasAttribute("checked") &&
//                        e.getAttribute("checked").equals("checked")) {
//                    checkbox.setSelected(true);
//                }
//                cc = checkbox;
//            } else if (type.equals("password")) {
//                JPasswordField pw = new JPasswordField();
//                if (e.hasAttribute("size")) {
//                    pw.setColumns(Integer.parseInt(e.getAttribute("size")));
//                } else {
//                    pw.setColumns(15);
//                }
//                if (e.hasAttribute("maxlength")) {
//                    final int maxlength = Integer.parseInt(e.getAttribute("maxlength"));
//                    pw.setDocument(new PlainDocument() {
//                        @Override
//                        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
//                            if (str == null) {
//                                return;
//                            }
//                            if ((getLength() + str.length()) <= maxlength) {
//                                super.insertString(offset, str, attr);
//                            }
//                        }
//                    });
//                }
//                cc = pw;
//            } else if (type.equals("radio")) {
//                JRadioButton radio = new JRadioButton();
//                radio.setText("");
//                radio.setOpaque(false);
//                if (e.hasAttribute("checked") &&
//                        e.getAttribute("checked").equals("checked")) {
//                    radio.setSelected(true);
//                }
//                if (buttonGroups == null) {
//                    buttonGroups = new HashMap<String,ButtonGroup>();
//                }
//                String name = e.getAttribute("name");
//                ButtonGroup group = (ButtonGroup) buttonGroups.get(name);
//                if (group == null) {
//                    group = new ButtonGroup();
//                    buttonGroups.put(name, group);
//                }
//                group.add(radio);
//                cc = radio;
//            } else if (type.equals("text")) {
//                JTextField text = new JTextField();
//                if (e.hasAttribute("value")) {
//                    text.setText(e.getAttribute("value"));
//                }
//                if (e.hasAttribute("size")) {
//                    text.setColumns(Integer.parseInt(e.getAttribute("size")));
//                } else {
//                    text.setColumns(15);
//                }
//                if (e.hasAttribute("maxlength")) {
//                    final int maxlength = Integer.parseInt(e.getAttribute("maxlength"));
//                    text.setDocument(new PlainDocument() {
//                        @Override
//                        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
//                            if (str == null) {
//                                return;
//                            }
//                            if ((getLength() + str.length()) <= maxlength) {
//                                super.insertString(offset, str, attr);
//                            }
//                        }
//                    });
//                }
//                if (e.hasAttribute("readonly") &&
//                        e.getAttribute("readonly").equals("readonly")) {
//                    text.setEditable(false);
//                }
//                cc = text;
////                } else if (type.equals("text")) {
////                    JTextField text = new JTextField();
////                    if (e.hasAttribute("value")) {
////                        text.setText(e.getAttribute("value"));
////                    }
////                    cc = null;//don't return it
////                    components.put(e, text);
//            } else if (type.equals("hidden")) {
//                // TODO: hidden form fields.
//                JTextField text = new JTextField();
//                if (e.hasAttribute("value")) {
//                    text.setText(e.getAttribute("value"));
//                }
//                cc = null;//don't return it
//                components.put(e, text);
//                text.setSize(new Dimension(0,0));
//                text.setVisible(false);
//            } else {
//                XRLog.layout("unknown input type " + type);
//            }
//            if (type.equals("submit") || type.equals("image")) {
//                ((JButton) cc).addActionListener(new ActionListener() {
//                    public void actionPerformed(ActionEvent evt) {
//                        submit((JComponent) evt.getSource());
//                    }
//                });
//
//            }
//        } else if (e.getNodeName().equals("textarea")) {
//            int rows = 4;
//            int cols = 10;
//            if (e.hasAttribute("rows")) {
//                rows = Integer.parseInt(e.getAttribute("rows"));
//            }
//            if (e.hasAttribute("cols")) {
//                cols = Integer.parseInt(e.getAttribute("cols"));
//            }
//
//            JTextArea ta = new JTextArea(rows, cols);
//            JScrollPane sp = new JScrollPane(ta);
//            sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//            sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//            if (e.getFirstChild() != null) {
//                //Uu.p("setting text to: " + elem.getFirstChild().getNodeValue());
//                ta.setText(e.getFirstChild().getNodeValue());
//            }
//            cc = sp;
//
//        } else if (e.getNodeName().equals("select")) {
//            JComboBox select = new JComboBox();
//            select.setEditable(false);//cannot edit it in HTML
//            NodeList options = e.getElementsByTagName("option");
//            int selected = -1;
//            for (int i = 0; i < options.getLength(); i++) {
//                Element value = (Element) options.item(i);
//                String svalue = value.getFirstChild().getNodeValue();
//                select.addItem(svalue);
//                if (value.hasAttribute("selected") && value.getAttribute("selected").equals("selected")) {
//                    selected = i;
//                }
//            }
//
//            if (selected != -1) {
//                select.setSelectedIndex(selected);
//            }
//            cc = select;
//        }
//        if (cc != null) {//was a form object
//            cc.setSize(cc.getPreferredSize());
//            if (e.hasAttribute("disabled") &&
//                    e.getAttribute("disabled").equals("disabled")) {
//                cc.setEnabled(false);
//            }
//            components.put(e, cc);
//        }
//        return cc;
//    }
//    @Override
//    public void submit(JComponent source) {
//        StringBuffer data = new StringBuffer();
//        Iterator fields = components.entrySet().iterator();
//        while (fields.hasNext()) {
//            Map.Entry field = (Map.Entry) fields.next();
//            Element e = (Element) field.getKey();
//            JComponent cc = (JComponent) field.getValue();
//            if (e.hasAttribute("disabled") &&
//                    e.getAttribute("disabled").equals("disabled")) {
//                continue;
//            }
//            if (e.getNodeName().equals("input")) {
//                String type = e.getAttribute("type");
//                String name = e.getAttribute("name");
//                logger.debug("<input name="+name==null?"null":name+" type="+type+"/>");
////                    if("HIDDEN".equalsIgnoreCase(type)) type="text";
//                if (type == null || type.equals("")) {
//                    type = "button";
//                }
//                if (type.equals("submit") && cc == source) {
//                    String value = e.getAttribute("value");
//                    if (value.equals("")) {
//                        value = "submit";
//                    }
//                    data.append('&');
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("name")));
//                    data.append("=");
//                    data.append(URLUTF8Encoder.encode(value));
//                } else if (type.equals("image")) {
//                    data.append('&');
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("name")));
//                    data.append("=");
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("value")));
//                } else if (type.equals("checkbox")) {
//                    JCheckBox checkbox = (JCheckBox) cc;
//                    if (!checkbox.isSelected()) {
//                        continue;
//                    }
//                    data.append('&');
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("name")));
//                    data.append("=");
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("value")));
//                } else if (type.equals("password")) {
//                    JPasswordField pw = new JPasswordField();
//                    data.append('&');
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("name")));
//                    data.append("=");
//                    data.append(URLUTF8Encoder.encode(pw.getPassword()));
//                } else if (type.equals("radio")) {
//                    JRadioButton radio = (JRadioButton) cc;
//                    if (!radio.isSelected()) {
//                        continue;
//                    }
//                    data.append('&');
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("name")));
//                    data.append("=");
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("value")));
//                } else if (type.equals("text") || type.equals("hidden")) {
//                    JTextField text = (JTextField) cc;
//                    data.append('&');
//                    data.append(URLUTF8Encoder.encode(e.getAttribute("name")));
//                    data.append("=");
//                    data.append(URLUTF8Encoder.encode(text.getText()));
//                }
//            } else if (e.getNodeName().equals("textarea")) {
//                JTextArea ta = (JTextArea) cc;
//                data.append('&');
//                data.append(URLUTF8Encoder.encode(e.getAttribute("name")));
//                data.append("=");
//                //TODO:check if we have to make linefeeds into CR-LF
//                data.append(URLUTF8Encoder.encode(ta.getText()));
//            } else if (e.getNodeName().equals("select")) {
//                JComboBox select = new JComboBox();
//                data.append('&');
//                data.append(URLUTF8Encoder.encode(e.getAttribute("name")));
//                data.append("=");
//                data.append(URLUTF8Encoder.encode(select.getSelectedItem().toString()));
//            }
//        }
//        data.deleteCharAt(0);//remove the first &
//        String action = formElement.getAttribute("action");
//        String method = formElement.getAttribute("method");
//        if (method.equals("")) {
//            method = "get";
//        }
//        //String formData = data.toString();
//        //TODO: make a real submission via uac
//        if("GET".equalsIgnoreCase(method)) {
//
//            // HERE
//            StringTokenizer st = new StringTokenizer(data.toString(),"&");
//            Hashtable<String,String> params = new Hashtable<String,String>();
//            while(st.hasMoreTokens()) {
//                String token = st.nextToken();
//                int pos = token.indexOf('=');
//                String paramName = token.substring(0,pos);
//                String value = token.substring(pos+1);
//                try { value = URLDecoder.decode(token.substring(pos+1),"UTF-8"); } catch(UnsupportedEncodingException uee) {}
//                // at this point, only one value per param is allowed
//                params.put(paramName, value);
//            }
//            String path = params.get("path");
//            if(path!=null) {
//                Set<String> keys = params.keySet();
//                Set<String> toRemove = new HashSet<String>();
//                for(String key:keys) {
//                    if(key.equals("path")) continue;
//                    if(path.indexOf("%"+key+"%")>=0) {
//                        path = path.replaceAll("%"+key+"%",params.get(key));
//                        toRemove.add(key);
//                    }
//                }
//                if(!toRemove.isEmpty()) {
//                    keys.removeAll(toRemove);
//                }
//                params.put("path",URLUTF8Encoder.encode(path));
//            }
//            path="";
//            for(String key:params.keySet()) {
//                path = path+key+"="+params.get(key)+"&";
//            }
//            path=path.substring(0,path.length()-1);
//
//            // END
//            String request = action+"?"+path;
//            String realUrl = uac.resolveURI(request);
//            logger.info("realUrl="+realUrl);
//            if(pane==null) logger.fatal("pane is NULL !");
//            pane.setDocument(realUrl);
//        }
//    }
}
