/*
 * Copyright 2017 ObjectBox Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.objectbox;

import io.objectbox.annotation.apihint.Internal;
import io.objectbox.converter.FlexObjectConverter;
import io.objectbox.converter.StringFlexMapConverter;
import io.objectbox.internal.CursorFactory;

import java.util.Map;

// NOTE: Copied from a plugin project (& removed some unused Properties).
// THIS CODE IS GENERATED BY ObjectBox, DO NOT EDIT.

/**
 * ObjectBox generated Cursor implementation for "TestEntity".
 * Note that this is a low-level class: usually you should stick to the Box class.
 */
public final class TestEntityCursor extends Cursor<TestEntity> {

    // For testing
    public static boolean INT_NULL_HACK;

    @Internal
    static final class Factory implements CursorFactory<TestEntity> {
        @Override
        public Cursor<TestEntity> createCursor(io.objectbox.Transaction tx, long cursorHandle, BoxStore boxStoreForEntities) {
            return new TestEntityCursor(tx, cursorHandle, boxStoreForEntities);
        }
    }

    private static final TestEntity_.TestEntityIdGetter ID_GETTER = TestEntity_.__ID_GETTER;

    private final StringFlexMapConverter stringObjectMapConverter = new StringFlexMapConverter();
    private final FlexObjectConverter flexPropertyConverter = new FlexObjectConverter();

    private final static int __ID_simpleBoolean = TestEntity_.simpleBoolean.id;
    private final static int __ID_simpleByte = TestEntity_.simpleByte.id;
    private final static int __ID_simpleShort = TestEntity_.simpleShort.id;
    private final static int __ID_simpleInt = TestEntity_.simpleInt.id;
    private final static int __ID_simpleLong = TestEntity_.simpleLong.id;
    private final static int __ID_simpleFloat = TestEntity_.simpleFloat.id;
    private final static int __ID_simpleDouble = TestEntity_.simpleDouble.id;
    private final static int __ID_simpleString = TestEntity_.simpleString.id;
    private final static int __ID_simpleByteArray = TestEntity_.simpleByteArray.id;
    private final static int __ID_simpleStringArray = TestEntity_.simpleStringArray.id;
    private final static int __ID_simpleStringList = TestEntity_.simpleStringList.id;
    private final static int __ID_simpleShortU = TestEntity_.simpleShortU.id;
    private final static int __ID_simpleIntU = TestEntity_.simpleIntU.id;
    private final static int __ID_simpleLongU = TestEntity_.simpleLongU.id;
    private final static int __ID_stringObjectMap = TestEntity_.stringObjectMap.id;
    private final static int __ID_flexProperty = TestEntity_.flexProperty.id;
    private final static int __ID_shortArray = TestEntity_.shortArray.id;
    private final static int __ID_charArray = TestEntity_.charArray.id;
    private final static int __ID_intArray = TestEntity_.intArray.id;
    private final static int __ID_longArray = TestEntity_.longArray.id;
    private final static int __ID_floatArray = TestEntity_.floatArray.id;
    private final static int __ID_doubleArray = TestEntity_.doubleArray.id;

    public TestEntityCursor(io.objectbox.Transaction tx, long cursor, BoxStore boxStore) {
        super(tx, cursor, TestEntity_.__INSTANCE, boxStore);
    }

    @Override
    public long getId(TestEntity entity) {
        return ID_GETTER.getId(entity);
    }

    /**
     * Puts an object into its box.
     *
     * @return The ID of the object within its box.
     */
    @Override
    public long put(TestEntity entity) {
        short[] shortArray = entity.getShortArray();
        int __id17 = shortArray != null ? __ID_shortArray : 0;

        collectShortArray(cursor, 0, PUT_FLAG_FIRST,
                __id17, shortArray);

        char[] charArray = entity.getCharArray();
        int __id18 = charArray != null ? __ID_charArray : 0;

        collectCharArray(cursor, 0, 0,
                __id18, charArray);

        int[] intArray = entity.getIntArray();
        int __id19 = intArray != null ? __ID_intArray : 0;

        collectIntArray(cursor, 0, 0,
                __id19, intArray);

        long[] longArray = entity.getLongArray();
        int __id20 = longArray != null ? __ID_longArray : 0;

        collectLongArray(cursor, 0, 0,
                __id20, longArray);

        float[] floatArray = entity.getFloatArray();
        int __id21 = floatArray != null ? __ID_floatArray : 0;

        collectFloatArray(cursor, 0, 0,
                __id21, floatArray);

        double[] doubleArray = entity.getDoubleArray();
        int __id22 = doubleArray != null ? __ID_doubleArray : 0;

        collectDoubleArray(cursor, 0, 0,
                __id22, doubleArray);

        String[] simpleStringArray = entity.getSimpleStringArray();
        int __id10 = simpleStringArray != null ? __ID_simpleStringArray : 0;

        collectStringArray(cursor, 0, 0,
                __id10, simpleStringArray);

        java.util.List<String> simpleStringList = entity.getSimpleStringList();
        int __id11 = simpleStringList != null ? __ID_simpleStringList : 0;

        collectStringList(cursor, 0, 0,
                __id11, simpleStringList);

        String simpleString = entity.getSimpleString();
        int __id8 = simpleString != null ? __ID_simpleString : 0;
        byte[] simpleByteArray = entity.getSimpleByteArray();
        int __id9 = simpleByteArray != null ? __ID_simpleByteArray : 0;
        Map stringObjectMap = entity.getStringObjectMap();
        int __id15 = stringObjectMap != null ? __ID_stringObjectMap : 0;
        Object flexProperty = entity.getFlexProperty();
        int __id16 = flexProperty != null ? __ID_flexProperty : 0;

        collect430000(cursor, 0, 0,
                __id8, simpleString, 0, null,
                0, null, 0, null,
                __id9, simpleByteArray, __id15, __id15 != 0 ? stringObjectMapConverter.convertToDatabaseValue(stringObjectMap) : null,
                __id16, __id16 != 0 ? flexPropertyConverter.convertToDatabaseValue(flexProperty) : null);

        collect313311(cursor, 0, 0,
                0, null, 0, null,
                0, null, 0, null,
                __ID_simpleLong, entity.getSimpleLong(), __ID_simpleLongU, entity.getSimpleLongU(),
                INT_NULL_HACK ? 0 : __ID_simpleInt, entity.getSimpleInt(), __ID_simpleIntU, entity.getSimpleIntU(),
                __ID_simpleShort, entity.getSimpleShort(), __ID_simpleShortU, entity.getSimpleShortU(),
                __ID_simpleFloat, entity.getSimpleFloat(), __ID_simpleDouble, entity.getSimpleDouble());

        long __assignedId = collect004000(cursor, entity.getId(), PUT_FLAG_COMPLETE,
                __ID_simpleByte, entity.getSimpleByte(), __ID_simpleBoolean, entity.getSimpleBoolean() ? 1 : 0,
                0, 0, 0, 0);

        entity.setId(__assignedId);

        return __assignedId;
    }

}
