package net.pokeranalytics.android.ui.view.rowrepresentable

import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.Criteria
import net.pokeranalytics.android.model.filter.QueryCondition
import net.pokeranalytics.android.model.filter.mapFirstCondition
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType

enum class FilterSectionRow(override val resId: Int?) : RowRepresentable {
	CASH_TOURNAMENT(R.string.cash_or_tournament),
	LIVE_ONLINE(R.string.live_or_online),
	GAME(R.string.games),
	LIMIT_TYPE(R.string.limits),
	TABLE_SIZE(R.string.table_sizes),
	DYNAMIC_DATE(R.string.dynamic_date),
	FIXED_DATE(R.string.fixed_date),
	DURATION(R.string.duration),
	YEAR(R.string.year),
	WEEKDAYS_OR_WEEKEND(R.string.weekdays_or_weekend),
	DAY_OF_WEEK(R.string.day_of_the_week),
	MONTH_OF_YEAR(R.string.month_of_the_year),
	SESSION_DURATION(R.string.session_duration),
	TIME_FRAME_RANGE(R.string.hour_slot),
	SESSIONS(R.string.sessions),
	BLIND(R.string.blinds),
	CASH_RE_BUY_COUNT(R.string.rebuy_count),
	TOURNAMENT_TYPE(R.string.tournament_types),
	TOURNAMENT_NAME(R.string.tournament_name),
	TOURNAMENT_FEATURE(R.string.tournament_feature),
	COMPLETION_PERCENTAGE(R.string.tournament_completion_percentage_interval),
	TOURNAMENT_FINAL_POSITION(R.string.final_position),
	TOURNAMENT_NUMBER_OF_PLAYER(R.string.players_count),
	TOURNAMENT_RE_BUY_COUNT(R.string.rebuy_count),
	TOURNAMENT_ENTRY_FEE(R.string.buyin),
	MULTI_TABLING(R.string.number_of_tables),
	VALUE(R.string.value),
	LOCATION(R.string.locations),
	BANKROLL(R.string.bankrolls),
	NUMBER_OF_PLAYERS(R.string.number_of_players),
	MULTI_PLAYER(R.string.multiplayer),
    NUMBER_OF_REBUY(R.string.number_of_buyins)
	;

	private enum class SelectionType {
		SINGLE,
		MULTIPLE,
	}

	override val viewType: Int = RowViewType.HEADER_TITLE.ordinal

	val allowMultiSelection: Boolean
		get() = (this.selectionType == SelectionType.MULTIPLE)

	val filterElements: List<RowRepresentable>
		get() {

			val data = arrayListOf<RowRepresentable>().apply {
				this.addAll(
					when (this@FilterSectionRow) {

						// General
						CASH_TOURNAMENT -> Criteria.SessionTypes.queryConditions.mapFirstCondition()
						LIVE_ONLINE -> Criteria.BankrollTypes.queryConditions.mapFirstCondition()
						GAME -> Criteria.Games.queryConditions.mapFirstCondition()
						LIMIT_TYPE -> Criteria.Limits.queryConditions.mapFirstCondition()
                        TABLE_SIZE -> Criteria.TableSizes.queryConditions.mapFirstCondition()
						// Date
						DYNAMIC_DATE -> arrayListOf(
							QueryCondition.IsToday,
							QueryCondition.WasYesterday,
							QueryCondition.WasTodayAndYesterday,
							QueryCondition.DuringThisWeek,
							QueryCondition.DuringThisMonth,
							QueryCondition.DuringThisYear
						)
						FIXED_DATE -> arrayListOf(QueryCondition.StartedFromDate(), QueryCondition.EndedToDate())
						DURATION -> arrayListOf(QueryCondition.PastDay())
                        WEEKDAYS_OR_WEEKEND -> arrayListOf(QueryCondition.IsWeekDay, QueryCondition.IsWeekEnd)
                        YEAR -> Criteria.Years.queryConditions.mapFirstCondition()
                        DAY_OF_WEEK -> Criteria.DaysOfWeek.queryConditions.mapFirstCondition()
                        MONTH_OF_YEAR -> Criteria.MonthsOfYear.queryConditions.mapFirstCondition()

						// Duration
						SESSION_DURATION -> QueryCondition.moreOrLess<QueryCondition.Duration>()
						TIME_FRAME_RANGE -> arrayListOf(QueryCondition.StartedFromTime(), QueryCondition.EndedToTime())

						// Sessions
						SESSIONS -> arrayListOf(QueryCondition.LastGame(), QueryCondition.LastSession())

						// Cash
                        BLIND -> Criteria.Blinds.queryConditions.mapFirstCondition()
//						CASH_RE_BUY_COUNT -> QueryCondition.moreOrLess<QueryCondition.Rebuy>()

						// Tournament
						TOURNAMENT_TYPE -> Criteria.TournamentTypes.queryConditions.mapFirstCondition()
//						COMPLETION_PERCENTAGE ->  arrayListOf()

						TOURNAMENT_FINAL_POSITION -> QueryCondition.moreOrLess<QueryCondition.TournamentFinalPosition>()
						TOURNAMENT_NUMBER_OF_PLAYER -> QueryCondition.moreOrLess<QueryCondition.TournamentNumberOfPlayer>()
						TOURNAMENT_ENTRY_FEE -> Criteria.TournamentFees.queryConditions.mapFirstCondition()
						TOURNAMENT_NAME -> Criteria.TournamentNames.queryConditions.mapFirstCondition()
						TOURNAMENT_FEATURE -> Criteria.TournamentFeatures.queryConditions.mapFirstCondition()
						LOCATION -> Criteria.Locations.queryConditions.mapFirstCondition()
						BANKROLL -> Criteria.Bankrolls.queryConditions.mapFirstCondition()
						MULTI_TABLING -> QueryCondition.moreOrLess<QueryCondition.NumberOfTable>()
						//NUMBER_OF_PLAYERS -> QueryCondition.moreOrLess<QueryCondition.TournamentNumberOfPlayer>()
                        NUMBER_OF_REBUY -> QueryCondition.moreOrLess<QueryCondition.NumberOfRebuy>()
//						MULTI_PLAYER -> arrayListOf()

						VALUE -> arrayListOf<QueryCondition>().apply {
                            addAll(QueryCondition.moreOrLess<QueryCondition.NetAmountWon>())
                            addAll(QueryCondition.moreOrLess<QueryCondition.NetAmountLost>())
                        }
                        else -> arrayListOf()
					}.apply {
						this.forEach {
							it.filterSectionRow = this@FilterSectionRow
						}
					}
				)
			}

			// Add the section row only if we have data for this section
			if (data.isNotEmpty()) {
				data.add(0, this@FilterSectionRow)
			}

			return data
		}

	private val selectionType: SelectionType
		get() {
			return when (this) {
				CASH_TOURNAMENT, DYNAMIC_DATE, LIVE_ONLINE, SESSIONS -> SelectionType.SINGLE
				else -> SelectionType.MULTIPLE
			}
		}

	val exclusiveWith: List<FilterSectionRow>?
		get() {
			return when (this) {
				DYNAMIC_DATE -> arrayListOf(FIXED_DATE)
				FIXED_DATE -> arrayListOf(DYNAMIC_DATE)
				else -> null
			}
		}
}
