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.observables.Observable
import app.furmeet.events.system.helper.observables.Observer
import app.furmeet.events.system.helper.uiThread
import app.furmeet.events.system.schedule.adapters.ConventionRulesAdapter
import app.furmeet.events.system.schedule.managers.ScheduleManagerGetter
import app.furmeet.events.bff.databinding.FragmentRulesListBinding

class ConventionRulesFragment : FragmentWithSafeActivity(), Observer<Any?> {
    companion object {

        fun newInstance(): Fragment {
            return ConventionRulesFragment().apply {
                arguments = Bundle()
            }
        }
    }

    val scheduleManager = ScheduleManagerGetter[safeActivity]
    val convention get() = scheduleManager.convention

    lateinit var binding: FragmentRulesListBinding
    lateinit var rootView: View

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

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

    private fun configureRootView() {
        doAsync {
            uiThread {
                binding.apply {
                    rulesRecyclerView.adapter = ConventionRulesAdapter(convention, safeActivity)
                    rulesRecyclerView.layoutManager = LinearLayoutManager(context)
                }
            }
        }
    }
}
