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

import androidx.room.EntityInsertAdapter
import androidx.room.RoomDatabase
import androidx.room.util.getColumnIndexOrThrow
import androidx.room.util.performSuspending
import androidx.sqlite.SQLiteStatement
import com.eygraber.uri.Uri
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.opds.OpdsParentType
import world.respect.datalayer.db.school.opds.OpdsTypeConverters
import world.respect.datalayer.db.school.opds.entities.ReadiumSubjectEntity
import world.respect.datalayer.db.shared.SharedConverters

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

  private val __insertAdapterOfReadiumSubjectEntity: EntityInsertAdapter<ReadiumSubjectEntity>

  private val __opdsTypeConverters: OpdsTypeConverters = OpdsTypeConverters()

  private val __sharedConverters: SharedConverters = SharedConverters()
  init {
    this.__db = __db
    this.__insertAdapterOfReadiumSubjectEntity = object : EntityInsertAdapter<ReadiumSubjectEntity>() {
      protected override fun createQuery(): String = "INSERT OR ABORT INTO `ReadiumSubjectEntity` (`rseUid`,`rseStringValue`,`rseTopParentType`,`rseTopParentUid`,`rseSubjectSortAs`,`rseSubjectCode`,`rseSubjectScheme`,`rseIndex`) VALUES (nullif(?, 0),?,?,?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: ReadiumSubjectEntity) {
        statement.bindLong(1, entity.rseUid)
        val _tmpRseStringValue: String? = entity.rseStringValue
        if (_tmpRseStringValue == null) {
          statement.bindNull(2)
        } else {
          statement.bindText(2, _tmpRseStringValue)
        }
        val _tmp: Int = __opdsTypeConverters.fromOpdsTopParentType(entity.rseTopParentType)
        statement.bindLong(3, _tmp.toLong())
        statement.bindLong(4, entity.rseTopParentUid)
        val _tmpRseSubjectSortAs: String? = entity.rseSubjectSortAs
        if (_tmpRseSubjectSortAs == null) {
          statement.bindNull(5)
        } else {
          statement.bindText(5, _tmpRseSubjectSortAs)
        }
        val _tmpRseSubjectCode: String? = entity.rseSubjectCode
        if (_tmpRseSubjectCode == null) {
          statement.bindNull(6)
        } else {
          statement.bindText(6, _tmpRseSubjectCode)
        }
        val _tmpRseSubjectScheme: Uri? = entity.rseSubjectScheme
        val _tmp_1: String? = __sharedConverters.fromUri(_tmpRseSubjectScheme)
        if (_tmp_1 == null) {
          statement.bindNull(7)
        } else {
          statement.bindText(7, _tmp_1)
        }
        statement.bindLong(8, entity.rseIndex.toLong())
      }
    }
  }

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

  public override suspend fun findAllByFeedUid(feedUid: Long): List<ReadiumSubjectEntity> {
    val _sql: String = """
        |
        |         WITH 
        |            FeedPublicationUids(publicationUid) AS(
        |             SELECT OpdsPublicationEntity.opeUid
        |               FROM OpdsPublicationEntity
        |              WHERE OpdsPublicationEntity.opeOfeUid = ? 
        |        )
        |        
        |
        |       SELECT ReadiumSubjectEntity.*
        |         FROM ReadiumSubjectEntity
        |        WHERE 
        |              (     ReadiumSubjectEntity.rseTopParentType = 1
        |                AND ReadiumSubjectEntity.rseTopParentUid = ?)
        |          OR  (     ReadiumSubjectEntity.rseTopParentType = 2
        |                AND ReadiumSubjectEntity.rseTopParentUid IN (SELECT publicationUid FROM FeedPublicationUids))
        |        
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, feedUid)
        _argIndex = 2
        _stmt.bindLong(_argIndex, feedUid)
        val _columnIndexOfRseUid: Int = getColumnIndexOrThrow(_stmt, "rseUid")
        val _columnIndexOfRseStringValue: Int = getColumnIndexOrThrow(_stmt, "rseStringValue")
        val _columnIndexOfRseTopParentType: Int = getColumnIndexOrThrow(_stmt, "rseTopParentType")
        val _columnIndexOfRseTopParentUid: Int = getColumnIndexOrThrow(_stmt, "rseTopParentUid")
        val _columnIndexOfRseSubjectSortAs: Int = getColumnIndexOrThrow(_stmt, "rseSubjectSortAs")
        val _columnIndexOfRseSubjectCode: Int = getColumnIndexOrThrow(_stmt, "rseSubjectCode")
        val _columnIndexOfRseSubjectScheme: Int = getColumnIndexOrThrow(_stmt, "rseSubjectScheme")
        val _columnIndexOfRseIndex: Int = getColumnIndexOrThrow(_stmt, "rseIndex")
        val _result: MutableList<ReadiumSubjectEntity> = mutableListOf()
        while (_stmt.step()) {
          val _item: ReadiumSubjectEntity
          val _tmpRseUid: Long
          _tmpRseUid = _stmt.getLong(_columnIndexOfRseUid)
          val _tmpRseStringValue: String?
          if (_stmt.isNull(_columnIndexOfRseStringValue)) {
            _tmpRseStringValue = null
          } else {
            _tmpRseStringValue = _stmt.getText(_columnIndexOfRseStringValue)
          }
          val _tmpRseTopParentType: OpdsParentType
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfRseTopParentType).toInt()
          _tmpRseTopParentType = __opdsTypeConverters.toOpdsTopParentType(_tmp)
          val _tmpRseTopParentUid: Long
          _tmpRseTopParentUid = _stmt.getLong(_columnIndexOfRseTopParentUid)
          val _tmpRseSubjectSortAs: String?
          if (_stmt.isNull(_columnIndexOfRseSubjectSortAs)) {
            _tmpRseSubjectSortAs = null
          } else {
            _tmpRseSubjectSortAs = _stmt.getText(_columnIndexOfRseSubjectSortAs)
          }
          val _tmpRseSubjectCode: String?
          if (_stmt.isNull(_columnIndexOfRseSubjectCode)) {
            _tmpRseSubjectCode = null
          } else {
            _tmpRseSubjectCode = _stmt.getText(_columnIndexOfRseSubjectCode)
          }
          val _tmpRseSubjectScheme: Uri?
          val _tmp_1: String?
          if (_stmt.isNull(_columnIndexOfRseSubjectScheme)) {
            _tmp_1 = null
          } else {
            _tmp_1 = _stmt.getText(_columnIndexOfRseSubjectScheme)
          }
          _tmpRseSubjectScheme = __sharedConverters.toUri(_tmp_1)
          val _tmpRseIndex: Int
          _tmpRseIndex = _stmt.getLong(_columnIndexOfRseIndex).toInt()
          _item = ReadiumSubjectEntity(_tmpRseUid,_tmpRseStringValue,_tmpRseTopParentType,_tmpRseTopParentUid,_tmpRseSubjectSortAs,_tmpRseSubjectCode,_tmpRseSubjectScheme,_tmpRseIndex)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun findAllByPubUid(publicationUid: Long): List<ReadiumSubjectEntity> {
    val _sql: String = """
        |
        |        SELECT ReadiumSubjectEntity.*
        |          FROM ReadiumSubjectEntity
        |         WHERE ReadiumSubjectEntity.rseTopParentType = 2
        |           AND ReadiumSubjectEntity.rseTopParentUid = ?
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, publicationUid)
        val _columnIndexOfRseUid: Int = getColumnIndexOrThrow(_stmt, "rseUid")
        val _columnIndexOfRseStringValue: Int = getColumnIndexOrThrow(_stmt, "rseStringValue")
        val _columnIndexOfRseTopParentType: Int = getColumnIndexOrThrow(_stmt, "rseTopParentType")
        val _columnIndexOfRseTopParentUid: Int = getColumnIndexOrThrow(_stmt, "rseTopParentUid")
        val _columnIndexOfRseSubjectSortAs: Int = getColumnIndexOrThrow(_stmt, "rseSubjectSortAs")
        val _columnIndexOfRseSubjectCode: Int = getColumnIndexOrThrow(_stmt, "rseSubjectCode")
        val _columnIndexOfRseSubjectScheme: Int = getColumnIndexOrThrow(_stmt, "rseSubjectScheme")
        val _columnIndexOfRseIndex: Int = getColumnIndexOrThrow(_stmt, "rseIndex")
        val _result: MutableList<ReadiumSubjectEntity> = mutableListOf()
        while (_stmt.step()) {
          val _item: ReadiumSubjectEntity
          val _tmpRseUid: Long
          _tmpRseUid = _stmt.getLong(_columnIndexOfRseUid)
          val _tmpRseStringValue: String?
          if (_stmt.isNull(_columnIndexOfRseStringValue)) {
            _tmpRseStringValue = null
          } else {
            _tmpRseStringValue = _stmt.getText(_columnIndexOfRseStringValue)
          }
          val _tmpRseTopParentType: OpdsParentType
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfRseTopParentType).toInt()
          _tmpRseTopParentType = __opdsTypeConverters.toOpdsTopParentType(_tmp)
          val _tmpRseTopParentUid: Long
          _tmpRseTopParentUid = _stmt.getLong(_columnIndexOfRseTopParentUid)
          val _tmpRseSubjectSortAs: String?
          if (_stmt.isNull(_columnIndexOfRseSubjectSortAs)) {
            _tmpRseSubjectSortAs = null
          } else {
            _tmpRseSubjectSortAs = _stmt.getText(_columnIndexOfRseSubjectSortAs)
          }
          val _tmpRseSubjectCode: String?
          if (_stmt.isNull(_columnIndexOfRseSubjectCode)) {
            _tmpRseSubjectCode = null
          } else {
            _tmpRseSubjectCode = _stmt.getText(_columnIndexOfRseSubjectCode)
          }
          val _tmpRseSubjectScheme: Uri?
          val _tmp_1: String?
          if (_stmt.isNull(_columnIndexOfRseSubjectScheme)) {
            _tmp_1 = null
          } else {
            _tmp_1 = _stmt.getText(_columnIndexOfRseSubjectScheme)
          }
          _tmpRseSubjectScheme = __sharedConverters.toUri(_tmp_1)
          val _tmpRseIndex: Int
          _tmpRseIndex = _stmt.getLong(_columnIndexOfRseIndex).toInt()
          _item = ReadiumSubjectEntity(_tmpRseUid,_tmpRseStringValue,_tmpRseTopParentType,_tmpRseTopParentUid,_tmpRseSubjectSortAs,_tmpRseSubjectCode,_tmpRseSubjectScheme,_tmpRseIndex)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun deleteAllByFeedUid(feedUid: Long) {
    val _sql: String = """
        |
        |        WITH 
        |            FeedPublicationUids(publicationUid) AS(
        |             SELECT OpdsPublicationEntity.opeUid
        |               FROM OpdsPublicationEntity
        |              WHERE OpdsPublicationEntity.opeOfeUid = ? 
        |        )
        |        
        |
        |       DELETE
        |         FROM ReadiumSubjectEntity
        |        WHERE 
        |              (     ReadiumSubjectEntity.rseTopParentType = 1
        |                AND ReadiumSubjectEntity.rseTopParentUid = ?)
        |          OR  (     ReadiumSubjectEntity.rseTopParentType = 2
        |                AND ReadiumSubjectEntity.rseTopParentUid IN (SELECT publicationUid FROM FeedPublicationUids))
        |        
        |    
        """.trimMargin()
    return performSuspending(__db, false, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, feedUid)
        _argIndex = 2
        _stmt.bindLong(_argIndex, feedUid)
        _stmt.step()
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun deleteAllByPublicationUid(publicationUid: Long) {
    val _sql: String = """
        |
        |       DELETE
        |         FROM ReadiumSubjectEntity
        |        WHERE ReadiumSubjectEntity.rseTopParentUid = ?
        |          AND ReadiumSubjectEntity.rseTopParentType = 2
        |    
        """.trimMargin()
    return performSuspending(__db, false, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, publicationUid)
        _stmt.step()
      } finally {
        _stmt.close()
      }
    }
  }

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