package kr.lunaticbum.back.lun.utils

import java.io.IOException
import java.io.InputStream
import java.net.MalformedURLException
import java.net.URL
import javax.xml.stream.XMLEventReader
import javax.xml.stream.XMLInputFactory
import javax.xml.stream.XMLStreamException
import javax.xml.stream.events.Characters
import javax.xml.stream.events.XMLEvent


class FeedMessage {
    var title: String? = null
    var description: String? = null
    var link: String? = null
    var author: String? = null
    var guid: String? = null

    override fun toString(): String {
        return ("FeedMessage [title=" + title + ", description=" + description
                + ", link=" + link + ", author=" + author + ", guid=" + guid
                + "]")
    }
}
class Feed(
    val title: String, val link: String, val description: String, val language: String,
    val copyright: String, val pubDate: String
) {
    val messages: ArrayList<FeedMessage> = ArrayList()

    override fun toString(): String {
        return ("Feed [copyright=" + copyright + ", description=" + description
                + ", language=" + language + ", link=" + link + ", pubDate="
                + pubDate + ", title=" + title + "]")
    }
}
class RssFeedsParser {

    val TITLE: String = "title"
    val DESCRIPTION: String = "description"
    val CHANNEL: String = "channel"
    val LANGUAGE: String = "language"
    val COPYRIGHT: String = "copyright"
    val LINK: String = "link"
    val AUTHOR: String = "author"
    val ITEM: String = "item"
    val PUB_DATE: String = "pubDate"
    val GUID: String = "guid"

    var url: URL? = null

//    fun parser(feedUrl: String?) {
//        try {
//            this.url = URL(feedUrl)
//        } catch (e: MalformedURLException) {
//            throw RuntimeException(e)
//        }
//    }

    fun readFeed(feedUrl: String?): Feed? {
        try {
            this.url = URL(feedUrl)
        } catch (e: MalformedURLException) {
            throw RuntimeException(e)
        }
        var feed: Feed? = null
        try {
            var isFeedHeader = true
            // Set header values intial to the empty string
            var description = ""
            var title = ""
            var link = ""
            var language = ""
            var copyright = ""
            var author = ""
            var pubdate = ""
            var guid = ""

            // First create a new XMLInputFactory
            val inputFactory = XMLInputFactory.newInstance()
            // create a new eventReader
            val `in` = read()
            val eventReader = inputFactory.createXMLEventReader(`in`)
            // read the XML document
            while (eventReader.hasNext()) {
                var event = eventReader.nextEvent()
                if (event.isStartElement) {
                    val localPart = event.asStartElement().name
                        .localPart
                    when (localPart) {
                        ITEM -> {
                            if (isFeedHeader) {
                                isFeedHeader = false
                                feed = Feed(
                                    title!!, link!!, description!!, language!!,
                                    copyright!!, pubdate!!
                                )
                            }
                            event = eventReader.nextEvent()
                        }

                        TITLE -> title = getCharacterData(event, eventReader)
                        DESCRIPTION -> description = getCharacterData(event, eventReader)
                        LINK -> link = getCharacterData(event, eventReader)
                        GUID -> guid = getCharacterData(event, eventReader)
                        LANGUAGE -> language = getCharacterData(event, eventReader)
                        AUTHOR -> author = getCharacterData(event, eventReader)
                        PUB_DATE -> pubdate = getCharacterData(event, eventReader)
                        COPYRIGHT -> copyright = getCharacterData(event, eventReader)
                    }
                } else if (event.isEndElement) {
                    if (event.asEndElement().name.localPart === (ITEM)) {
                        val message = FeedMessage()
                        message.author = author
                        message.description = description
                        message.guid = guid
                        message.link = link
                        message.title = title
                        feed!!.messages.add(message)
                        event = eventReader.nextEvent()
                        continue
                    }
                }
            }
        } catch (e: XMLStreamException) {
            throw RuntimeException(e)
        }
        return feed
    }

    @Throws(XMLStreamException::class)
    private fun getCharacterData(event: XMLEvent, eventReader: XMLEventReader): String {
        var event = event
        var result = ""
        event = eventReader.nextEvent()
        if (event is Characters) {
            result = event.asCharacters().data
        }
        return result
    }

    private fun read(): InputStream {
        try {
            return url!!.openStream()
        } catch (e: IOException) {
            throw RuntimeException(e)
        }
    }
}