package com.parkplants.data.db.dao;

import android.database.Cursor;
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.SharedSQLiteStatement;
import androidx.room.util.CursorUtil;
import androidx.room.util.DBUtil;
import androidx.sqlite.db.SupportSQLiteStatement;
import com.parkplants.data.db.entities.PendingPlant;
import java.lang.Class;
import java.lang.Exception;
import java.lang.Integer;
import java.lang.Long;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
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 PendingPlantDao_Impl implements PendingPlantDao {
  private final RoomDatabase __db;

  private final EntityInsertionAdapter<PendingPlant> __insertionAdapterOfPendingPlant;

  private final SharedSQLiteStatement __preparedStmtOfMarkSynced;

  private final SharedSQLiteStatement __preparedStmtOfMarkFailed;

  private final SharedSQLiteStatement __preparedStmtOfUpdatePhotoUrl;

  private final SharedSQLiteStatement __preparedStmtOfDeleteSynced;

  public PendingPlantDao_Impl(@NonNull final RoomDatabase __db) {
    this.__db = __db;
    this.__insertionAdapterOfPendingPlant = new EntityInsertionAdapter<PendingPlant>(__db) {
      @Override
      @NonNull
      protected String createQuery() {
        return "INSERT OR ABORT INTO `pending_plants` (`id`,`plantTypeId`,`plantTypeName`,`lat`,`lng`,`plantedAt`,`nursery`,`localPhotoPath`,`photoUrl`,`createdAt`,`synced`,`syncError`) VALUES (nullif(?, 0),?,?,?,?,?,?,?,?,?,?,?)";
      }

      @Override
      protected void bind(@NonNull final SupportSQLiteStatement statement,
          @NonNull final PendingPlant entity) {
        statement.bindLong(1, entity.getId());
        statement.bindLong(2, entity.getPlantTypeId());
        statement.bindString(3, entity.getPlantTypeName());
        statement.bindDouble(4, entity.getLat());
        statement.bindDouble(5, entity.getLng());
        if (entity.getPlantedAt() == null) {
          statement.bindNull(6);
        } else {
          statement.bindString(6, entity.getPlantedAt());
        }
        if (entity.getNursery() == null) {
          statement.bindNull(7);
        } else {
          statement.bindString(7, entity.getNursery());
        }
        if (entity.getLocalPhotoPath() == null) {
          statement.bindNull(8);
        } else {
          statement.bindString(8, entity.getLocalPhotoPath());
        }
        if (entity.getPhotoUrl() == null) {
          statement.bindNull(9);
        } else {
          statement.bindString(9, entity.getPhotoUrl());
        }
        statement.bindLong(10, entity.getCreatedAt());
        final int _tmp = entity.getSynced() ? 1 : 0;
        statement.bindLong(11, _tmp);
        if (entity.getSyncError() == null) {
          statement.bindNull(12);
        } else {
          statement.bindString(12, entity.getSyncError());
        }
      }
    };
    this.__preparedStmtOfMarkSynced = new SharedSQLiteStatement(__db) {
      @Override
      @NonNull
      public String createQuery() {
        final String _query = "UPDATE pending_plants SET synced = 1, syncError = NULL WHERE id = ?";
        return _query;
      }
    };
    this.__preparedStmtOfMarkFailed = new SharedSQLiteStatement(__db) {
      @Override
      @NonNull
      public String createQuery() {
        final String _query = "UPDATE pending_plants SET syncError = ? WHERE id = ?";
        return _query;
      }
    };
    this.__preparedStmtOfUpdatePhotoUrl = new SharedSQLiteStatement(__db) {
      @Override
      @NonNull
      public String createQuery() {
        final String _query = "UPDATE pending_plants SET photoUrl = ? WHERE id = ?";
        return _query;
      }
    };
    this.__preparedStmtOfDeleteSynced = new SharedSQLiteStatement(__db) {
      @Override
      @NonNull
      public String createQuery() {
        final String _query = "DELETE FROM pending_plants WHERE synced = 1";
        return _query;
      }
    };
  }

  @Override
  public Object insert(final PendingPlant plant, final Continuation<? super Long> $completion) {
    return CoroutinesRoom.execute(__db, true, new Callable<Long>() {
      @Override
      @NonNull
      public Long call() throws Exception {
        __db.beginTransaction();
        try {
          final Long _result = __insertionAdapterOfPendingPlant.insertAndReturnId(plant);
          __db.setTransactionSuccessful();
          return _result;
        } finally {
          __db.endTransaction();
        }
      }
    }, $completion);
  }

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

  @Override
  public Object markFailed(final long id, final String error,
      final Continuation<? super Unit> $completion) {
    return CoroutinesRoom.execute(__db, true, new Callable<Unit>() {
      @Override
      @NonNull
      public Unit call() throws Exception {
        final SupportSQLiteStatement _stmt = __preparedStmtOfMarkFailed.acquire();
        int _argIndex = 1;
        _stmt.bindString(_argIndex, error);
        _argIndex = 2;
        _stmt.bindLong(_argIndex, id);
        try {
          __db.beginTransaction();
          try {
            _stmt.executeUpdateDelete();
            __db.setTransactionSuccessful();
            return Unit.INSTANCE;
          } finally {
            __db.endTransaction();
          }
        } finally {
          __preparedStmtOfMarkFailed.release(_stmt);
        }
      }
    }, $completion);
  }

  @Override
  public Object updatePhotoUrl(final long id, final String url,
      final Continuation<? super Unit> $completion) {
    return CoroutinesRoom.execute(__db, true, new Callable<Unit>() {
      @Override
      @NonNull
      public Unit call() throws Exception {
        final SupportSQLiteStatement _stmt = __preparedStmtOfUpdatePhotoUrl.acquire();
        int _argIndex = 1;
        _stmt.bindString(_argIndex, url);
        _argIndex = 2;
        _stmt.bindLong(_argIndex, id);
        try {
          __db.beginTransaction();
          try {
            _stmt.executeUpdateDelete();
            __db.setTransactionSuccessful();
            return Unit.INSTANCE;
          } finally {
            __db.endTransaction();
          }
        } finally {
          __preparedStmtOfUpdatePhotoUrl.release(_stmt);
        }
      }
    }, $completion);
  }

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

  @Override
  public Object getUnsynced(final Continuation<? super List<PendingPlant>> $completion) {
    final String _sql = "SELECT * FROM pending_plants WHERE synced = 0 ORDER BY createdAt ASC";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    final CancellationSignal _cancellationSignal = DBUtil.createCancellationSignal();
    return CoroutinesRoom.execute(__db, false, _cancellationSignal, new Callable<List<PendingPlant>>() {
      @Override
      @NonNull
      public List<PendingPlant> call() throws Exception {
        final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
        try {
          final int _cursorIndexOfId = CursorUtil.getColumnIndexOrThrow(_cursor, "id");
          final int _cursorIndexOfPlantTypeId = CursorUtil.getColumnIndexOrThrow(_cursor, "plantTypeId");
          final int _cursorIndexOfPlantTypeName = CursorUtil.getColumnIndexOrThrow(_cursor, "plantTypeName");
          final int _cursorIndexOfLat = CursorUtil.getColumnIndexOrThrow(_cursor, "lat");
          final int _cursorIndexOfLng = CursorUtil.getColumnIndexOrThrow(_cursor, "lng");
          final int _cursorIndexOfPlantedAt = CursorUtil.getColumnIndexOrThrow(_cursor, "plantedAt");
          final int _cursorIndexOfNursery = CursorUtil.getColumnIndexOrThrow(_cursor, "nursery");
          final int _cursorIndexOfLocalPhotoPath = CursorUtil.getColumnIndexOrThrow(_cursor, "localPhotoPath");
          final int _cursorIndexOfPhotoUrl = CursorUtil.getColumnIndexOrThrow(_cursor, "photoUrl");
          final int _cursorIndexOfCreatedAt = CursorUtil.getColumnIndexOrThrow(_cursor, "createdAt");
          final int _cursorIndexOfSynced = CursorUtil.getColumnIndexOrThrow(_cursor, "synced");
          final int _cursorIndexOfSyncError = CursorUtil.getColumnIndexOrThrow(_cursor, "syncError");
          final List<PendingPlant> _result = new ArrayList<PendingPlant>(_cursor.getCount());
          while (_cursor.moveToNext()) {
            final PendingPlant _item;
            final long _tmpId;
            _tmpId = _cursor.getLong(_cursorIndexOfId);
            final int _tmpPlantTypeId;
            _tmpPlantTypeId = _cursor.getInt(_cursorIndexOfPlantTypeId);
            final String _tmpPlantTypeName;
            _tmpPlantTypeName = _cursor.getString(_cursorIndexOfPlantTypeName);
            final double _tmpLat;
            _tmpLat = _cursor.getDouble(_cursorIndexOfLat);
            final double _tmpLng;
            _tmpLng = _cursor.getDouble(_cursorIndexOfLng);
            final String _tmpPlantedAt;
            if (_cursor.isNull(_cursorIndexOfPlantedAt)) {
              _tmpPlantedAt = null;
            } else {
              _tmpPlantedAt = _cursor.getString(_cursorIndexOfPlantedAt);
            }
            final String _tmpNursery;
            if (_cursor.isNull(_cursorIndexOfNursery)) {
              _tmpNursery = null;
            } else {
              _tmpNursery = _cursor.getString(_cursorIndexOfNursery);
            }
            final String _tmpLocalPhotoPath;
            if (_cursor.isNull(_cursorIndexOfLocalPhotoPath)) {
              _tmpLocalPhotoPath = null;
            } else {
              _tmpLocalPhotoPath = _cursor.getString(_cursorIndexOfLocalPhotoPath);
            }
            final String _tmpPhotoUrl;
            if (_cursor.isNull(_cursorIndexOfPhotoUrl)) {
              _tmpPhotoUrl = null;
            } else {
              _tmpPhotoUrl = _cursor.getString(_cursorIndexOfPhotoUrl);
            }
            final long _tmpCreatedAt;
            _tmpCreatedAt = _cursor.getLong(_cursorIndexOfCreatedAt);
            final boolean _tmpSynced;
            final int _tmp;
            _tmp = _cursor.getInt(_cursorIndexOfSynced);
            _tmpSynced = _tmp != 0;
            final String _tmpSyncError;
            if (_cursor.isNull(_cursorIndexOfSyncError)) {
              _tmpSyncError = null;
            } else {
              _tmpSyncError = _cursor.getString(_cursorIndexOfSyncError);
            }
            _item = new PendingPlant(_tmpId,_tmpPlantTypeId,_tmpPlantTypeName,_tmpLat,_tmpLng,_tmpPlantedAt,_tmpNursery,_tmpLocalPhotoPath,_tmpPhotoUrl,_tmpCreatedAt,_tmpSynced,_tmpSyncError);
            _result.add(_item);
          }
          return _result;
        } finally {
          _cursor.close();
          _statement.release();
        }
      }
    }, $completion);
  }

  @Override
  public Flow<List<PendingPlant>> observeAll() {
    final String _sql = "SELECT * FROM pending_plants ORDER BY createdAt DESC";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    return CoroutinesRoom.createFlow(__db, false, new String[] {"pending_plants"}, new Callable<List<PendingPlant>>() {
      @Override
      @NonNull
      public List<PendingPlant> call() throws Exception {
        final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
        try {
          final int _cursorIndexOfId = CursorUtil.getColumnIndexOrThrow(_cursor, "id");
          final int _cursorIndexOfPlantTypeId = CursorUtil.getColumnIndexOrThrow(_cursor, "plantTypeId");
          final int _cursorIndexOfPlantTypeName = CursorUtil.getColumnIndexOrThrow(_cursor, "plantTypeName");
          final int _cursorIndexOfLat = CursorUtil.getColumnIndexOrThrow(_cursor, "lat");
          final int _cursorIndexOfLng = CursorUtil.getColumnIndexOrThrow(_cursor, "lng");
          final int _cursorIndexOfPlantedAt = CursorUtil.getColumnIndexOrThrow(_cursor, "plantedAt");
          final int _cursorIndexOfNursery = CursorUtil.getColumnIndexOrThrow(_cursor, "nursery");
          final int _cursorIndexOfLocalPhotoPath = CursorUtil.getColumnIndexOrThrow(_cursor, "localPhotoPath");
          final int _cursorIndexOfPhotoUrl = CursorUtil.getColumnIndexOrThrow(_cursor, "photoUrl");
          final int _cursorIndexOfCreatedAt = CursorUtil.getColumnIndexOrThrow(_cursor, "createdAt");
          final int _cursorIndexOfSynced = CursorUtil.getColumnIndexOrThrow(_cursor, "synced");
          final int _cursorIndexOfSyncError = CursorUtil.getColumnIndexOrThrow(_cursor, "syncError");
          final List<PendingPlant> _result = new ArrayList<PendingPlant>(_cursor.getCount());
          while (_cursor.moveToNext()) {
            final PendingPlant _item;
            final long _tmpId;
            _tmpId = _cursor.getLong(_cursorIndexOfId);
            final int _tmpPlantTypeId;
            _tmpPlantTypeId = _cursor.getInt(_cursorIndexOfPlantTypeId);
            final String _tmpPlantTypeName;
            _tmpPlantTypeName = _cursor.getString(_cursorIndexOfPlantTypeName);
            final double _tmpLat;
            _tmpLat = _cursor.getDouble(_cursorIndexOfLat);
            final double _tmpLng;
            _tmpLng = _cursor.getDouble(_cursorIndexOfLng);
            final String _tmpPlantedAt;
            if (_cursor.isNull(_cursorIndexOfPlantedAt)) {
              _tmpPlantedAt = null;
            } else {
              _tmpPlantedAt = _cursor.getString(_cursorIndexOfPlantedAt);
            }
            final String _tmpNursery;
            if (_cursor.isNull(_cursorIndexOfNursery)) {
              _tmpNursery = null;
            } else {
              _tmpNursery = _cursor.getString(_cursorIndexOfNursery);
            }
            final String _tmpLocalPhotoPath;
            if (_cursor.isNull(_cursorIndexOfLocalPhotoPath)) {
              _tmpLocalPhotoPath = null;
            } else {
              _tmpLocalPhotoPath = _cursor.getString(_cursorIndexOfLocalPhotoPath);
            }
            final String _tmpPhotoUrl;
            if (_cursor.isNull(_cursorIndexOfPhotoUrl)) {
              _tmpPhotoUrl = null;
            } else {
              _tmpPhotoUrl = _cursor.getString(_cursorIndexOfPhotoUrl);
            }
            final long _tmpCreatedAt;
            _tmpCreatedAt = _cursor.getLong(_cursorIndexOfCreatedAt);
            final boolean _tmpSynced;
            final int _tmp;
            _tmp = _cursor.getInt(_cursorIndexOfSynced);
            _tmpSynced = _tmp != 0;
            final String _tmpSyncError;
            if (_cursor.isNull(_cursorIndexOfSyncError)) {
              _tmpSyncError = null;
            } else {
              _tmpSyncError = _cursor.getString(_cursorIndexOfSyncError);
            }
            _item = new PendingPlant(_tmpId,_tmpPlantTypeId,_tmpPlantTypeName,_tmpLat,_tmpLng,_tmpPlantedAt,_tmpNursery,_tmpLocalPhotoPath,_tmpPhotoUrl,_tmpCreatedAt,_tmpSynced,_tmpSyncError);
            _result.add(_item);
          }
          return _result;
        } finally {
          _cursor.close();
        }
      }

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

  @Override
  public Flow<Integer> observeUnsyncedCount() {
    final String _sql = "SELECT COUNT(*) FROM pending_plants WHERE synced = 0";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    return CoroutinesRoom.createFlow(__db, false, new String[] {"pending_plants"}, new Callable<Integer>() {
      @Override
      @NonNull
      public Integer call() throws Exception {
        final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
        try {
          final Integer _result;
          if (_cursor.moveToFirst()) {
            final int _tmp;
            _tmp = _cursor.getInt(0);
            _result = _tmp;
          } else {
            _result = 0;
          }
          return _result;
        } finally {
          _cursor.close();
        }
      }

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

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