package com.maniu.lib_cache.dao;

import android.database.Cursor;
import androidx.annotation.NonNull;
import androidx.lifecycle.ComputableLiveData;
import androidx.lifecycle.LiveData;
import androidx.room.EntityDeletionOrUpdateAdapter;
import androidx.room.EntityInsertionAdapter;
import androidx.room.InvalidationTracker.Observer;
import androidx.room.RoomDatabase;
import androidx.room.RoomSQLiteQuery;
import androidx.room.util.StringUtil;
import androidx.sqlite.db.SupportSQLiteStatement;
import com.maniu.lib_cache.model.User;
import java.lang.Integer;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import java.lang.SuppressWarnings;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@SuppressWarnings("unchecked")
public final class UserDao_Impl implements UserDao {
  private final RoomDatabase __db;

  private final EntityInsertionAdapter __insertionAdapterOfUser;

  private final EntityDeletionOrUpdateAdapter __deletionAdapterOfUser;

  private final EntityDeletionOrUpdateAdapter __updateAdapterOfUser;

  public UserDao_Impl(RoomDatabase __db) {
    this.__db = __db;
    this.__insertionAdapterOfUser = new EntityInsertionAdapter<User>(__db) {
      @Override
      public String createQuery() {
        return "INSERT OR REPLACE INTO `tb_user`(`account`,`passWord`,`age`,`u_id`) VALUES (?,?,?,?)";
      }

      @Override
      public void bind(SupportSQLiteStatement stmt, User value) {
        if (value.getAccount() == null) {
          stmt.bindNull(1);
        } else {
          stmt.bindString(1, value.getAccount());
        }
        if (value.getPassWord() == null) {
          stmt.bindNull(2);
        } else {
          stmt.bindString(2, value.getPassWord());
        }
        if (value.getAge() == null) {
          stmt.bindNull(3);
        } else {
          stmt.bindLong(3, value.getAge());
        }
        if (value.getU_id() == null) {
          stmt.bindNull(4);
        } else {
          stmt.bindLong(4, value.getU_id());
        }
      }
    };
    this.__deletionAdapterOfUser = new EntityDeletionOrUpdateAdapter<User>(__db) {
      @Override
      public String createQuery() {
        return "DELETE FROM `tb_user` WHERE `u_id` = ?";
      }

      @Override
      public void bind(SupportSQLiteStatement stmt, User value) {
        if (value.getU_id() == null) {
          stmt.bindNull(1);
        } else {
          stmt.bindLong(1, value.getU_id());
        }
      }
    };
    this.__updateAdapterOfUser = new EntityDeletionOrUpdateAdapter<User>(__db) {
      @Override
      public String createQuery() {
        return "UPDATE OR ABORT `tb_user` SET `account` = ?,`passWord` = ?,`age` = ?,`u_id` = ? WHERE `u_id` = ?";
      }

      @Override
      public void bind(SupportSQLiteStatement stmt, User value) {
        if (value.getAccount() == null) {
          stmt.bindNull(1);
        } else {
          stmt.bindString(1, value.getAccount());
        }
        if (value.getPassWord() == null) {
          stmt.bindNull(2);
        } else {
          stmt.bindString(2, value.getPassWord());
        }
        if (value.getAge() == null) {
          stmt.bindNull(3);
        } else {
          stmt.bindLong(3, value.getAge());
        }
        if (value.getU_id() == null) {
          stmt.bindNull(4);
        } else {
          stmt.bindLong(4, value.getU_id());
        }
        if (value.getU_id() == null) {
          stmt.bindNull(5);
        } else {
          stmt.bindLong(5, value.getU_id());
        }
      }
    };
  }

  @Override
  public void insertAll(User... users) {
    __db.beginTransaction();
    try {
      __insertionAdapterOfUser.insert(users);
      __db.setTransactionSuccessful();
    } finally {
      __db.endTransaction();
    }
  }

  @Override
  public void delete(User user) {
    __db.beginTransaction();
    try {
      __deletionAdapterOfUser.handle(user);
      __db.setTransactionSuccessful();
    } finally {
      __db.endTransaction();
    }
  }

