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 androidx.recyclerview.widget.LinearLayoutManager
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.schedule.adapters.ConventionSocialMediasAdapter
import app.furmeet.events.brasilfurfest.schedule.managers.ScheduleManagerGetter
import kotlinx.android.synthetic.main.fragment_socialmedia_list.view.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

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

    lateinit var rootView: View

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

    val safeActivity = ActivitiesForFragments["ScheduleActivity"]!!
    val scheduleManager = ScheduleManagerGetter[safeActivity]
    val convention get() = scheduleManager.convention

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

    private fun configureRootView() {
        doAsync {
            uiThread {
                rootView.apply {
                    socialMedias.adapter = ConventionSocialMediasAdapter(convention)
                    socialMedias.layoutManager = LinearLayoutManager(context)
                }
            }
        }
    }
}