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.database.Cursor;
import java.lang.Integer;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;
import java.util.List;
import net.letime.lecjoa.entity.Emoji;

@SuppressWarnings("unchecked")
public class InstanceDao_Impl implements InstanceDao {
  private final RoomDatabase __db;

  private final EntityInsertionAdapter __insertionAdapterOfInstanceEntity;

  private final Converters __converters = new Converters();

  public InstanceDao_Impl(RoomDatabase __db) {
    this.__db = __db;
    this.__insertionAdapterOfInstanceEntity = new EntityInsertionAdapter<InstanceEntity>(__db) {
      @Override
      public String createQuery() {
        return "INSERT OR REPLACE INTO `InstanceEntity`(`instance`,`emojiList`,`maximumTootCharacters`) VALUES (?,?,?)";
      }

      @Override
      public void bind(SupportSQLiteStatement stmt, InstanceEntity value) {
        if (value.getInstance() == null) {
          stmt.bindNull(1);
        } else {
          stmt.bindString(1, value.getInstance());
        }
        final String _tmp;
        _tmp = __converters.listToJson(value.getEmojiList());
        if (_tmp == null) {
          stmt.bindNull(2);
        } else {
          stmt.bindString(2, _tmp);
        }
        if (value.getMaximumTootCharacters() == null) {
          stmt.bindNull(3);
        } else {
          stmt.bindLong(3, value.getMaximumTootCharacters());
        }
      }
    };
  }

  @Override
  public void insertOrReplace(InstanceEntity instance) {
    __db.beginTransaction();
    try {
      __insertionAdapterOfInstanceEntity.insert(instance);
      __db.setTransactionSuccessful();
    } finally {
      __db.endTransaction();
    }
  }

  @Override
  public InstanceEntity loadMetadataForInstance(String instance) {
    final String _sql = "SELECT * FROM InstanceEntity WHERE instance = ? LIMIT 1";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    if (instance == null) {
      _statement.bindNull(_argIndex);
    } else {
      _statement.bindString(_argIndex, instance);
    }
    final Cursor _cursor = __db.query(_statement);
    try {
      final int _cursorIndexOfInstance = _cursor.getColumnIndexOrThrow("instance");
      final int _cursorIndexOfEmojiList = _cursor.getColumnIndexOrThrow("emojiList");
      final int _cursorIndexOfMaximumTootCharacters = _cursor.getColumnIndexOrThrow("maximumTootCharacters");
      final InstanceEntity _result;
      if(_cursor.moveToFirst()) {
        final String _tmpInstance;
        _tmpInstance = _cursor.getString(_cursorIndexOfInstance);
        final List<Emoji> _tmpEmojiList;
        final String _tmp;
        _tmp = _cursor.getString(_cursorIndexOfEmojiList);
        _tmpEmojiList = __converters.jsonToList(_tmp);
        final Integer _tmpMaximumTootCharacters;
        if (_cursor.isNull(_cursorIndexOfMaximumTootCharacters)) {
          _tmpMaximumTootCharacters = null;
        } else {
          _tmpMaximumTootCharacters = _cursor.getInt(_cursorIndexOfMaximumTootCharacters);
        }
        _result = new InstanceEntity(_tmpInstance,_tmpEmojiList,_tmpMaximumTootCharacters);
      } else {
        _result = null;
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }
}
