package net.letime.lecjoa.db;

import android.arch.persistence.db.SupportSQLiteStatement;
import android.arch.persistence.room.EntityInsertionAdapter;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.RoomSQLiteQuery;
import android.arch.persistence.room.SharedSQLiteStatement;
import android.database.Cursor;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;
import java.util.ArrayList;
import java.util.List;
import net.letime.lecjoa.entity.Status;

@SuppressWarnings("unchecked")
public class TootDao_Impl implements TootDao {
  private final RoomDatabase __db;

  private final EntityInsertionAdapter __insertionAdapterOfTootEntity;

  private final TootEntity.Converters __converters = new TootEntity.Converters();

  private final SharedSQLiteStatement __preparedStmtOfDelete;

  public TootDao_Impl(RoomDatabase __db) {
    this.__db = __db;
    this.__insertionAdapterOfTootEntity = new EntityInsertionAdapter<TootEntity>(__db) {
      @Override
      public String createQuery() {
        return "INSERT OR REPLACE INTO `TootEntity`(`uid`,`text`,`urls`,`contentWarning`,`inReplyToId`,`inReplyToText`,`inReplyToUsername`,`visibility`) VALUES (nullif(?, 0),?,?,?,?,?,?,?)";
      }

      @Override
      public void bind(SupportSQLiteStatement stmt, TootEntity value) {
        stmt.bindLong(1, value.getUid());
        if (value.getText() == null) {
          stmt.bindNull(2);
        } else {
          stmt.bindString(2, value.getText());
        }
        if (value.getUrls() == null) {
          stmt.bindNull(3);
        } else {
          stmt.bindString(3, value.getUrls());
        }
        if (value.getContentWarning() == null) {
          stmt.bindNull(4);
        } else {
          stmt.bindString(4, value.getContentWarning());
        }
        if (value.getInReplyToId() == null) {
          stmt.bindNull(5);
        } else {
          stmt.bindString(5, value.getInReplyToId());
        }
        if (value.getInReplyToText() == null) {
          stmt.bindNull(6);
        } else {
          stmt.bindString(6, value.getInReplyToText());
        }
        if (value.getInReplyToUsername() == null) {
          stmt.bindNull(7);
        } else {
          stmt.bindString(7, value.getInReplyToUsername());
        }
        final int _tmp;
        _tmp = __converters.intToVisibility(value.getVisibility());
        stmt.bindLong(8, _tmp);
      }
    };
    this.__preparedStmtOfDelete = new SharedSQLiteStatement(__db) {
      @Override
      public String createQuery() {
        final String _query = "DELETE FROM TootEntity WHERE uid = ?";
        return _query;
      }
    };
  }

  @Override
  public void insertOrReplace(TootEntity users) {
    __db.beginTransaction();
    try {
      __insertionAdapterOfTootEntity.insert(users);
      __db.setTransactionSuccessful();
    } finally {
      __db.endTransaction();
    }
  }

  @Override
  public int delete(int uid) {
    final SupportSQLiteStatement _stmt = __preparedStmtOfDelete.acquire();
    __db.beginTransaction();
    try {
      int _argIndex = 1;
      _stmt.bindLong(_argIndex, uid);
      final int _result = _stmt.executeUpdateDelete();
      __db.setTransactionSuccessful();
      return _result;
    } finally {
      __db.endTransaction();
      __preparedStmtOfDelete.release(_stmt);
    }
  }

