package hanson.xyz.vpnhotspotmod.room;

import android.database.Cursor;
import android.net.MacAddress;
import android.os.CancellationSignal;
import androidx.annotation.NonNull;
import androidx.room.CoroutinesRoom;
import androidx.room.EntityInsertionAdapter;
import androidx.room.RoomDatabase;
import androidx.room.RoomSQLiteQuery;
import androidx.room.util.DBUtil;
import androidx.sqlite.db.SupportSQLiteStatement;
import java.lang.Class;
import java.lang.Exception;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import javax.annotation.processing.Generated;
import kotlin.coroutines.Continuation;

@Generated("androidx.room.RoomProcessor")
@SuppressWarnings({"unchecked", "deprecation"})
public final class TrafficRecord_Dao_Impl extends TrafficRecord.Dao {
  private final RoomDatabase __db;

  private final EntityInsertionAdapter<TrafficRecord> __insertionAdapterOfTrafficRecord;

  public TrafficRecord_Dao_Impl(@NonNull final RoomDatabase __db) {
    this.__db = __db;
    this.__insertionAdapterOfTrafficRecord = new EntityInsertionAdapter<TrafficRecord>(__db) {
      @Override
      @NonNull
      protected String createQuery() {
        return "INSERT OR ABORT INTO `TrafficRecord` (`id`,`timestamp`,`mac`,`ip`,`upstream`,`downstream`,`sentPackets`,`sentBytes`,`receivedPackets`,`receivedBytes`,`previousId`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
      }

      @Override
      protected void bind(@NonNull final SupportSQLiteStatement statement,
          @NonNull final TrafficRecord entity) {
        if (entity.getId() == null) {
          statement.bindNull(1);
        } else {
          statement.bindLong(1, entity.getId());
        }
        statement.bindLong(2, entity.getTimestamp());
        final long _tmp = Converters.INSTANCE.persistMacAddress(entity.getMac());
        statement.bindLong(3, _tmp);
        final byte[] _tmp_1 = Converters.INSTANCE.persistInetAddress(entity.getIp());
        if (_tmp_1 == null) {
          statement.bindNull(4);
        } else {
          statement.bindBlob(4, _tmp_1);
        }
        if (entity.getUpstream() == null) {
          statement.bindNull(5);
        } else {
          statement.bindString(5, entity.getUpstream());
        }
        if (entity.getDownstream() == null) {
          statement.bindNull(6);
        } else {
          statement.bindString(6, entity.getDownstream());
        }
        statement.bindLong(7, entity.getSentPackets());
        statement.bindLong(8, entity.getSentBytes());
        statement.bindLong(9, entity.getReceivedPackets());
        statement.bindLong(10, entity.getReceivedBytes());
        if (entity.getPreviousId() == null) {
          statement.bindNull(11);
        } else {
          statement.bindLong(11, entity.getPreviousId());
        }
      }
    };
  }

  @Override
  protected long insertInternal(final TrafficRecord value) {
    __db.assertNotSuspendingTransaction();
    __db.beginTransaction();
    try {
      final long _result = __insertionAdapterOfTrafficRecord.insertAndReturnId(value);
      __db.setTransactionSuccessful();
      return _result;
    } finally {
      __db.endTransaction();
    }
  }

  @Override
  public Object queryStats(final MacAddress mac,
      final Continuation<? super ClientStats> $completion) {
    final String _sql = "\n"
            + "            SELECT  MIN(TrafficRecord.timestamp) AS timestamp,\n"
            + "                    COUNT(TrafficRecord.id) AS count,\n"
            + "                    SUM(TrafficRecord.sentPackets) AS sentPackets,\n"
            + "                    SUM(TrafficRecord.sentBytes) AS sentBytes,\n"
            + "                    SUM(TrafficRecord.receivedPackets) AS receivedPackets,\n"
            + "                    SUM(TrafficRecord.receivedBytes) AS receivedBytes\n"
            + "                FROM TrafficRecord LEFT JOIN TrafficRecord AS Next ON TrafficRecord.id = Next.previousId\n"
            + "                /* We only want to find the last record for each chain so that we don't double count */\n"
            + "                WHERE TrafficRecord.mac = ? AND Next.id IS NULL\n"
            + "                ";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    final long _tmp = Converters.INSTANCE.persistMacAddress(mac);
    _statement.bindLong(_argIndex, _tmp);
    final CancellationSignal _cancellationSignal = DBUtil.createCancellationSignal();
    return CoroutinesRoom.execute(__db, false, _cancellationSignal, new Callable<ClientStats>() {
      @Override
      @NonNull
      public ClientStats call() throws Exception {
        final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
        try {
          final int _cursorIndexOfTimestamp = 0;
          final int _cursorIndexOfCount = 1;
          final int _cursorIndexOfSentPackets = 2;
          final int _cursorIndexOfSentBytes = 3;
          final int _cursorIndexOfReceivedPackets = 4;
          final int _cursorIndexOfReceivedBytes = 5;
          final ClientStats _result;
          if (_cursor.moveToFirst()) {
            final long _tmpTimestamp;
            _tmpTimestamp = _cursor.getLong(_cursorIndexOfTimestamp);
            final long _tmpCount;
            _tmpCount = _cursor.getLong(_cursorIndexOfCount);
            final long _tmpSentPackets;
            _tmpSentPackets = _cursor.getLong(_cursorIndexOfSentPackets);
            final long _tmpSentBytes;
            _tmpSentBytes = _cursor.getLong(_cursorIndexOfSentBytes);
            final long _tmpReceivedPackets;
            _tmpReceivedPackets = _cursor.getLong(_cursorIndexOfReceivedPackets);
            final long _tmpReceivedBytes;
            _tmpReceivedBytes = _cursor.getLong(_cursorIndexOfReceivedBytes);
            _result = new ClientStats(_tmpTimestamp,_tmpCount,_tmpSentPackets,_tmpSentBytes,_tmpReceivedPackets,_tmpReceivedBytes);
          } else {
            _result = null;
          }
          return _result;
        } finally {
          _cursor.close();
          _statement.release();
        }
      }
    }, $completion);
  }

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