package io.realm;


import android.util.JsonReader;
import android.util.JsonToken;
import com.mgdiez.data.bean.vo.tweet.UserVo;
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 UserVoRealmProxy extends UserVo
    implements RealmObjectProxy {

    static final class UserVoColumnInfo extends ColumnInfo {

        public final long userNameIndex;
        public final long twitterImageIndex;
        public final long userPublicNameIndex;
        public final long backgroundImageIndex;
        public final long followersIndex;
        public final long followingIndex;
        public final long nTweetsIndex;

        UserVoColumnInfo(String path, Table table) {
            final Map<String, Long> indicesMap = new HashMap<String, Long>(7);
            this.userNameIndex = getValidColumnIndex(path, table, "UserVo", "userName");
            indicesMap.put("userName", this.userNameIndex);

            this.twitterImageIndex = getValidColumnIndex(path, table, "UserVo", "twitterImage");
            indicesMap.put("twitterImage", this.twitterImageIndex);

            this.userPublicNameIndex = getValidColumnIndex(path, table, "UserVo", "userPublicName");
            indicesMap.put("userPublicName", this.userPublicNameIndex);

            this.backgroundImageIndex = getValidColumnIndex(path, table, "UserVo", "backgroundImage");
            indicesMap.put("backgroundImage", this.backgroundImageIndex);

            this.followersIndex = getValidColumnIndex(path, table, "UserVo", "followers");
            indicesMap.put("followers", this.followersIndex);

            this.followingIndex = getValidColumnIndex(path, table, "UserVo", "following");
            indicesMap.put("following", this.followingIndex);

            this.nTweetsIndex = getValidColumnIndex(path, table, "UserVo", "nTweets");
            indicesMap.put("nTweets", this.nTweetsIndex);

            setIndicesMap(indicesMap);
        }
    }

    private final UserVoColumnInfo columnInfo;
    private static final List<String> FIELD_NAMES;
    static {
        List<String> fieldNames = new ArrayList<String>();
        fieldNames.add("userName");
        fieldNames.add("twitterImage");
        fieldNames.add("userPublicName");
        fieldNames.add("backgroundImage");
        fieldNames.add("followers");
        fieldNames.add("following");
        fieldNames.add("nTweets");
        FIELD_NAMES = Collections.unmodifiableList(fieldNames);
    }

    UserVoRealmProxy(ColumnInfo columnInfo) {
        this.columnInfo = (UserVoColumnInfo) columnInfo;
    }

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

    @Override
    public void setUserName(String value) {
        realm.checkIfValid();
        if (value == null) {
            throw new IllegalArgumentException("Trying to set non-nullable field userName to null.");
        }
        row.setString(columnInfo.userNameIndex, value);
    }

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

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

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

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

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

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

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

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

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

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

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

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

    public static Table initTable(ImplicitTransaction transaction) {
        if (!transaction.hasTable("class_UserVo")) {
            Table table = transaction.getTable("class_UserVo");
            table.addColumn(RealmFieldType.STRING, "userName", Table.NOT_NULLABLE);
            table.addColumn(RealmFieldType.STRING, "twitterImage", Table.NULLABLE);
            table.addColumn(RealmFieldType.STRING, "userPublicName", Table.NULLABLE);
            table.addColumn(RealmFieldType.STRING, "backgroundImage", Table.NULLABLE);
            table.addColumn(RealmFieldType.INTEGER, "followers", Table.NOT_NULLABLE);
            table.addColumn(RealmFieldType.INTEGER, "following", Table.NOT_NULLABLE);
            table.addColumn(RealmFieldType.INTEGER, "nTweets", Table.NOT_NULLABLE);
            table.addSearchIndex(table.getColumnIndex("userName"));
            table.setPrimaryKey("userName");
            return table;
        }
        return transaction.getTable("class_UserVo");
    }

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

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

            if (!columnTypes.containsKey("userName")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'userName' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("userName") != RealmFieldType.STRING) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'String' for field 'userName' in existing Realm file.");
            }
            if (table.isColumnNullable(columnInfo.userNameIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'userName' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'userName' or migrate using io.realm.internal.Table.convertColumnToNotNullable().");
            }
            if (table.getPrimaryKey() != table.getColumnIndex("userName")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Primary key not defined for field 'userName' in existing Realm file. Add @PrimaryKey.");
            }
            if (!table.hasSearchIndex(table.getColumnIndex("userName"))) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Index not defined for field 'userName' in existing Realm file. Either set @Index or migrate using io.realm.internal.Table.removeSearchIndex().");
            }
            if (!columnTypes.containsKey("twitterImage")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'twitterImage' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("twitterImage") != RealmFieldType.STRING) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'String' for field 'twitterImage' in existing Realm file.");
            }
            if (!table.isColumnNullable(columnInfo.twitterImageIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'twitterImage' is required. Either set @Required to field 'twitterImage' or migrate using io.realm.internal.Table.convertColumnToNullable().");
            }
            if (!columnTypes.containsKey("userPublicName")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'userPublicName' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("userPublicName") != RealmFieldType.STRING) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'String' for field 'userPublicName' in existing Realm file.");
            }
            if (!table.isColumnNullable(columnInfo.userPublicNameIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'userPublicName' is required. Either set @Required to field 'userPublicName' or migrate using io.realm.internal.Table.convertColumnToNullable().");
            }
            if (!columnTypes.containsKey("backgroundImage")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'backgroundImage' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("backgroundImage") != RealmFieldType.STRING) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'String' for field 'backgroundImage' in existing Realm file.");
            }
            if (!table.isColumnNullable(columnInfo.backgroundImageIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'backgroundImage' is required. Either set @Required to field 'backgroundImage' or migrate using io.realm.internal.Table.convertColumnToNullable().");
            }
            if (!columnTypes.containsKey("followers")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'followers' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("followers") != RealmFieldType.INTEGER) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'int' for field 'followers' in existing Realm file.");
            }
            if (table.isColumnNullable(columnInfo.followersIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'followers' does support null values in the existing Realm file. Use corresponding boxed type for field 'followers' or migrate using io.realm.internal.Table.convertColumnToNotNullable().");
            }
            if (!columnTypes.containsKey("following")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'following' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("following") != RealmFieldType.INTEGER) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'int' for field 'following' in existing Realm file.");
            }
            if (table.isColumnNullable(columnInfo.followingIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'following' does support null values in the existing Realm file. Use corresponding boxed type for field 'following' or migrate using io.realm.internal.Table.convertColumnToNotNullable().");
            }
            if (!columnTypes.containsKey("nTweets")) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Missing field 'nTweets' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
            }
            if (columnTypes.get("nTweets") != RealmFieldType.INTEGER) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Invalid type 'int' for field 'nTweets' in existing Realm file.");
            }
            if (table.isColumnNullable(columnInfo.nTweetsIndex)) {
                throw new RealmMigrationNeededException(transaction.getPath(), "Field 'nTweets' does support null values in the existing Realm file. Use corresponding boxed type for field 'nTweets' or migrate using io.realm.internal.Table.convertColumnToNotNullable().");
            }
            return columnInfo;
        } else {
            throw new RealmMigrationNeededException(transaction.getPath(), "The UserVo class is missing from the schema for this Realm.");
        }
    }

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

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

    @SuppressWarnings("cast")
    public static UserVo createOrUpdateUsingJsonObject(Realm realm, JSONObject json, boolean update)
        throws JSONException {
        UserVo obj = null;
        if (update) {
            Table table = realm.getTable(UserVo.class);
            long pkColumnIndex = table.getPrimaryKey();
            if (!json.isNull("userName")) {
                long rowIndex = table.findFirstString(pkColumnIndex, json.getString("userName"));
                if (rowIndex != TableOrView.NO_MATCH) {
                    obj = new UserVoRealmProxy(realm.schema.getColumnInfo(UserVo.class));
                    obj.realm = realm;
                    obj.row = table.getUncheckedRow(rowIndex);
                }
            }
        }
        if (obj == null) {
            if (json.has("userName")) {
                if (json.isNull("userName")) {
                    obj = realm.createObject(UserVo.class, null);
                } else {
                    obj = realm.createObject(UserVo.class, json.getString("userName"));
                }
            } else {
                obj = realm.createObject(UserVo.class);
            }
        }
        if (json.has("userName")) {
            if (json.isNull("userName")) {
                obj.setUserName(null);
            } else {
                obj.setUserName((String) json.getString("userName"));
            }
        }
        if (json.has("twitterImage")) {
            if (json.isNull("twitterImage")) {
                obj.setTwitterImage(null);
            } else {
                obj.setTwitterImage((String) json.getString("twitterImage"));
            }
        }
        if (json.has("userPublicName")) {
            if (json.isNull("userPublicName")) {
                obj.setUserPublicName(null);
            } else {
                obj.setUserPublicName((String) json.getString("userPublicName"));
            }
        }
        if (json.has("backgroundImage")) {
            if (json.isNull("backgroundImage")) {
                obj.setBackgroundImage(null);
            } else {
                obj.setBackgroundImage((String) json.getString("backgroundImage"));
            }
        }
        if (json.has("followers")) {
            if (json.isNull("followers")) {
                throw new IllegalArgumentException("Trying to set non-nullable field followers to null.");
            } else {
                obj.setFollowers((int) json.getInt("followers"));
            }
        }
        if (json.has("following")) {
            if (json.isNull("following")) {
                throw new IllegalArgumentException("Trying to set non-nullable field following to null.");
            } else {
                obj.setFollowing((int) json.getInt("following"));
            }
        }
        if (json.has("nTweets")) {
            if (json.isNull("nTweets")) {
                throw new IllegalArgumentException("Trying to set non-nullable field nTweets to null.");
            } else {
                obj.setnTweets((int) json.getInt("nTweets"));
            }
        }
        return obj;
    }

    @SuppressWarnings("cast")
    public static UserVo createUsingJsonStream(Realm realm, JsonReader reader)
        throws IOException {
        UserVo obj = realm.createObject(UserVo.class);
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("userName")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    obj.setUserName(null);
                } else {
                    obj.setUserName((String) reader.nextString());
                }
            } else if (name.equals("twitterImage")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    obj.setTwitterImage(null);
                } else {
                    obj.setTwitterImage((String) reader.nextString());
                }
            } else if (name.equals("userPublicName")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    obj.setUserPublicName(null);
                } else {
                    obj.setUserPublicName((String) reader.nextString());
                }
            } else if (name.equals("backgroundImage")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    obj.setBackgroundImage(null);
                } else {
                    obj.setBackgroundImage((String) reader.nextString());
                }
            } else if (name.equals("followers")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    throw new IllegalArgumentException("Trying to set non-nullable field followers to null.");
                } else {
                    obj.setFollowers((int) reader.nextInt());
                }
            } else if (name.equals("following")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    throw new IllegalArgumentException("Trying to set non-nullable field following to null.");
                } else {
                    obj.setFollowing((int) reader.nextInt());
                }
            } else if (name.equals("nTweets")) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                    throw new IllegalArgumentException("Trying to set non-nullable field nTweets to null.");
                } else {
                    obj.setnTweets((int) reader.nextInt());
                }
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        return obj;
    }

    public static UserVo copyOrUpdate(Realm realm, UserVo object, boolean update, Map<RealmObject,RealmObjectProxy> cache) {
        if (object.realm != null && object.realm.getPath().equals(realm.getPath())) {
            return object;
        }
        UserVo realmObject = null;
        boolean canUpdate = update;
        if (canUpdate) {
            Table table = realm.getTable(UserVo.class);
            long pkColumnIndex = table.getPrimaryKey();
            if (object.getUserName() == null) {
                throw new IllegalArgumentException("Primary key value must not be null.");
            }
            long rowIndex = table.findFirstString(pkColumnIndex, object.getUserName());
            if (rowIndex != TableOrView.NO_MATCH) {
                realmObject = new UserVoRealmProxy(realm.schema.getColumnInfo(UserVo.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 UserVo copy(Realm realm, UserVo newObject, boolean update, Map<RealmObject,RealmObjectProxy> cache) {
        UserVo realmObject = realm.createObject(UserVo.class, newObject.getUserName());
        cache.put(newObject, (RealmObjectProxy) realmObject);
        realmObject.setUserName(newObject.getUserName());
        realmObject.setTwitterImage(newObject.getTwitterImage());
        realmObject.setUserPublicName(newObject.getUserPublicName());
        realmObject.setBackgroundImage(newObject.getBackgroundImage());
        realmObject.setFollowers(newObject.getFollowers());
        realmObject.setFollowing(newObject.getFollowing());
        realmObject.setnTweets(newObject.getnTweets());
        return realmObject;
    }

    public static UserVo createDetachedCopy(UserVo realmObject, int currentDepth, int maxDepth, Map<RealmObject, CacheData<RealmObject>> cache) {
        if (currentDepth > maxDepth || realmObject == null) {
            return null;
        }
        CacheData<UserVo> cachedObject = (CacheData) cache.get(realmObject);
        UserVo 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 UserVo();
            cache.put(realmObject, new RealmObjectProxy.CacheData<RealmObject>(currentDepth, standaloneObject));
        }
        standaloneObject.setUserName(realmObject.getUserName());
        standaloneObject.setTwitterImage(realmObject.getTwitterImage());
        standaloneObject.setUserPublicName(realmObject.getUserPublicName());
        standaloneObject.setBackgroundImage(realmObject.getBackgroundImage());
        standaloneObject.setFollowers(realmObject.getFollowers());
        standaloneObject.setFollowing(realmObject.getFollowing());
        standaloneObject.setnTweets(realmObject.getnTweets());
        return standaloneObject;
    }

    static UserVo update(Realm realm, UserVo realmObject, UserVo newObject, Map<RealmObject, RealmObjectProxy> cache) {
        realmObject.setTwitterImage(newObject.getTwitterImage());
        realmObject.setUserPublicName(newObject.getUserPublicName());
        realmObject.setBackgroundImage(newObject.getBackgroundImage());
        realmObject.setFollowers(newObject.getFollowers());
        realmObject.setFollowing(newObject.getFollowing());
        realmObject.setnTweets(newObject.getnTweets());
        return realmObject;
    }

    @Override
    public String toString() {
        if (!isValid()) {
            return "Invalid object";
        }
        StringBuilder stringBuilder = new StringBuilder("UserVo = [");
        stringBuilder.append("{userName:");
        stringBuilder.append(getUserName());
        stringBuilder.append("}");
        stringBuilder.append(",");
        stringBuilder.append("{twitterImage:");
        stringBuilder.append(getTwitterImage() != null ? getTwitterImage() : "null");
        stringBuilder.append("}");
        stringBuilder.append(",");
        stringBuilder.append("{userPublicName:");
        stringBuilder.append(getUserPublicName() != null ? getUserPublicName() : "null");
        stringBuilder.append("}");
        stringBuilder.append(",");
        stringBuilder.append("{backgroundImage:");
        stringBuilder.append(getBackgroundImage() != null ? getBackgroundImage() : "null");
        stringBuilder.append("}");
        stringBuilder.append(",");
        stringBuilder.append("{followers:");
        stringBuilder.append(getFollowers());
        stringBuilder.append("}");
        stringBuilder.append(",");
        stringBuilder.append("{following:");
        stringBuilder.append(getFollowing());
        stringBuilder.append("}");
        stringBuilder.append(",");
        stringBuilder.append("{nTweets:");
        stringBuilder.append(getnTweets());
        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;
        UserVoRealmProxy aUserVo = (UserVoRealmProxy)o;

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

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

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

        return true;
    }

}
