/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package kr.pe.elex.example;

import com.rometools.rome.feed.synd.*;
import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.SyndFeedOutput;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

public class FeedService {

	public static List<String> getSupportedTypes() {
		final SyndFeed feed = new SyndFeedImpl();
		return feed.getSupportedFeedTypes();
	}

	public static SyndFeed read(final Reader reader) throws FeedException {
		return new SyndFeedInput().build(reader);
	}

	public static @NotNull SyndFeed create(final List<SyndEntry> entries) {
		final SyndFeed feed = new SyndFeedImpl();
		feed.setFeedType(FeedType.RSS.getValue());
		feed.setTitle("Sample Feed (created with ROME)");
		feed.setLink("http://rome.dev.java.net");
		feed.setDescription("This feed has been created using ROME (Java syndication utilities");
		feed.setEntries(entries);

		return feed;
	}

	public static @NotNull SyndEntry createEntry() throws ParseException {
		final SyndEntry entry = new SyndEntryImpl();
		entry.setTitle("Rome v1.0");
		entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
		entry.setPublishedDate(SimpleDateFormat.getDateInstance().parse("2004-06-08"));
		final SyndContent description = new SyndContentImpl();
		description.setType("text/plain");
		description.setValue("Initial release of Rome");
		entry.setDescription(description);
		return entry;
	}

	public static String write(final SyndFeed feed) throws FeedException, IOException {
		try (final StringWriter writer = new StringWriter()) {
			new SyndFeedOutput().output(feed, writer);

			return writer.toString();
		}


	}


}
