package app.furmeet.events.system.schedule.adapters

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import app.furmeet.events.bff.R
import app.furmeet.events.system.helper.dayRange
import app.furmeet.events.system.helper.formatAsDayOfWeekNumberNoParenthesis
import app.furmeet.events.system.helper.getString
import app.furmeet.events.system.helper.solved
import app.furmeet.events.system.schedule.fragments.*
import app.furmeet.events.system.schedule.managers.ScheduleManager

class DaysPagerAdapter(fm: FragmentManager, private val scheduleManager: ScheduleManager) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
    val displacement get() = if (scheduleManager.conventionSeries.urgentMessage == null) 0 else 1

    override fun getItem(position: Int): Fragment = when (position) {
        0 -> ConventionSummaryFragment.newInstance()
        displacement -> ConventionUrgentMessageFragment.newInstance()
        displacement + scheduleManager.convention.days.size + 1 -> ConventionAnnouncementsFragment.newInstance()
        displacement + scheduleManager.convention.days.size + 2 -> ConventionRulesFragment.newInstance()
        displacement + scheduleManager.convention.days.size + 3 -> ConventionSocialMediasFragment.newInstance()
        else -> ConventionDayFragment.newInstance(scheduleManager.convention.days[position - 1 - displacement].dayRange)
    }

    override fun getPageTitle(position: Int): String = when (position) {
        0 -> R.string.schecule_tab_summary.getString()
        displacement -> scheduleManager.conventionSeries.urgentMessage!!.tab.name.solved!!
        displacement + scheduleManager.convention.days.size + 1 -> R.string.past_notifications_tab.getString()
        displacement + scheduleManager.convention.days.size + 2 -> R.string.rules.getString()
        displacement + scheduleManager.convention.days.size + 3 -> R.string.social_media.getString()
        else -> scheduleManager.convention.days[position - 1 - displacement].timeInMillis.formatAsDayOfWeekNumberNoParenthesis()
    }

    override fun getCount(): Int = scheduleManager.convention.days.size + 4 + displacement
}