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

import io.realm.Realm
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.CustomField
import net.pokeranalytics.android.ui.interfaces.FilterableType
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterSectionRow.*
import net.pokeranalytics.android.util.extensions.sorted

enum class FilterCategoryRow(override val resId: Int?, override val viewType: Int = RowViewType.TITLE_VALUE_ARROW.ordinal) : RowRepresentable {
	GENERAL(R.string.general),
	DATE(R.string.date),
	TIME_FRAME(R.string.duration),
	//Sessions(R.string.sessions),
	CASH(R.string.cash),
	TOURNAMENT(R.string.tournament),
	//ONLINE(R.string.online),
	RESULT(R.string.result),
	TRANSACTION_TYPES(R.string.operation_types),
	LOCATIONS(R.string.locations),
	BANKROLLS(R.string.bankrolls),
	//PLAYERS(R.string.players),
    CUSTOM_FIELDS(R.string.custom_fields),
	;

    companion object {
        fun values(filterableType:FilterableType): ArrayList<FilterCategoryRow> {
            val list = arrayListOf<FilterCategoryRow>()
            for (category in values()) {
                if (category.isAllowed(filterableType)) {
                    list.add(category)
                }
            }
            return list
        }
    }

    fun isAllowed(filterableType:FilterableType): Boolean {
        return when (filterableType) {
            FilterableType.SESSION -> {
                when (this) {
                    TRANSACTION_TYPES -> false
                    else -> true
                }
            }
            FilterableType.TRANSACTION -> {
                when (this) {
                    DATE, TRANSACTION_TYPES -> true
                    else -> false
                }
            }
            else -> {
                when (this) {
                    DATE -> true
                    else -> false
                }
            }
        }
    }

	val filterElements: List<RowRepresentable>
		get() {
			return filterSectionRows.flatMap {
				it.filterElements
			}
		}

	val filterSectionRows: List<FilterSectionRow>
		get() {
			return when (this) {
				GENERAL -> arrayListOf(
					CashOrTournament,
					LiveOrOnline,
					Game,
                    LimitType,
					TableSize
				)
				DATE -> arrayListOf(
					DynamicDate,
					FixedDate,
					Duration,
					Year,
					WeekdayOrWeekend,
					DayOfWeek,
					MonthOfYear
				)
				TIME_FRAME -> arrayListOf(
					SessionDuration,
					TimeFrameRange
				)
				//Sessions -> arrayListOf(FilterSectionRow.Sessions)
				BANKROLLS -> arrayListOf(
					Bankroll
				)
				CASH -> arrayListOf(
					Blind
				)
				TOURNAMENT -> arrayListOf(
					TournamentType,
                    TournamentName,
                    TournamentFeature,
                    TournamentEntryFee,
                    TournamentNumberOfPlayer,
                    TournamentFinalPosition
				)
				//ONLINE -> arrayListOf(MultiTabling)
				LOCATIONS -> arrayListOf(
					Location
				)
                //PLAYERS -> arrayListOf(MultiPlayer)
				RESULT -> arrayListOf(
					Value,
                    NumberOfRebuy
				)

                CUSTOM_FIELDS -> {
                    val realm = Realm.getDefaultInstance()
                    val sections = realm.sorted(CustomField::class.java).map {
                        CustomField(it)
                    }
                    realm.close()
                    sections
                }

				TRANSACTION_TYPES -> arrayListOf(TransactionType)
			}
		}
}
