package com.ustadmobile.core.db.dao.xapi

import androidx.room.EntityInsertAdapter
import androidx.room.RoomDatabase
import androidx.room.util.getColumnIndexOrThrow
import androidx.room.util.performSuspending
import androidx.sqlite.SQLiteStatement
import javax.`annotation`.processing.Generated
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Suppress
import kotlin.Unit
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.collections.mutableListOf
import kotlin.reflect.KClass
import kotlin.time.Instant
import world.respect.datalayer.db.school.SchoolTypeConverters
import world.respect.datalayer.db.school.xapi.entities.XapiActivityLangMapEntry
import world.respect.datalayer.db.school.xapi.entities.XapiActivityLangMapEntryPropEnum
import world.respect.datalayer.db.shared.SharedConverters

@Generated(value = ["androidx.room.RoomProcessor"])
@Suppress(names = ["UNCHECKED_CAST", "DEPRECATION", "REDUNDANT_PROJECTION", "REMOVAL"])
public class XapiActivityLangMapEntryDao_Impl(
  __db: RoomDatabase,
) : XapiActivityLangMapEntryDao {
  private val __db: RoomDatabase

  private val __insertAdapterOfXapiActivityLangMapEntry:
      EntityInsertAdapter<XapiActivityLangMapEntry>

  private val __schoolTypeConverters: SchoolTypeConverters = SchoolTypeConverters()

  private val __sharedConverters: SharedConverters = SharedConverters()

  private val __insertAdapterOfXapiActivityLangMapEntry_1:
      EntityInsertAdapter<XapiActivityLangMapEntry>
  init {
    this.__db = __db
    this.__insertAdapterOfXapiActivityLangMapEntry = object : EntityInsertAdapter<XapiActivityLangMapEntry>() {
      protected override fun createQuery(): String = "INSERT OR REPLACE INTO `XapiActivityLangMapEntry` (`almeUid`,`almeActivityUid`,`almeProperty`,`almeInteractionId`,`almeLangCode`,`almeValue`,`almeLastModified`) VALUES (nullif(?, 0),?,?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: XapiActivityLangMapEntry) {
        statement.bindLong(1, entity.almeUid)
        statement.bindLong(2, entity.almeActivityUid)
        val _tmp: Int = __schoolTypeConverters.fromActivityLangMapEntryPropEnum(entity.almeProperty)
        statement.bindLong(3, _tmp.toLong())
        val _tmpAlmeInteractionId: String? = entity.almeInteractionId
        if (_tmpAlmeInteractionId == null) {
          statement.bindNull(4)
        } else {
          statement.bindText(4, _tmpAlmeInteractionId)
        }
        statement.bindText(5, entity.almeLangCode)
        statement.bindText(6, entity.almeValue)
        val _tmp_1: Long? = __sharedConverters.fromInstant(entity.almeLastModified)
        if (_tmp_1 == null) {
          statement.bindNull(7)
        } else {
          statement.bindLong(7, _tmp_1)
        }
      }
    }
    this.__insertAdapterOfXapiActivityLangMapEntry_1 = object : EntityInsertAdapter<XapiActivityLangMapEntry>() {
      protected override fun createQuery(): String = "INSERT OR IGNORE INTO `XapiActivityLangMapEntry` (`almeUid`,`almeActivityUid`,`almeProperty`,`almeInteractionId`,`almeLangCode`,`almeValue`,`almeLastModified`) VALUES (nullif(?, 0),?,?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: XapiActivityLangMapEntry) {
        statement.bindLong(1, entity.almeUid)
        statement.bindLong(2, entity.almeActivityUid)
        val _tmp: Int = __schoolTypeConverters.fromActivityLangMapEntryPropEnum(entity.almeProperty)
        statement.bindLong(3, _tmp.toLong())
        val _tmpAlmeInteractionId: String? = entity.almeInteractionId
        if (_tmpAlmeInteractionId == null) {
          statement.bindNull(4)
        } else {
          statement.bindText(4, _tmpAlmeInteractionId)
        }
        statement.bindText(5, entity.almeLangCode)
        statement.bindText(6, entity.almeValue)
        val _tmp_1: Long? = __sharedConverters.fromInstant(entity.almeLastModified)
        if (_tmp_1 == null) {
          statement.bindNull(7)
        } else {
          statement.bindLong(7, _tmp_1)
        }
      }
    }
  }

  public override suspend fun upsertList(entities: List<XapiActivityLangMapEntry>): Unit = performSuspending(__db, false, true) { _connection ->
    __insertAdapterOfXapiActivityLangMapEntry.insert(_connection, entities)
  }

  public override suspend fun insertOrIgnore(entities: List<XapiActivityLangMapEntry>): Unit = performSuspending(__db, false, true) { _connection ->
    __insertAdapterOfXapiActivityLangMapEntry_1.insert(_connection, entities)
  }

  public override suspend fun findAllByActivityUid(activityUid: Long): List<XapiActivityLangMapEntry> {
    val _sql: String = """
        |
        |        SELECT XapiActivityLangMapEntry.*
        |          FROM XapiActivityLangMapEntry
        |         WHERE XapiActivityLangMapEntry.almeActivityUid = ?
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, activityUid)
        val _columnIndexOfAlmeUid: Int = getColumnIndexOrThrow(_stmt, "almeUid")
        val _columnIndexOfAlmeActivityUid: Int = getColumnIndexOrThrow(_stmt, "almeActivityUid")
        val _columnIndexOfAlmeProperty: Int = getColumnIndexOrThrow(_stmt, "almeProperty")
        val _columnIndexOfAlmeInteractionId: Int = getColumnIndexOrThrow(_stmt, "almeInteractionId")
        val _columnIndexOfAlmeLangCode: Int = getColumnIndexOrThrow(_stmt, "almeLangCode")
        val _columnIndexOfAlmeValue: Int = getColumnIndexOrThrow(_stmt, "almeValue")
        val _columnIndexOfAlmeLastModified: Int = getColumnIndexOrThrow(_stmt, "almeLastModified")
        val _result: MutableList<XapiActivityLangMapEntry> = mutableListOf()
        while (_stmt.step()) {
          val _item: XapiActivityLangMapEntry
          val _tmpAlmeUid: Long
          _tmpAlmeUid = _stmt.getLong(_columnIndexOfAlmeUid)
          val _tmpAlmeActivityUid: Long
          _tmpAlmeActivityUid = _stmt.getLong(_columnIndexOfAlmeActivityUid)
          val _tmpAlmeProperty: XapiActivityLangMapEntryPropEnum
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfAlmeProperty).toInt()
          _tmpAlmeProperty = __schoolTypeConverters.toActivityLangMapEntryPropEnum(_tmp)
          val _tmpAlmeInteractionId: String?
          if (_stmt.isNull(_columnIndexOfAlmeInteractionId)) {
            _tmpAlmeInteractionId = null
          } else {
            _tmpAlmeInteractionId = _stmt.getText(_columnIndexOfAlmeInteractionId)
          }
          val _tmpAlmeLangCode: String
          _tmpAlmeLangCode = _stmt.getText(_columnIndexOfAlmeLangCode)
          val _tmpAlmeValue: String
          _tmpAlmeValue = _stmt.getText(_columnIndexOfAlmeValue)
          val _tmpAlmeLastModified: Instant
          val _tmp_1: Long?
          if (_stmt.isNull(_columnIndexOfAlmeLastModified)) {
            _tmp_1 = null
          } else {
            _tmp_1 = _stmt.getLong(_columnIndexOfAlmeLastModified)
          }
          val _tmp_2: Instant? = __sharedConverters.toInstant(_tmp_1)
          if (_tmp_2 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpAlmeLastModified = _tmp_2
          }
          _item = XapiActivityLangMapEntry(_tmpAlmeUid,_tmpAlmeActivityUid,_tmpAlmeProperty,_tmpAlmeInteractionId,_tmpAlmeLangCode,_tmpAlmeValue,_tmpAlmeLastModified)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun updateIfChanged(
    almeActivityUid: Long,
    almeProperty: Int,
    almeValue: String,
    almeInteractionId: String?,
    changeTime: Long,
  ) {
    val _sql: String = """
        |
        |        UPDATE XapiActivityLangMapEntry
        |           SET almeValue = ?
        |         WHERE almeActivityUid = ?
        |           AND almeProperty = ?
        |           AND almeInteractionId = ?
        |           AND almeLastModified > ?
        |           AND almeValue != ?
        |    
        """.trimMargin()
    return performSuspending(__db, false, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, almeValue)
        _argIndex = 2
        _stmt.bindLong(_argIndex, almeActivityUid)
        _argIndex = 3
        _stmt.bindLong(_argIndex, almeProperty.toLong())
        _argIndex = 4
        if (almeInteractionId == null) {
          _stmt.bindNull(_argIndex)
        } else {
          _stmt.bindText(_argIndex, almeInteractionId)
        }
        _argIndex = 5
        _stmt.bindLong(_argIndex, changeTime)
        _argIndex = 6
        _stmt.bindText(_argIndex, almeValue)
        _stmt.step()
      } finally {
        _stmt.close()
      }
    }
  }

  public companion object {
    public fun getRequiredConverters(): List<KClass<*>> = emptyList()
  }
}
