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.dayRange
import app.furmeet.events.brasilfurfest.helper.observables.Observer
import app.furmeet.events.brasilfurfest.schedule.abstractDataTypes.GregorianCalendarRange
import app.furmeet.events.brasilfurfest.schedule.adapters.DayHoursAdapter
import app.furmeet.events.brasilfurfest.schedule.adapters.EmptyRecyclerViewAdapter
import app.furmeet.events.brasilfurfest.schedule.managers.ScheduleManagerGetter
import com.brandongogetap.stickyheaders.StickyLayoutManager
import kotlinx.android.synthetic.main.fragment_schedule_day.view.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

class ConventionDayFragment : Fragment() {
    companion object {
        fun newInstance(day: GregorianCalendarRange): Fragment {
            return ConventionDayFragment().apply {
                arguments = Bundle().apply {
                    putSerializable("dayRange", day)
                }
            }
        }
    }

    private val safeActivity = ActivitiesForFragments["ScheduleActivity"]!!
    private val scheduleManager = ScheduleManagerGetter[safeActivity]
    lateinit var day: GregorianCalendarRange
    lateinit var dayParts: List<GregorianCalendarRange>

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(R.layout.fragment_schedule_day, container, false)
        rootView.stickyDays.adapter = EmptyRecyclerViewAdapter()
        rootView.stickyDays.layoutManager = LinearLayoutManager(rootView.context)
        doAsync {
            arguments?.apply {
                day = getSerializable("dayRange")!! as GregorianCalendarRange
            }
            dayParts = day.split(
                    scheduleManager.convention.splitDayIn
            ).filter {
                val today = scheduleManager.convention.days.first { conDay ->
                    it in conDay.dayRange
                }.dayRange
                if (scheduleManager.convention.paddingStartOfDayMillis <= scheduleManager.convention.paddingEndOfDayMillis) {
                    val today1 = GregorianCalendarRange(
                            today.start.timeInMillis + scheduleManager.convention.paddingStartOfDayMillis,
                            today.start.timeInMillis + scheduleManager.convention.paddingEndOfDayMillis
                    )
                    it in today1
                } else {
                    val today1 = GregorianCalendarRange(
                            today.start.timeInMillis,
                            today.start.timeInMillis + scheduleManager.convention.paddingEndOfDayMillis
                    )
                    val today2 = GregorianCalendarRange(
                            today.start.timeInMillis + scheduleManager.convention.paddingStartOfDayMillis,
                            today.finish.timeInMillis
                    )
                    it in today1 || it in today2
                }
            }.filter {
                it.start >= scheduleManager.convention.startCalendarOn
            }.filter {
                it.start < scheduleManager.convention.endCalendarOn
            }
            val adapter = DayHoursAdapter(
                    safeActivity,
                    dayParts,
                    scheduleManager
            )
            val layoutManager = StickyLayoutManager(context!!, adapter)
            layoutManager.elevateHeaders(3)
            uiThread {
                rootView.stickyDays.layoutManager = layoutManager
                rootView.stickyDays.adapter = adapter
            }
            scheduleManager.addObserver(Observer { _, _: Any? ->
                doAsync {
                    adapter.onDataChanged()
                }
            })
        }
        return rootView
    }
}