package app.furmeet.events.system.schedule.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import app.furmeet.events.system.helper.*
import app.furmeet.events.bff.R
import app.furmeet.events.system.helper.observables.Observable
import app.furmeet.events.system.helper.observables.Observer
import app.furmeet.events.system.schedule.managers.ScheduleManagerGetter
import app.furmeet.events.bff.databinding.FragmentUrgentmessageBinding

//import kotlinx.android.synthetic.main.fragment_urgentmessage.view.*
//import org.jetbrains.anko.doAsync

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

    private val scheduleManager = ScheduleManagerGetter[safeActivity]
    private val urgentMessage = scheduleManager.conventionSeries.urgentMessage?.tab
    lateinit var rootView: View
    lateinit var binding: FragmentUrgentmessageBinding

    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)
        binding = FragmentUrgentmessageBinding.bind(rootView)
        rootView.visibility = View.VISIBLE
        doAsync { configureRootView() }
        scheduleManager.addObserver(this)
        return rootView
    }

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