package net.pokeranalytics.android.model.migrations

import io.realm.DynamicRealm
import io.realm.RealmMigration
import timber.log.Timber




class PokerAnalyticsMigration : RealmMigration {

	override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {

		// DynamicRealm exposes an editable schema
		val schema = realm.schema

		var currentVersion = oldVersion.toInt()
		Timber.d("*** migrate from $oldVersion to $newVersion")

		// Migrate to version 1
		if (currentVersion == 0) {
			Timber.d("*** Running migration 1")

			schema.get("Filter")?.addField("entityType", Int::class.java)?.setNullable("entityType", true)
			schema.get("FilterElement")?.let {
				it.setNullable("filterName", true)
				it.setNullable("sectionName", true)
			}
			schema.get("FilterElementBlind")?.renameField("code", "currencyCode")
			currentVersion++
		}

		// Migrate to version 2
		if (currentVersion == 1) {
			Timber.d("*** Running migration ${currentVersion + 1}")
			schema.rename("FilterElement", "FilterCondition")

			schema.get("Filter")?.renameField("filterElements", "filterConditions")

			schema.get("SessionSet")?.let {
				it.addField("id", String::class.java).setRequired("id", true)
				it.addPrimaryKey("id")
			}
			currentVersion++
		}

		// Migrate to version 3
		if (currentVersion == 2) {
			Timber.d("*** Running migration ${currentVersion + 1}")
			schema.rename("Report", "ReportSetup")

			schema.get("Filter")?.removeField("entityType")

            schema.get("Session")?.let {
                it.addField("blinds", String::class.java).transform {

                }
            }

            schema.get("FilterCondition")?.let {
                it.removeField("blindValues")
                it.removeField("numericValues")

                it.addField("operator", Integer::class.java)
                it.addField("intValue", Integer::class.java)
                it.addRealmListField("intValues", Integer::class.java)
                it.addField("doubleValue", Double::class.java).setNullable("doubleValue", true)
                it.addRealmListField("doubleValues", Double::class.java)
                if(it.isRequired("doubleValues")) {
                    it.setRequired("doubleValues", false)
                }
                it.addField("stringValue", String::class.java)
            }

			schema.get("ComputableResult")?.removeField("sessionSet")

			schema.get("Bankroll")?.addField("initialValue", Double::class.java)

            currentVersion++
		}

        // Migrate to version 4
        if (currentVersion == 3) {
            Timber.d("*** Running migration ${currentVersion + 1}")

	        schema.get("Result")?.addField("numberOfRebuy", Double::class.java)?.setNullable("numberOfRebuy", true)
            currentVersion++
        }

		// Migrate to version 5
		if (currentVersion == 4) {
			Timber.d("*** Running migration ${currentVersion + 1}")
			schema.get("Bankroll")?.removeField("transactions")
			currentVersion++
		}

	}

	override fun equals(other: Any?): Boolean {
		return other is RealmMigration
	}

	override fun hashCode(): Int {
		return RealmMigration::javaClass.hashCode()
	}
}