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 androidx.recyclerview.widget.LinearLayoutManager
import app.furmeet.events.bff.R
import app.furmeet.events.system.helper.ActivitiesForFragments
import app.furmeet.events.system.helper.doAsync
import app.furmeet.events.system.helper.inflate
import app.furmeet.events.system.helper.observables.Observable
import app.furmeet.events.system.helper.observables.Observer
import app.furmeet.events.system.helper.uiThread
import app.furmeet.events.system.schedule.adapters.ConventionSocialMediasAdapter
import app.furmeet.events.system.schedule.managers.ScheduleManagerGetter
import app.furmeet.events.bff.databinding.FragmentSocialmediaListBinding

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

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

    lateinit var binding: FragmentSocialmediaListBinding
    lateinit var rootView: View

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

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

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