/*
 * GPSiesSource.java
 * 
 * Copyright (c) 2010 Rob Sim <robsim91 at users.sourceforge.net>.
 * 
 * This file is part of jmap.
 * 
 * jmap 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.
 * 
 * jmap 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 jmap.  If not, see <http ://www.gnu.org/licenses/>.
 */

package jmap.DataSources.Misc;

import com.sun.cnpi.rss.elements.Item;
import com.sun.cnpi.rss.elements.Rss;
import com.sun.cnpi.rss.parser.RssParser;
import com.sun.cnpi.rss.parser.RssParserException;
import com.sun.cnpi.rss.parser.RssParserFactory;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import jmap.Base.DataSources.Plugin;
import jmap.Base.MapManager;
import jmap.Base.PointD;
import jmap.Base.ZoomLevel;
import jmap.DataSources.GeoData.Route;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class GPSiesSource extends Plugin{
    Collection<RouteProperties> routeProperties = new ArrayList<>();
    public GPSiesSource() {
        super("GPSiesSource");
        try {
            RssParser parser = RssParserFactory.createDefault();
            Rss rss = parser.parse(new URL("http://www.gpsies.com/gpsiesFeed.do?username=mbrmagazine"));
            for(Object o : rss.getChannel().getItems()){
                if(o.getClass().equals(Item.class)){
                    Item i = (Item) o;
                    routeProperties.add(new RouteProperties(i.getTitle().getText(), i.getDescription().getText()));
                }
            }
        } catch (IOException | RssParserException ex) {
            Logger.getLogger(GPSiesSource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @Override
    public JPanel getSidebarDisplay() {
        final JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        final JLabel jLabel = new JLabel();
        jLabel.setBackground(Color.white);
        mainPanel.add(jLabel);
        mainPanel.add(new JSeparator());

        for(final RouteProperties rp : routeProperties){
            final JButton route = new JButton(rp.getName());
                route.setBorder(null);
                route.setContentAreaFilled(false);
                route.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getButton() == MouseEvent.BUTTON1){
                            try {
                                jLabel.setText("<html>\n" + rp.getDescription() + "\n</html>");
                                if(rp.route == null){
                                    rp.downloadRoute();
                                }
                                renderer.setMapPos(rp.getRoute().getWaypoint(0).getPosition());
                            } catch (IOException | SAXException ex) {
                                Logger.getLogger(GPSiesSource.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        super.mouseEntered(e);
                        ((JButton)e.getSource()).setContentAreaFilled(true);
                    }
                    @Override
                    public void mouseExited(MouseEvent e) {
                        super.mouseExited(e);
                        ((JButton)e.getSource()).setContentAreaFilled(false);
                    }

                });
                mainPanel.add(route);
        }

        return mainPanel;
    }
    @Override
    public void draw(Graphics g, PointD topLeft, int bottem, double xRatio, double yRatio, ZoomLevel zl) {
        for(RouteProperties r : routeProperties){
            if(r.getRoute() != null){
                r.route.draw(g, topLeft, bottem, xRatio, yRatio, zl);
            }
        }
    }
    @Override
    public String getName() {
        return "GPSies";
    }
    @Override
    public void configure(Element el, MapManager mapManager){
    }

    @Override
    protected String dataToXml(){
        return "";
    }
    
    protected class RouteProperties{
        protected String name, Description;
        public RouteProperties(String name, String Description) {
            this.name = name;
            this.Description = Description;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getDescription() {
            return Description;
        }
        public void setDescription(String Description) {
            this.Description = Description;
        }

        protected Route route;
        public Route getRoute() {
            return route;
        }
        public void downloadRoute() throws IOException, SAXException, ArrayIndexOutOfBoundsException {
            for(String s : getDescription().split("<a")){
                if(s.contains("GPX")){
                    route = (Route) new jmap.DataSources.GeoData.GPXDocument(s.substring(s.indexOf("\"") + 1, s.lastIndexOf("\""))).getTracks().toArray()[0];
                }
                System.out.println(s);
            }
        }
    }
}
