package net.pokeranalytics.android.model

import android.content.Context
import io.realm.Realm
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.interfaces.Deletable
import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.ui.view.Localizable
import net.pokeranalytics.android.util.extensions.findById

/**
 * An enum managing the business objects related to a realm results
 */
enum class LiveData : Localizable {
	BANKROLL,
	GAME,
	LOCATION,
	TOURNAMENT_NAME,
	TOURNAMENT_FEATURE,
	TRANSACTION,
	TRANSACTION_TYPE,
	FILTER,
	CUSTOM_FIELD,
	REPORT_SETUP;

    var subType:Int? = null

	val relatedEntity: Class<out Deletable>
		get() {
			return when (this) {
				BANKROLL -> Bankroll::class.java
				GAME -> Game::class.java
				LOCATION -> Location::class.java
				TOURNAMENT_NAME -> TournamentName::class.java
				TOURNAMENT_FEATURE -> TournamentFeature::class.java
				TRANSACTION -> Transaction::class.java
				TRANSACTION_TYPE -> TransactionType::class.java
				FILTER -> Filter::class.java
				CUSTOM_FIELD -> CustomField::class.java
				REPORT_SETUP -> ReportSetup::class.java
			}
		}

	fun updateOrCreate(realm: Realm, primaryKey: String?): Deletable {
		val proxyItem: Deletable? = this.getData(realm, primaryKey)
		proxyItem?.let {
			return realm.copyFromRealm(it)
		} ?: run {
			return this.newEntity()
		}
	}

	private fun newEntity(): Deletable {
		return this.relatedEntity.newInstance()
	}

	fun getData(realm: Realm, primaryKey: String?): Deletable? {
		var proxyItem: Deletable? = null
		primaryKey?.let {
			val t = realm.findById(this.relatedEntity, it)
			t?.let {
				proxyItem = t
			}
		}
		return proxyItem
	}

	override val resId: Int?
		get() {
			return when (this) {
				BANKROLL -> R.string.bankroll
				GAME -> R.string.game
				LOCATION -> R.string.location
				TOURNAMENT_NAME -> R.string.tournament_name
				TOURNAMENT_FEATURE -> R.string.tournament_feature
				TRANSACTION -> R.string.operation
				TRANSACTION_TYPE -> R.string.operation_type
				FILTER -> R.string.filter
				CUSTOM_FIELD -> R.string.custom_field
				REPORT_SETUP -> R.string.custom
			}
		}

	val pluralResId: Int
		get() {
			return when (this) {
				BANKROLL -> R.string.bankrolls
				GAME -> R.string.games
				LOCATION -> R.string.locations
				TOURNAMENT_NAME -> R.string.tournament_names
				TOURNAMENT_FEATURE -> R.string.tournament_features
				TRANSACTION -> R.string.operations
				TRANSACTION_TYPE -> R.string.operation_types
				FILTER -> R.string.filters
				CUSTOM_FIELD -> R.string.custom_fields
				REPORT_SETUP -> R.string.custom
			}
		}

    private val newResId: Int
        get() {
            return when (this) {
                BANKROLL -> R.string.new_bankroll
                GAME -> R.string.new_variant
                LOCATION -> R.string.new_location
                TOURNAMENT_NAME -> R.string.new_tournament_name
                TOURNAMENT_FEATURE -> R.string.new_tournament_feature
                TRANSACTION -> R.string.new_operation
                TRANSACTION_TYPE -> R.string.new_operation_type
                FILTER -> R.string.new_filter
                CUSTOM_FIELD -> R.string.new_custom_field
                REPORT_SETUP -> R.string.new_report
            }
        }



    /**
	 * Return the new entity titleResId
	 */
	fun newEntityLocalizedTitle(context: Context): String {
		return context.getString(this.newResId)
	}

	/**
	 * Return the update entity titleResId
	 */
	fun updateEntityLocalizedTitle(context: Context): String {
		return context.getString(R.string.update_entity, this.localizedTitle(context).toLowerCase())
	}

	/**
	 * Return the update entity titleResId
	 */
	fun pluralLocalizedTitle(context: Context): String {
		return context.getString(this.pluralResId, context)
	}

}
