package de.kif.backend

import twitter4j.*
import twitter4j.conf.ConfigurationBuilder


/*
fun twitter() {
    val msgQueue = LinkedBlockingQueue<String>(100000)
    val eventQueue = LinkedBlockingQueue<Event>(1000)

    val hosts = HttpHosts(Constants.STREAM_HOST)

    val filterEndpoint = StatusesFilterEndpoint()
    filterEndpoint.trackTerms(listOf("kif"))

    println(Configuration.Twitter.username)
    println(Configuration.Twitter.password)

    val authentication = BasicAuth(
        Configuration.Twitter.username,
        Configuration.Twitter.password
    )

    val builder = ClientBuilder()
        .name("kif-portal")
        .hosts(hosts)
        .authentication(authentication)
        .endpoint(filterEndpoint)
        .processor(StringDelimitedProcessor(msgQueue))
        .eventMessageQueue(eventQueue)

    val client = builder.build()

    val listener = object: StatusStreamHandler {
        override fun onUnknownMessageType(msg: String?) {
            println("onUnknownMessageType")
            println(msg)
        }

        override fun onDisconnectMessage(message: DisconnectMessage?) {
            println("onDisconnectMessage")
            println(message?.disconnectReason)
        }

        override fun onStallWarningMessage(warning: StallWarningMessage?) {
            println("onStallWarningMessage")
            println(warning?.message)
        }

        override fun onTrackLimitationNotice(numberOfLimitedStatuses: Int) {
            println("onTrackLimitationNotice")
            println(numberOfLimitedStatuses)
        }

        override fun onStallWarning(warning: StallWarning?) {
            println("onStallWarning")
            println(warning?.message)
        }

        override fun onException(ex: Exception?) {
            println("onException")
            ex?.printStackTrace()
        }

        override fun onDeletionNotice(statusDeletionNotice: StatusDeletionNotice?) {
            println("onDeletionNotice")
            println(statusDeletionNotice?.statusId)
        }

        override fun onStatus(status: Status?) {
            println("onStatus")
            println(status?.text)
        }

        override fun onScrubGeo(userId: Long, upToStatusId: Long) {
            println("onScrubGeo")
        }

    }

    val t4jClient = Twitter4jStatusClient(
        client,
        msgQueue,
        listOf(listener),
        Executors.newFixedThreadPool(4)
    )

    t4jClient.connect()

    t4jClient.process()

    while (true) {

    }
}
 */

fun twitter() {
    val cb = ConfigurationBuilder()
    cb.setDebugEnabled(true)
        .setUser(Configuration.Twitter.username)
        .setPassword(Configuration.Twitter.password)

    val listener = object : StatusListener {

        override fun onTrackLimitationNotice(numberOfLimitedStatuses: Int) {
            println("onTrackLimitationNotice")
            println(numberOfLimitedStatuses)
        }

        override fun onStallWarning(warning: StallWarning?) {
            println("onStallWarning")
            println(warning?.message)
        }

        override fun onException(ex: Exception?) {
            println("onException")
            ex?.printStackTrace()
        }

        override fun onDeletionNotice(statusDeletionNotice: StatusDeletionNotice?) {
            println("onDeletionNotice")
            println(statusDeletionNotice?.statusId)
        }

        override fun onStatus(status: Status?) {
            println("onStatus")
            println(status?.text)
        }

        override fun onScrubGeo(userId: Long, upToStatusId: Long) {
            println("onScrubGeo")
        }
    }

    val twitterStream = TwitterStreamFactory(cb.build()).instance

    addTwitterStreamListener(twitterStream, listener)

    twitterStream.sample()
}