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

import androidx.room.EntityInsertAdapter
import androidx.room.RoomDatabase
import androidx.room.util.appendPlaceholders
import androidx.room.util.getColumnIndexOrThrow
import androidx.room.util.performSuspending
import androidx.sqlite.SQLiteStatement
import javax.`annotation`.processing.Generated
import kotlin.Boolean
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.text.StringBuilder
import world.respect.datalayer.db.school.xapi.entities.XapiGroupMemberActorJoin

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

  private val __insertAdapterOfXapiGroupMemberActorJoin:
      EntityInsertAdapter<XapiGroupMemberActorJoin>

  private val __insertAdapterOfXapiGroupMemberActorJoin_1:
      EntityInsertAdapter<XapiGroupMemberActorJoin>
  init {
    this.__db = __db
    this.__insertAdapterOfXapiGroupMemberActorJoin = object : EntityInsertAdapter<XapiGroupMemberActorJoin>() {
      protected override fun createQuery(): String = "INSERT OR IGNORE INTO `XapiGroupMemberActorJoin` (`gmajGroupActorUid`,`gmajMemberActorUid`) VALUES (?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: XapiGroupMemberActorJoin) {
        statement.bindLong(1, entity.gmajGroupActorUid)
        statement.bindLong(2, entity.gmajMemberActorUid)
      }
    }
    this.__insertAdapterOfXapiGroupMemberActorJoin_1 = object : EntityInsertAdapter<XapiGroupMemberActorJoin>() {
      protected override fun createQuery(): String = "INSERT OR REPLACE INTO `XapiGroupMemberActorJoin` (`gmajGroupActorUid`,`gmajMemberActorUid`) VALUES (?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: XapiGroupMemberActorJoin) {
        statement.bindLong(1, entity.gmajGroupActorUid)
        statement.bindLong(2, entity.gmajMemberActorUid)
      }
    }
  }

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

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

  public override suspend fun findByGroupActorUidList(uidList: List<Long>, excludeIdentifiedGroups: Boolean): List<XapiGroupMemberActorJoin> {
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("        SELECT XapiGroupMemberActorJoin.*")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("          FROM XapiGroupMemberActorJoin")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("         WHERE XapiGroupMemberActorJoin.gmajGroupActorUid IN (")
    val _inputSize: Int = uidList.size
    appendPlaceholders(_stringBuilder, _inputSize)
    _stringBuilder.append(")")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("           AND (    ")
    _stringBuilder.append("?")
    _stringBuilder.append(" = 0")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("                 OR (SELECT XapiActorEntity.actorIsAnonGroup")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("                       FROM XapiActorEntity")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("                      WHERE XapiActorEntity.actorUid = XapiGroupMemberActorJoin.gmajGroupActorUid) = 1)")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("    ")
    val _sql: String = _stringBuilder.toString()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        for (_item: Long in uidList) {
          _stmt.bindLong(_argIndex, _item)
          _argIndex++
        }
        _argIndex = 1 + _inputSize
        val _tmp: Int = if (excludeIdentifiedGroups) 1 else 0
        _stmt.bindLong(_argIndex, _tmp.toLong())
        val _columnIndexOfGmajGroupActorUid: Int = getColumnIndexOrThrow(_stmt, "gmajGroupActorUid")
        val _columnIndexOfGmajMemberActorUid: Int = getColumnIndexOrThrow(_stmt, "gmajMemberActorUid")
        val _result: MutableList<XapiGroupMemberActorJoin> = mutableListOf()
        while (_stmt.step()) {
          val _item_1: XapiGroupMemberActorJoin
          val _tmpGmajGroupActorUid: Long
          _tmpGmajGroupActorUid = _stmt.getLong(_columnIndexOfGmajGroupActorUid)
          val _tmpGmajMemberActorUid: Long
          _tmpGmajMemberActorUid = _stmt.getLong(_columnIndexOfGmajMemberActorUid)
          _item_1 = XapiGroupMemberActorJoin(_tmpGmajGroupActorUid,_tmpGmajMemberActorUid)
          _result.add(_item_1)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

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

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