package com.optix.android.data.local;

import android.database.Cursor;
import androidx.annotation.NonNull;
import androidx.room.CoroutinesRoom;
import androidx.room.EntityInsertionAdapter;
import androidx.room.RoomDatabase;
import androidx.room.RoomSQLiteQuery;
import androidx.room.SharedSQLiteStatement;
import androidx.room.util.CursorUtil;
import androidx.room.util.DBUtil;
import androidx.room.util.StringUtil;
import androidx.sqlite.db.SupportSQLiteStatement;
import com.optix.android.data.local.entities.NodeEntity;
import java.lang.Class;
import java.lang.Exception;
import java.lang.Integer;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import java.lang.SuppressWarnings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import javax.annotation.processing.Generated;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlinx.coroutines.flow.Flow;

@Generated("androidx.room.RoomProcessor")
@SuppressWarnings({"unchecked", "deprecation"})
public final class NodeDao_Impl implements NodeDao {
  private final RoomDatabase __db;

  private final EntityInsertionAdapter<NodeEntity> __insertionAdapterOfNodeEntity;

  private final SharedSQLiteStatement __preparedStmtOfClear;

  private final SharedSQLiteStatement __preparedStmtOfMarkAllStale;

  public NodeDao_Impl(@NonNull final RoomDatabase __db) {
    this.__db = __db;
    this.__insertionAdapterOfNodeEntity = new EntityInsertionAdapter<NodeEntity>(__db) {
      @Override
      @NonNull
      protected String createQuery() {
        return "INSERT OR REPLACE INTO `nodes` (`nodeId`,`ip`,`hostname`,`status`,`openPortsJson`,`latencyMs`,`macAddress`,`deviceType`,`alias`,`transport`,`security`,`signalLevelDbm`,`note`,`firstSeenAt`,`lastSeenAt`,`seenCount`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
      }

      @Override
      protected void bind(@NonNull final SupportSQLiteStatement statement,
          @NonNull final NodeEntity entity) {
        statement.bindString(1, entity.getNodeId());
        statement.bindString(2, entity.getIp());
        if (entity.getHostname() == null) {
          statement.bindNull(3);
        } else {
          statement.bindString(3, entity.getHostname());
        }
        statement.bindString(4, entity.getStatus());
        statement.bindString(5, entity.getOpenPortsJson());
        statement.bindDouble(6, entity.getLatencyMs());
        if (entity.getMacAddress() == null) {
          statement.bindNull(7);
        } else {
          statement.bindString(7, entity.getMacAddress());
        }
        if (entity.getDeviceType() == null) {
          statement.bindNull(8);
        } else {
          statement.bindString(8, entity.getDeviceType());
        }
        if (entity.getAlias() == null) {
          statement.bindNull(9);
        } else {
          statement.bindString(9, entity.getAlias());
        }
        statement.bindString(10, entity.getTransport());
        if (entity.getSecurity() == null) {
          statement.bindNull(11);
        } else {
          statement.bindString(11, entity.getSecurity());
        }
        if (entity.getSignalLevelDbm() == null) {
          statement.bindNull(12);
        } else {
          statement.bindLong(12, entity.getSignalLevelDbm());
        }
        if (entity.getNote() == null) {
          statement.bindNull(13);
        } else {
          statement.bindString(13, entity.getNote());
        }
        statement.bindLong(14, entity.getFirstSeenAt());
        statement.bindLong(15, entity.getLastSeenAt());
        statement.bindLong(16, entity.getSeenCount());
      }
    };
    this.__preparedStmtOfClear = new SharedSQLiteStatement(__db) {
      @Override
      @NonNull
      public String createQuery() {
        final String _query = "DELETE FROM nodes";
        return _query;
      }
    };
    this.__preparedStmtOfMarkAllStale = new SharedSQLiteStatement(__db) {
      @Override
      @NonNull
      public String createQuery() {
        final String _query = "UPDATE nodes SET status = 'STALE'";
        return _query;
      }
    };
  }

  @Override
  public Object upsertAll(final List<NodeEntity> nodes,
      final Continuation<? super Unit> $completion) {
    return CoroutinesRoom.execute(__db, true, new Callable<Unit>() {
      @Override
      @NonNull
      public Unit call() throws Exception {
        __db.beginTransaction();
        try {
          __insertionAdapterOfNodeEntity.insert(nodes);
          __db.setTransactionSuccessful();
          return Unit.INSTANCE;
        } finally {
          __db.endTransaction();
        }
      }
    }, $completion);
  }

