package app.furmeet.events.brasilfurfest.schedule.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.adlerosn.brasilfurfest.R
import app.furmeet.events.brasilfurfest.helper.ActivitiesForFragments
import app.furmeet.events.brasilfurfest.helper.inflate
import app.furmeet.events.brasilfurfest.helper.observables.Observable
import app.furmeet.events.brasilfurfest.helper.observables.Observer
import app.furmeet.events.brasilfurfest.helper.solved
import app.furmeet.events.brasilfurfest.helper.toHtml
import app.furmeet.events.brasilfurfest.schedule.managers.ScheduleManagerGetter
import kotlinx.android.synthetic.main.fragment_urgentmessage.view.*
import org.jetbrains.anko.doAsync

class ConventionUrgentMessageFragment : Fragment(), Observer<Any?> {
    companion object {
        fun newInstance(): Fragment {
            return ConventionUrgentMessageFragment().apply {
                arguments = Bundle()
            }
        }
    }

    private val safeActivity = ActivitiesForFragments["ScheduleActivity"]!!
    private val scheduleManager = ScheduleManagerGetter[safeActivity]
    private val urgentMessage = scheduleManager.conventionSeries.urgentMessage?.tab
    lateinit var rootView: View

    override fun update(observable: Observable<Any?>, args: Any?) =
        safeActivity.runOnUiThread { configureRootView() }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        rootView = inflater.inflate(R.layout.fragment_urgentmessage)
        rootView.visibility = View.VISIBLE
        doAsync { configureRootView() }
        scheduleManager.addObserver(this)
        return rootView
    }

    private fun configureRootView() {
        urgentMessage!!
        safeActivity.runOnUiThread {
            rootView.description.text = urgentMessage.description.solved!!
            rootView.text.text = urgentMessage.text.solved!!.toHtml()
        }
    }
}