package eu.toldi.rss;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@XStreamAlias("article")
public class Article {
    private String title;
    @XStreamAlias("link")
    private String URL;
    @XStreamAlias("description")
    private String Description;
    private String imageURL;
    private String author;
    private LocalDateTime pubDate;
    @Override
    public String toString() {
        return "Article{" +
                "title='" + title + '\'' +
                ", URL='" + URL + '\'' +
                ", Description='" + Description + '\'' +
                ", imageURL='" + imageURL + '\'' +
                ", author='" + author + '\'' +
                ", pubDate=" + pubDate+
                '}';
    }

    public Article(){}
    public Article(Node article,DateTimeFormatter formatter){
        NodeList properties = article.getChildNodes();
        for (int j = 0; j < properties.getLength(); j++) {
            Node node = properties.item(j);
            String tagName = node.getNodeName();
            switch (tagName) {
                case "title":
                    this.setTitle(node.getTextContent());
                    break;
                case "author":
                case "dc:creator":
                    this.setAuthor(node.getTextContent());
                    break;
                case "pubDate":
                    this.setPubDate(LocalDateTime.parse(node.getTextContent(), formatter));
                    break;
                case "link":
                    this.setURL(node.getTextContent());
                    break;
                case "description":
                    this.setDescription(node.getTextContent());
                    break;
                case "enclosure":
                    Element e = (Element) node;
                    if (e.getAttribute("type").equals("image/png") || e.getAttribute("type").equals("image/jpeg"))
                        this.setImageURL(e.getAttribute("url"));
                default:
                    break;
            }
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getURL() {
        return URL;
    }

    public void setURL(String URL) {
        this.URL = URL;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public LocalDateTime getPubDate() {
        return pubDate;
    }

    public void setPubDate(LocalDateTime pubDate) {
        this.pubDate = pubDate;
    }


    public void open(){
        Desktop desktop;
        if(Desktop.isDesktopSupported() && (desktop = Desktop.getDesktop()).isSupported(Desktop.Action.BROWSE)){
            try {
                desktop.browse(new URI(URL));
            } catch (IOException | URISyntaxException e) {
                e.printStackTrace();
            }
        }else{
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("xdg-open " + URL);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