  @Override
  public Object clear(final Continuation<? super Unit> $completion) {
    return CoroutinesRoom.execute(__db, true, new Callable<Unit>() {
      @Override
      @NonNull
      public Unit call() throws Exception {
        final SupportSQLiteStatement _stmt = __preparedStmtOfClear.acquire();
        try {
          __db.beginTransaction();
          try {
            _stmt.executeUpdateDelete();
            __db.setTransactionSuccessful();
            return Unit.INSTANCE;
          } finally {
            __db.endTransaction();
          }
        } finally {
          __preparedStmtOfClear.release(_stmt);
        }
      }
    }, $completion);
  }

  @Override
  public Object markAllStale(final Continuation<? super Unit> $completion) {
    return CoroutinesRoom.execute(__db, true, new Callable<Unit>() {
      @Override
      @NonNull
      public Unit call() throws Exception {
        final SupportSQLiteStatement _stmt = __preparedStmtOfMarkAllStale.acquire();
        try {
          __db.beginTransaction();
          try {
            _stmt.executeUpdateDelete();
            __db.setTransactionSuccessful();
            return Unit.INSTANCE;
          } finally {
            __db.endTransaction();
          }
        } finally {
          __preparedStmtOfMarkAllStale.release(_stmt);
        }
      }
    }, $completion);
  }

