package fr.forum_thalie.tsumugi.ui.news

import android.content.Context
import android.os.Build
import android.util.Log
import android.view.View
import android.webkit.WebView
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.RecyclerView
import com.prof.rssparser.Parser
import fr.forum_thalie.tsumugi.Async
import fr.forum_thalie.tsumugi.R
import fr.forum_thalie.tsumugi.tag
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONObject
import java.net.URL
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.min


class NewsViewModel : ViewModel() {

    var screenRatio: Int = 100
    lateinit var root: View
    var webView: WebView? = null
    var webViewNews: WebViewNews? = null

    val newsArray : ArrayList<News> = ArrayList()
    var isWebViewLoaded = false

    private val viewModelJob = Job()
    private val coroutineScope = CoroutineScope(Dispatchers.Main + viewModelJob)

    fun fetch(root: androidx.swiperefreshlayout.widget.SwipeRefreshLayout? = null, viewAdapter: RecyclerView.Adapter<*>? = null, c: Context)
    {
        val urlToScrape = c.getString(R.string.rss_url)
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
            return // the RSS Parser does not support API20- because of no TLS v1.2

        val maxNumberOfArticles = 5
        coroutineScope.launch(Dispatchers.Main) {
            Log.d(tag, "launching coroutine")
                val parser = Parser()
            try {
                val articleList = parser.getArticles(urlToScrape)
                newsArray.clear()
                for (i in 0 until min(articleList.size, maxNumberOfArticles)) {
                    val item = articleList[i]
                    Log.d(tag, "i = $i / ${articleList.size}")
                    val news = News()
                    news.title = item.title ?: ""
                    news.link = item.link ?: urlToScrape
                    news.author = item.author ?: ""
                    news.text = item.content ?: ""
                    news.header = item.description ?: ""

                    val formatter6 = SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH)
                    val dateString = item.pubDate.toString()
                    Log.d(tag, "$news --- ${dateString}")

                    news.date = formatter6.parse(dateString) ?: Date(0)

                    newsArray.add(news)
                }
                // The list contains all article's data. For example you can use it for your adapter.
                root?.isRefreshing = false
                viewAdapter?.notifyDataSetChanged()
            }catch (e: Exception)
            {
                e.printStackTrace()
            }
        }
    }
}