  @Override
  public void update(User user) {
    __db.beginTransaction();
    try {
      __updateAdapterOfUser.handle(user);
      __db.setTransactionSuccessful();
    } finally {
      __db.endTransaction();
    }
  }

  @Override
  public List<User> loadAllByIds(int[] userIds) {
    StringBuilder _stringBuilder = StringUtil.newStringBuilder();
    _stringBuilder.append("SELECT * FROM tb_user WHERE u_id IN (");
    final int _inputSize = userIds.length;
    StringUtil.appendPlaceholders(_stringBuilder, _inputSize);
    _stringBuilder.append(")");
    final String _sql = _stringBuilder.toString();
    final int _argCount = 0 + _inputSize;
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, _argCount);
    int _argIndex = 1;
    for (int _item : userIds) {
      _statement.bindLong(_argIndex, _item);
      _argIndex ++;
    }
    final Cursor _cursor = __db.query(_statement);
    try {
      final int _cursorIndexOfAccount = _cursor.getColumnIndexOrThrow("account");
      final int _cursorIndexOfPassWord = _cursor.getColumnIndexOrThrow("passWord");
      final int _cursorIndexOfAge = _cursor.getColumnIndexOrThrow("age");
      final int _cursorIndexOfUId = _cursor.getColumnIndexOrThrow("u_id");
      final List<User> _result = new ArrayList<User>(_cursor.getCount());
      while(_cursor.moveToNext()) {
        final User _item_1;
        final String _tmpAccount;
        _tmpAccount = _cursor.getString(_cursorIndexOfAccount);
        final String _tmpPassWord;
        _tmpPassWord = _cursor.getString(_cursorIndexOfPassWord);
        final Integer _tmpAge;
        if (_cursor.isNull(_cursorIndexOfAge)) {
          _tmpAge = null;
        } else {
          _tmpAge = _cursor.getInt(_cursorIndexOfAge);
        }
        final Integer _tmpU_id;
        if (_cursor.isNull(_cursorIndexOfUId)) {
          _tmpU_id = null;
        } else {
          _tmpU_id = _cursor.getInt(_cursorIndexOfUId);
        }
        _item_1 = new User(_tmpAccount,_tmpPassWord,_tmpAge,_tmpU_id);
        _result.add(_item_1);
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }

  @Override
  public LiveData<List<User>> getAll() {
    final String _sql = "SELECT * FROM tb_user";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    return new ComputableLiveData<List<User>>(__db.getQueryExecutor()) {
      private Observer _observer;

      @Override
      protected List<User> compute() {
        if (_observer == null) {
          _observer = new Observer("tb_user") {
            @Override
            public void onInvalidated(@NonNull Set<String> tables) {
              invalidate();
            }
          };
          __db.getInvalidationTracker().addWeakObserver(_observer);
        }
        final Cursor _cursor = __db.query(_statement);
        try {
          final int _cursorIndexOfAccount = _cursor.getColumnIndexOrThrow("account");
          final int _cursorIndexOfPassWord = _cursor.getColumnIndexOrThrow("passWord");
          final int _cursorIndexOfAge = _cursor.getColumnIndexOrThrow("age");
          final int _cursorIndexOfUId = _cursor.getColumnIndexOrThrow("u_id");
          final List<User> _result = new ArrayList<User>(_cursor.getCount());
          while(_cursor.moveToNext()) {
            final User _item;
            final String _tmpAccount;
            _tmpAccount = _cursor.getString(_cursorIndexOfAccount);
            final String _tmpPassWord;
            _tmpPassWord = _cursor.getString(_cursorIndexOfPassWord);
            final Integer _tmpAge;
            if (_cursor.isNull(_cursorIndexOfAge)) {
              _tmpAge = null;
            } else {
              _tmpAge = _cursor.getInt(_cursorIndexOfAge);
            }
            final Integer _tmpU_id;
            if (_cursor.isNull(_cursorIndexOfUId)) {
              _tmpU_id = null;
            } else {
              _tmpU_id = _cursor.getInt(_cursorIndexOfUId);
            }
            _item = new User(_tmpAccount,_tmpPassWord,_tmpAge,_tmpU_id);
            _result.add(_item);
          }
          return _result;
        } finally {
          _cursor.close();
        }
      }

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