package io.realm;


import android.util.JsonReader;
import android.util.JsonToken;
import com.kitowcy.incrediblephysicsritual.model.RealmQuestion;
import io.realm.RealmFieldType;
import io.realm.exceptions.RealmMigrationNeededException;
import io.realm.internal.ColumnInfo;
import io.realm.internal.ImplicitTransaction;
import io.realm.internal.LinkView;
import io.realm.internal.RealmObjectProxy;
import io.realm.internal.Table;
import io.realm.internal.TableOrView;
import io.realm.internal.android.JsonUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class RealmQuestionRealmProxy extends RealmQuestion
    implements RealmObjectProxy {

    static final class RealmQuestionColumnInfo extends ColumnInfo {

        public final long idIndex;
        public final long questionIndex;
        public final long answerIndex;

        RealmQuestionColumnInfo(String path, Table table) {
            final Map<String, Long> indicesMap = new HashMap<String, Long>(3);
            this.idIndex = getValidColumnIndex(path, table, "RealmQuestion", "id");
            indicesMap.put("id", this.idIndex);

            this.questionIndex = getValidColumnIndex(path, table, "RealmQuestion", "question");
            indicesMap.put("question", this.questionIndex);

            this.answerIndex = getValidColumnIndex(path, table, "RealmQuestion", "answer");
            indicesMap.put("answer", this.answerIndex);

            setIndicesMap(indicesMap);
        }
    }

    private final RealmQuestionColumnInfo columnInfo;
    private static final List<String> FIELD_NAMES;
    static {
        List<String> fieldNames = new ArrayList<String>();
        fieldNames.add("id");
        fieldNames.add("question");
        fieldNames.add("answer");
        FIELD_NAMES = Collections.unmodifiableList(fieldNames);
    }

    RealmQuestionRealmProxy(ColumnInfo columnInfo) {
        this.columnInfo = (RealmQuestionColumnInfo) columnInfo;
    }

    @Override
    @SuppressWarnings("cast")
    public int getId() {
        realm.checkIfValid();
        return (int) row.getLong(columnInfo.idIndex);
    }

    @Override
    public void setId(int value) {
        realm.checkIfValid();
        row.setLong(columnInfo.idIndex, value);
    }

    @Override
    @SuppressWarnings("cast")
    public String getQuestion() {
        realm.checkIfValid();
        return (java.lang.String) row.getString(columnInfo.questionIndex);
    }

    @Override
    public void setQuestion(String value) {
        realm.checkIfValid();
        if (value == null) {
            row.setNull(columnInfo.questionIndex);
            return;
        }
        row.setString(columnInfo.questionIndex, value);
    }

    @Override
    @SuppressWarnings("cast")
    public int getAnswer() {
        realm.checkIfValid();
        return (int) row.getLong(columnInfo.answerIndex);
    }

    @Override
    public void setAnswer(int value) {
        realm.checkIfValid();
        row.setLong(columnInfo.answerIndex, value);
    }

    public static Table initTable(ImplicitTransaction transaction) {
        if (!transaction.hasTable("class_RealmQuestion")) {
            Table table = transaction.getTable("class_RealmQuestion");
            table.addColumn(RealmFieldType.INTEGER, "id", Table.NOT_NULLABLE);
            table.addColumn(RealmFieldType.STRING, "question", Table.NULLABLE);
            table.addColumn(RealmFieldType.INTEGER, "answer", Table.NOT_NULLABLE);
            table.addSearchIndex(table.getColumnIndex("id"));
            table.setPrimaryKey("id");
            return table;
        }
        return transaction.getTable("class_RealmQuestion");
    }

    public static RealmQuestionColumnInfo validateTable(ImplicitTransaction transaction) {
        if (transaction.hasTable("class_RealmQuestion")) {
            Table table = transaction.getTable("class_RealmQuestion");
            if (table.getColumnCount() != 3) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field count does not match - expected 3 but was " + table.getColumnCount());
            }
            Map<String, RealmFieldType> columnTypes = new HashMap<String, RealmFieldType>();
            for (long i = 0; i < 3; i++) {
                columnTypes.put(table.getColumnName(i), table.getColumnType(i));
            }

            final RealmQuestionColumnInfo columnInfo = new RealmQuestionColumnInfo(transaction.getPath(), table);

            if (!columnTypes.containsKey("id")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'id' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("id") != RealmFieldType.INTEGER) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'int' for field 'id' in existing Realm file.");
            }
            if (table.isColumnNullable(columnInfo.idIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'id' does support null values in the existing Realm file. Use corresponding boxed type for field 'id' or migrate using io.realm.internal.Table.convertColumnToNotNullable().");
            }
            if (table.getPrimaryKey() != table.getColumnIndex("id")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Primary key not defined for field 'id' in existing Realm file. Add @PrimaryKey.");
            }
            if (!table.hasSearchIndex(table.getColumnIndex("id"))) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Index not defined for field 'id' in existing Realm file. Either set @Index or migrate using io.realm.internal.Table.removeSearchIndex().");
            }
            if (!columnTypes.containsKey("question")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'question' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("question") != RealmFieldType.STRING) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'String' for field 'question' in existing Realm file.");
            }
            if (!table.isColumnNullable(columnInfo.questionIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'question' is required. Either set @Required to field 'question' or migrate using io.realm.internal.Table.convertColumnToNullable().");
            }
            if (!columnTypes.containsKey("answer")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'answer' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("answer") != RealmFieldType.INTEGER) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'int' for field 'answer' in existing Realm file.");
            }
            if (table.isColumnNullable(columnInfo.answerIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'answer' does support null values in the existing Realm file. Use corresponding boxed type for field 'answer' or migrate using io.realm.internal.Table.convertColumnToNotNullable().");
            }
            return columnInfo;
        } else {
            throw new RealmMigrationNeededException(transaction.getPath(), "The RealmQuestion class is missing from the schema for this Realm.");
        }
    }

    public static String getTableName() {
        return "class_RealmQuestion";
    }

    public static List<String> getFieldNames() {
        return FIELD_NAMES;
    }

    @SuppressWarnings("cast")
    public static RealmQuestion createOrUpdateUsingJsonObject(Realm realm, JSONObject json, boolean update)
        throws JSONException {
        RealmQuestion obj = null;
        if (update) {
            Table table = realm.getTable(RealmQuestion.class);
            long pkColumnIndex = table.getPrimaryKey();
            if (!json.isNull("id")) {
                long rowIndex = table.findFirstLong(pkColumnIndex, json.getLong("id"));
                if (rowIndex != TableOrView.NO_MATCH) {
                    obj = new RealmQuestionRealmProxy(realm.schema.getColumnInfo(RealmQuestion.class));
                    obj.realm = realm;
                    obj.row = table.getUncheckedRow(rowIndex);
                }
            }
        }
        if (obj == null) {
            if (json.has("id")) {
                if (json.isNull("id")) {
                    obj = realm.createObject(RealmQuestion.class, null);
                } else {
                    obj = realm.createObject(RealmQuestion.class, json.getInt("id"));
                }
            } else {
                obj = realm.createObject(RealmQuestion.class);
            }
        }
        if (json.has("id")) {
            if (json.isNull("id")) {
                throw new IllegalArgumentException("Trying to set non-nullable field id to null.");
            } else {
                obj.setId((int) json.getInt("id"));
            }
        }
        if (json.has("question")) {
            if (json.isNull("question")) {
                obj.setQuestion(null);
            } else {
                obj.setQuestion((String) json.getString("question"));
            }
        }
        if (json.has("answer")) {
            if (json.isNull("answer")) {
                throw new IllegalArgumentException("Trying to set non-nullable field answer to null.");
            } else {
                obj.setAnswer((int) json.getInt("answer"));
            }
        }
        return obj;
    }

    @SuppressWarnings("cast")
    public static RealmQuestion createUsingJsonStream(Realm realm, JsonReader reader)
        throws IOException {
        RealmQuestion obj = realm.createObject(RealmQuestion.class);
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("id")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    throw new IllegalArgumentException("Trying to set non-nullable field id to null.");
                } else {
                    obj.setId((int) reader.nextInt());
                }
            } else if (name.equals("question")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    obj.setQuestion(null);
                } else {
                    obj.setQuestion((String) reader.nextString());
                }
            } else if (name.equals("answer")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    throw new IllegalArgumentException("Trying to set non-nullable field answer to null.");
                } else {
                    obj.setAnswer((int) reader.nextInt());
                }
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        return obj;
    }

    public static RealmQuestion copyOrUpdate(Realm realm, RealmQuestion object, boolean update, Map<RealmObject,RealmObjectProxy> cache) {
        if (object.realm != null && object.realm.getPath().equals(realm.getPath())) {
            return object;
        }
        RealmQuestion realmObject = null;
        boolean canUpdate = update;
        if (canUpdate) {
            Table table = realm.getTable(RealmQuestion.class);
            long pkColumnIndex = table.getPrimaryKey();
            long rowIndex = table.findFirstLong(pkColumnIndex, object.getId());
            if (rowIndex != TableOrView.NO_MATCH) {
                realmObject = new RealmQuestionRealmProxy(realm.schema.getColumnInfo(RealmQuestion.class));
                realmObject.realm = realm;
                realmObject.row = table.getUncheckedRow(rowIndex);
                cache.put(object, (RealmObjectProxy) realmObject);
            } else {
                canUpdate = false;
            }
        }

        if (canUpdate) {
            return update(realm, realmObject, object, cache);
        } else {
            return copy(realm, object, update, cache);
        }
    }

    public static RealmQuestion copy(Realm realm, RealmQuestion newObject, boolean update, Map<RealmObject,RealmObjectProxy> cache) {
        RealmQuestion realmObject = realm.createObject(RealmQuestion.class, newObject.getId());
        cache.put(newObject, (RealmObjectProxy) realmObject);
        realmObject.setId(newObject.getId());
        realmObject.setQuestion(newObject.getQuestion());
        realmObject.setAnswer(newObject.getAnswer());
        return realmObject;
    }

    public static RealmQuestion createDetachedCopy(RealmQuestion realmObject, int currentDepth, int maxDepth, Map<RealmObject, CacheData<RealmObject>> cache) {
        if (currentDepth > maxDepth || realmObject == null) {
            return null;
        }
        CacheData<RealmQuestion> cachedObject = (CacheData) cache.get(realmObject);
        RealmQuestion standaloneObject;
        if (cachedObject != null) {
            // Reuse cached object or recreate it because it was encountered at a lower depth.
            if (currentDepth >= cachedObject.minDepth) {
                return cachedObject.object;
            } else {
                standaloneObject = cachedObject.object;
                cachedObject.minDepth = currentDepth;
            }
        } else {
            standaloneObject = new RealmQuestion();
            cache.put(realmObject, new RealmObjectProxy.CacheData<RealmObject>(currentDepth, standaloneObject));
        }
        standaloneObject.setId(realmObject.getId());
        standaloneObject.setQuestion(realmObject.getQuestion());
        standaloneObject.setAnswer(realmObject.getAnswer());
        return standaloneObject;
    }

    static RealmQuestion update(Realm realm, RealmQuestion realmObject, RealmQuestion newObject, Map<RealmObject, RealmObjectProxy> cache) {
        realmObject.setQuestion(newObject.getQuestion());
        realmObject.setAnswer(newObject.getAnswer());
        return realmObject;
    }

    @Override
    public String toString() {
        if (!isValid()) {
            return "Invalid object";
        }
        StringBuilder stringBuilder = new StringBuilder("RealmQuestion = [");
        stringBuilder.append("{id:");
        stringBuilder.append(getId());
        stringBuilder.append("}");
        stringBuilder.append(",");
        stringBuilder.append("{question:");
        stringBuilder.append(getQuestion() != null ? getQuestion() : "null");
        stringBuilder.append("}");
        stringBuilder.append(",");
        stringBuilder.append("{answer:");
        stringBuilder.append(getAnswer());
        stringBuilder.append("}");
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    @Override
    public int hashCode() {
        String realmName = realm.getPath();
        String tableName = row.getTable().getName();
        long rowIndex = row.getIndex();

        int result = 17;
        result = 31 * result + ((realmName != null) ? realmName.hashCode() : 0);
        result = 31 * result + ((tableName != null) ? tableName.hashCode() : 0);
        result = 31 * result + (int) (rowIndex ^ (rowIndex >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RealmQuestionRealmProxy aRealmQuestion = (RealmQuestionRealmProxy)o;

        String path = realm.getPath();
        String otherPath = aRealmQuestion.realm.getPath();
        if (path != null ? !path.equals(otherPath) : otherPath != null) return false;;

        String tableName = row.getTable().getName();
        String otherTableName = aRealmQuestion.row.getTable().getName();
        if (tableName != null ? !tableName.equals(otherTableName) : otherTableName != null) return false;

        if (row.getIndex() != aRealmQuestion.row.getIndex()) return false;

        return true;
    }

}