  @Override
  public Flow<List<NodeEntity>> observeAll() {
    final String _sql = "SELECT * FROM nodes ORDER BY lastSeenAt DESC, ip ASC";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    return CoroutinesRoom.createFlow(__db, false, new String[] {"nodes"}, new Callable<List<NodeEntity>>() {
      @Override
      @NonNull
      public List<NodeEntity> call() throws Exception {
        final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
        try {
          final int _cursorIndexOfNodeId = CursorUtil.getColumnIndexOrThrow(_cursor, "nodeId");
          final int _cursorIndexOfIp = CursorUtil.getColumnIndexOrThrow(_cursor, "ip");
          final int _cursorIndexOfHostname = CursorUtil.getColumnIndexOrThrow(_cursor, "hostname");
          final int _cursorIndexOfStatus = CursorUtil.getColumnIndexOrThrow(_cursor, "status");
          final int _cursorIndexOfOpenPortsJson = CursorUtil.getColumnIndexOrThrow(_cursor, "openPortsJson");
          final int _cursorIndexOfLatencyMs = CursorUtil.getColumnIndexOrThrow(_cursor, "latencyMs");
          final int _cursorIndexOfMacAddress = CursorUtil.getColumnIndexOrThrow(_cursor, "macAddress");
          final int _cursorIndexOfDeviceType = CursorUtil.getColumnIndexOrThrow(_cursor, "deviceType");
          final int _cursorIndexOfAlias = CursorUtil.getColumnIndexOrThrow(_cursor, "alias");
          final int _cursorIndexOfTransport = CursorUtil.getColumnIndexOrThrow(_cursor, "transport");
          final int _cursorIndexOfSecurity = CursorUtil.getColumnIndexOrThrow(_cursor, "security");
          final int _cursorIndexOfSignalLevelDbm = CursorUtil.getColumnIndexOrThrow(_cursor, "signalLevelDbm");
          final int _cursorIndexOfNote = CursorUtil.getColumnIndexOrThrow(_cursor, "note");
          final int _cursorIndexOfFirstSeenAt = CursorUtil.getColumnIndexOrThrow(_cursor, "firstSeenAt");
          final int _cursorIndexOfLastSeenAt = CursorUtil.getColumnIndexOrThrow(_cursor, "lastSeenAt");
          final int _cursorIndexOfSeenCount = CursorUtil.getColumnIndexOrThrow(_cursor, "seenCount");
          final List<NodeEntity> _result = new ArrayList<NodeEntity>(_cursor.getCount());
          while (_cursor.moveToNext()) {
            final NodeEntity _item;
            final String _tmpNodeId;
            _tmpNodeId = _cursor.getString(_cursorIndexOfNodeId);
            final String _tmpIp;
            _tmpIp = _cursor.getString(_cursorIndexOfIp);
            final String _tmpHostname;
            if (_cursor.isNull(_cursorIndexOfHostname)) {
              _tmpHostname = null;
            } else {
              _tmpHostname = _cursor.getString(_cursorIndexOfHostname);
            }
            final String _tmpStatus;
            _tmpStatus = _cursor.getString(_cursorIndexOfStatus);
            final String _tmpOpenPortsJson;
            _tmpOpenPortsJson = _cursor.getString(_cursorIndexOfOpenPortsJson);
            final double _tmpLatencyMs;
            _tmpLatencyMs = _cursor.getDouble(_cursorIndexOfLatencyMs);
            final String _tmpMacAddress;
            if (_cursor.isNull(_cursorIndexOfMacAddress)) {
              _tmpMacAddress = null;
            } else {
              _tmpMacAddress = _cursor.getString(_cursorIndexOfMacAddress);
            }
            final String _tmpDeviceType;
            if (_cursor.isNull(_cursorIndexOfDeviceType)) {
              _tmpDeviceType = null;
            } else {
              _tmpDeviceType = _cursor.getString(_cursorIndexOfDeviceType);
            }
            final String _tmpAlias;
            if (_cursor.isNull(_cursorIndexOfAlias)) {
              _tmpAlias = null;
            } else {
              _tmpAlias = _cursor.getString(_cursorIndexOfAlias);
            }
            final String _tmpTransport;
            _tmpTransport = _cursor.getString(_cursorIndexOfTransport);
            final String _tmpSecurity;
            if (_cursor.isNull(_cursorIndexOfSecurity)) {
              _tmpSecurity = null;
            } else {
              _tmpSecurity = _cursor.getString(_cursorIndexOfSecurity);
            }
            final Integer _tmpSignalLevelDbm;
            if (_cursor.isNull(_cursorIndexOfSignalLevelDbm)) {
              _tmpSignalLevelDbm = null;
            } else {
              _tmpSignalLevelDbm = _cursor.getInt(_cursorIndexOfSignalLevelDbm);
            }
            final String _tmpNote;
            if (_cursor.isNull(_cursorIndexOfNote)) {
              _tmpNote = null;
            } else {
              _tmpNote = _cursor.getString(_cursorIndexOfNote);
            }
            final long _tmpFirstSeenAt;
            _tmpFirstSeenAt = _cursor.getLong(_cursorIndexOfFirstSeenAt);
            final long _tmpLastSeenAt;
            _tmpLastSeenAt = _cursor.getLong(_cursorIndexOfLastSeenAt);
            final int _tmpSeenCount;
            _tmpSeenCount = _cursor.getInt(_cursorIndexOfSeenCount);
            _item = new NodeEntity(_tmpNodeId,_tmpIp,_tmpHostname,_tmpStatus,_tmpOpenPortsJson,_tmpLatencyMs,_tmpMacAddress,_tmpDeviceType,_tmpAlias,_tmpTransport,_tmpSecurity,_tmpSignalLevelDbm,_tmpNote,_tmpFirstSeenAt,_tmpLastSeenAt,_tmpSeenCount);
            _result.add(_item);
          }
          return _result;
        } finally {
          _cursor.close();
        }
      }

      @Override
      protected void finalize() {
        _statement.release();
      }
    });
  }

  @Override
  public Object markMissing(final List<String> activeIds,
      final Continuation<? super Unit> $completion) {
    return CoroutinesRoom.execute(__db, true, new Callable<Unit>() {
      @Override
      @NonNull
      public Unit call() throws Exception {
        final StringBuilder _stringBuilder = StringUtil.newStringBuilder();
        _stringBuilder.append("UPDATE nodes SET status = 'STALE' WHERE nodeId NOT IN (");
        final int _inputSize = activeIds.size();
        StringUtil.appendPlaceholders(_stringBuilder, _inputSize);
        _stringBuilder.append(")");
        final String _sql = _stringBuilder.toString();
        final SupportSQLiteStatement _stmt = __db.compileStatement(_sql);
        int _argIndex = 1;
        for (String _item : activeIds) {
          _stmt.bindString(_argIndex, _item);
          _argIndex++;
        }
        __db.beginTransaction();
        try {
          _stmt.executeUpdateDelete();
          __db.setTransactionSuccessful();
          return Unit.INSTANCE;
        } finally {
          __db.endTransaction();
        }
      }
    }, $completion);
  }

  @NonNull
  public static List<Class<?>> getRequiredConverters() {
    return Collections.emptyList();
  }
}
