package eu.toldi.rss;

import javax.swing.table.AbstractTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


public class FeedData extends AbstractTableModel {

    private List<Feed> feeds = new ArrayList<Feed>();
    private List<Article> articleList = new ArrayList<Article>();

    private Feed limit = null;

    public void addFeed(Feed f){
        feeds.add(f);
        for (int i = 0; i < f.getArticleCount(); i++) {
            articleList.add(f.get(i));
        }
    }

    @Override
    public int getRowCount() {
        return (limit == null) ? articleList.size() : limit.getArticleCount();
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public String getColumnName(int oszlop) {
        switch (oszlop) {
            case 0:
                return "Cím";
            case 1:
                return "Szerző";
            case 2:
                return "Dátum";
            default:
                return "Leírás";
        }
    }

    @Override
    public Class<?> getColumnClass(int oszlop) {
        switch (oszlop) {
            case 2:
                return LocalDateTime.class;
            case 0:
            case 1:
            default:
                return String.class;
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

            switch (columnIndex) {
                case 0:
                    return get(rowIndex).getTitle();
                case 1:
                    return get(rowIndex).getAuthor();
                case 2:
                    return get(rowIndex).getPubDate();
                default:
                    return get(rowIndex).getDescription();
            }

    }

     public Article get(int i){
        return (limit == null) ? articleList.get(i) : limit.get(i);
     }

    public void clicked(int row){
        get(row).open();
    }

    public void limitTo(Feed feed){
        limit = feed;
    }

    public DefaultMutableTreeNode getRootNode(){
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Feeds");
        for (Feed f:feeds) {
            root.add(f.getTreeNode());
        }
        return root;
    }

    public List<Feed> getFeeds() {
        return feeds;
    }

    public void removeFeed(Feed feed){
        boolean found = false;
        for (int i = 0; i < feeds.size(); i++) {
            //Csak ha PONTOSAN megegyezik
            if(feeds.get(i) == feed){
                feeds.remove(i);
                found =true;
                break;
            }
        }
        if(found){
            List<Article> similar = new ArrayList<Article>( articleList );
            List<Article> different = new ArrayList<Article>();
            different.addAll( articleList );
            different.addAll( feed.getArticleList() );

            similar.retainAll( feed.getArticleList() );
            different.removeAll( similar );
            articleList = different;
        }
    }

    public void setLocation(Feed f,int index){
        feeds.remove(f);
        feeds.add(index,f);
    }

    @Override
    public String toString() {
        return "FeedData{" +
                "feeds=" + feeds +
                '}';
    }
}
