package world.respect.datalayer.db.school.xapi.daos

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 world.respect.datalayer.db.school.xapi.entities.XapiActivityExtensionEntity

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

  private val __insertAdapterOfXapiActivityExtensionEntity:
      EntityInsertAdapter<XapiActivityExtensionEntity>

  private val __insertAdapterOfXapiActivityExtensionEntity_1:
      EntityInsertAdapter<XapiActivityExtensionEntity>
  init {
    this.__db = __db
    this.__insertAdapterOfXapiActivityExtensionEntity = object : EntityInsertAdapter<XapiActivityExtensionEntity>() {
      protected override fun createQuery(): String = "INSERT OR REPLACE INTO `XapiActivityExtensionEntity` (`aeeActivityUid`,`aeeKeyHash`,`aeeKey`,`aeeJson`,`aeeLastMod`) VALUES (?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: XapiActivityExtensionEntity) {
        statement.bindLong(1, entity.aeeActivityUid)
        statement.bindLong(2, entity.aeeKeyHash)
        statement.bindText(3, entity.aeeKey)
        statement.bindText(4, entity.aeeJson)
        statement.bindLong(5, entity.aeeLastMod)
      }
    }
    this.__insertAdapterOfXapiActivityExtensionEntity_1 = object : EntityInsertAdapter<XapiActivityExtensionEntity>() {
      protected override fun createQuery(): String = "INSERT OR IGNORE INTO `XapiActivityExtensionEntity` (`aeeActivityUid`,`aeeKeyHash`,`aeeKey`,`aeeJson`,`aeeLastMod`) VALUES (?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: XapiActivityExtensionEntity) {
        statement.bindLong(1, entity.aeeActivityUid)
        statement.bindLong(2, entity.aeeKeyHash)
        statement.bindText(3, entity.aeeKey)
        statement.bindText(4, entity.aeeJson)
        statement.bindLong(5, entity.aeeLastMod)
      }
    }
  }

  public override suspend fun upsertListAsync(list: List<XapiActivityExtensionEntity>): Unit = performSuspending(__db, false, true) { _connection ->
    __insertAdapterOfXapiActivityExtensionEntity.insert(_connection, list)
  }

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

  public override suspend fun findAllByActivityContextUids(activityUid: Long, filterByKeyHash: Long): List<XapiActivityExtensionEntity> {
    val _sql: String = """
        |
        |        WITH ActivityUids(activityUid) AS (
        |             SELECT DISTINCT XapiStatementContextActivityJoin.scajToActivityUid
        |               FROM XapiStatementContextActivityJoin
        |              WHERE (XapiStatementContextActivityJoin.scajFromStatementIdHi, XapiStatementContextActivityJoin.scajFromStatementIdLo) IN
        |                    (SELECT XapiStatementEntity.statementIdHi, XapiStatementEntity.statementIdLo
        |                       FROM XapiStatementEntity
        |                      WHERE XapiStatementEntity.statementObjectUid1 = ?
        |                        AND XapiStatementEntity.statementObjectType = 5)
        |                        
        |             )
        |        
        |        SELECT XapiActivityExtensionEntity.* 
        |          FROM XapiActivityExtensionEntity
        |         WHERE aeeActivityUid IN (SELECT activityUid FROM ActivityUids)
        |           AND aeeKeyHash = ?
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, activityUid)
        _argIndex = 2
        _stmt.bindLong(_argIndex, filterByKeyHash)
        val _columnIndexOfAeeActivityUid: Int = getColumnIndexOrThrow(_stmt, "aeeActivityUid")
        val _columnIndexOfAeeKeyHash: Int = getColumnIndexOrThrow(_stmt, "aeeKeyHash")
        val _columnIndexOfAeeKey: Int = getColumnIndexOrThrow(_stmt, "aeeKey")
        val _columnIndexOfAeeJson: Int = getColumnIndexOrThrow(_stmt, "aeeJson")
        val _columnIndexOfAeeLastMod: Int = getColumnIndexOrThrow(_stmt, "aeeLastMod")
        val _result: MutableList<XapiActivityExtensionEntity> = mutableListOf()
        while (_stmt.step()) {
          val _item: XapiActivityExtensionEntity
          val _tmpAeeActivityUid: Long
          _tmpAeeActivityUid = _stmt.getLong(_columnIndexOfAeeActivityUid)
          val _tmpAeeKeyHash: Long
          _tmpAeeKeyHash = _stmt.getLong(_columnIndexOfAeeKeyHash)
          val _tmpAeeKey: String
          _tmpAeeKey = _stmt.getText(_columnIndexOfAeeKey)
          val _tmpAeeJson: String
          _tmpAeeJson = _stmt.getText(_columnIndexOfAeeJson)
          val _tmpAeeLastMod: Long
          _tmpAeeLastMod = _stmt.getLong(_columnIndexOfAeeLastMod)
          _item = XapiActivityExtensionEntity(_tmpAeeActivityUid,_tmpAeeKeyHash,_tmpAeeKey,_tmpAeeJson,_tmpAeeLastMod)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun deleteByActivityUid(activityUid: Long) {
    val _sql: String = """
        |
        |        DELETE FROM XapiActivityExtensionEntity 
        |              WHERE aeeActivityUid = ?
        |    
        """.trimMargin()
    return performSuspending(__db, false, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, activityUid)
        _stmt.step()
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun updateIfNewer(
    activityUid: Long,
    keyHash: Long,
    json: String,
    changeTime: Long,
  ) {
    val _sql: String = """
        |
        |        UPDATE XapiActivityExtensionEntity
        |           SET aeeJson = ?, 
        |               aeeLastMod = ?
        |         WHERE aeeActivityUid = ? 
        |           AND aeeKeyHash = ?
        |           AND aeeLastMod < ?
        |    
        """.trimMargin()
    return performSuspending(__db, false, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, json)
        _argIndex = 2
        _stmt.bindLong(_argIndex, changeTime)
        _argIndex = 3
        _stmt.bindLong(_argIndex, activityUid)
        _argIndex = 4
        _stmt.bindLong(_argIndex, keyHash)
        _argIndex = 5
        _stmt.bindLong(_argIndex, changeTime)
        _stmt.step()
      } finally {
        _stmt.close()
      }
    }
  }

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