  @Override
  public List<TootEntity> loadAll() {
    final String _sql = "SELECT * FROM TootEntity ORDER BY uid DESC";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    final Cursor _cursor = __db.query(_statement);
    try {
      final int _cursorIndexOfUid = _cursor.getColumnIndexOrThrow("uid");
      final int _cursorIndexOfText = _cursor.getColumnIndexOrThrow("text");
      final int _cursorIndexOfUrls = _cursor.getColumnIndexOrThrow("urls");
      final int _cursorIndexOfContentWarning = _cursor.getColumnIndexOrThrow("contentWarning");
      final int _cursorIndexOfInReplyToId = _cursor.getColumnIndexOrThrow("inReplyToId");
      final int _cursorIndexOfInReplyToText = _cursor.getColumnIndexOrThrow("inReplyToText");
      final int _cursorIndexOfInReplyToUsername = _cursor.getColumnIndexOrThrow("inReplyToUsername");
      final int _cursorIndexOfVisibility = _cursor.getColumnIndexOrThrow("visibility");
      final List<TootEntity> _result = new ArrayList<TootEntity>(_cursor.getCount());
      while(_cursor.moveToNext()) {
        final TootEntity _item;
        final int _tmpUid;
        _tmpUid = _cursor.getInt(_cursorIndexOfUid);
        final String _tmpText;
        _tmpText = _cursor.getString(_cursorIndexOfText);
        final String _tmpUrls;
        _tmpUrls = _cursor.getString(_cursorIndexOfUrls);
        final String _tmpContentWarning;
        _tmpContentWarning = _cursor.getString(_cursorIndexOfContentWarning);
        final String _tmpInReplyToId;
        _tmpInReplyToId = _cursor.getString(_cursorIndexOfInReplyToId);
        final String _tmpInReplyToText;
        _tmpInReplyToText = _cursor.getString(_cursorIndexOfInReplyToText);
        final String _tmpInReplyToUsername;
        _tmpInReplyToUsername = _cursor.getString(_cursorIndexOfInReplyToUsername);
        final Status.Visibility _tmpVisibility;
        final int _tmp;
        _tmp = _cursor.getInt(_cursorIndexOfVisibility);
        _tmpVisibility = __converters.visibilityFromInt(_tmp);
        _item = new TootEntity(_tmpUid,_tmpText,_tmpUrls,_tmpContentWarning,_tmpInReplyToId,_tmpInReplyToText,_tmpInReplyToUsername,_tmpVisibility);
        _result.add(_item);
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }

  @Override
  public TootEntity find(int uid) {
    final String _sql = "SELECT * FROM TootEntity WHERE uid = ?";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    _statement.bindLong(_argIndex, uid);
    final Cursor _cursor = __db.query(_statement);
    try {
      final int _cursorIndexOfUid = _cursor.getColumnIndexOrThrow("uid");
      final int _cursorIndexOfText = _cursor.getColumnIndexOrThrow("text");
      final int _cursorIndexOfUrls = _cursor.getColumnIndexOrThrow("urls");
      final int _cursorIndexOfContentWarning = _cursor.getColumnIndexOrThrow("contentWarning");
      final int _cursorIndexOfInReplyToId = _cursor.getColumnIndexOrThrow("inReplyToId");
      final int _cursorIndexOfInReplyToText = _cursor.getColumnIndexOrThrow("inReplyToText");
      final int _cursorIndexOfInReplyToUsername = _cursor.getColumnIndexOrThrow("inReplyToUsername");
      final int _cursorIndexOfVisibility = _cursor.getColumnIndexOrThrow("visibility");
      final TootEntity _result;
      if(_cursor.moveToFirst()) {
        final int _tmpUid;
        _tmpUid = _cursor.getInt(_cursorIndexOfUid);
        final String _tmpText;
        _tmpText = _cursor.getString(_cursorIndexOfText);
        final String _tmpUrls;
        _tmpUrls = _cursor.getString(_cursorIndexOfUrls);
        final String _tmpContentWarning;
        _tmpContentWarning = _cursor.getString(_cursorIndexOfContentWarning);
        final String _tmpInReplyToId;
        _tmpInReplyToId = _cursor.getString(_cursorIndexOfInReplyToId);
        final String _tmpInReplyToText;
        _tmpInReplyToText = _cursor.getString(_cursorIndexOfInReplyToText);
        final String _tmpInReplyToUsername;
        _tmpInReplyToUsername = _cursor.getString(_cursorIndexOfInReplyToUsername);
        final Status.Visibility _tmpVisibility;
        final int _tmp;
        _tmp = _cursor.getInt(_cursorIndexOfVisibility);
        _tmpVisibility = __converters.visibilityFromInt(_tmp);
        _result = new TootEntity(_tmpUid,_tmpText,_tmpUrls,_tmpContentWarning,_tmpInReplyToId,_tmpInReplyToText,_tmpInReplyToUsername,_tmpVisibility);
      } else {
        _result = null;
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }
}
