/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * 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 org.tanrabad.survey.repository.persistence;

import android.database.Cursor;

import org.tanrabad.survey.entity.Place;
import org.tanrabad.survey.entity.field.Location;
import org.tanrabad.survey.entity.lookup.PlaceSubType;
import org.tanrabad.survey.repository.BrokerPlaceSubTypeRepository;
import org.tanrabad.survey.utils.collection.CursorMapper;

import java.util.UUID;

class PlaceCursorMapper implements CursorMapper<Place> {

    private int idIndex;
    private int nameIndex;
    private int subtypeIndex;
    private int subdistrictCodeIndex;
    private int latIndex;
    private int lngIndex;
    private int updateByIndex;
    private int updateTimeIndex;
    private int changedStatusIndex;
    private int isTypeEditedIndex;

    public PlaceCursorMapper(Cursor cursor) {
        findColumnIndexOf(cursor);
    }

    private void findColumnIndexOf(Cursor cursor) {
        idIndex = cursor.getColumnIndex(PlaceColumn.ID);
        nameIndex = cursor.getColumnIndex(PlaceColumn.NAME);
        subtypeIndex = cursor.getColumnIndex(PlaceColumn.SUBTYPE_ID);
        subdistrictCodeIndex = cursor.getColumnIndex(PlaceColumn.SUBDISTRICT_CODE);
        latIndex = cursor.getColumnIndex(PlaceColumn.LATITUDE);
        lngIndex = cursor.getColumnIndex(PlaceColumn.LONGITUDE);
        updateByIndex = cursor.getColumnIndex(PlaceColumn.UPDATE_BY);
        updateTimeIndex = cursor.getColumnIndex(PlaceColumn.UPDATE_TIME);
        changedStatusIndex = cursor.getColumnIndex(PlaceColumn.CHANGED_STATUS);
        isTypeEditedIndex = cursor.getColumnIndex(PlaceColumn.IS_TYPE_EDITED);
    }

    @Override
    public PlaceWithChange map(Cursor cursor) {
        UUID uuid = UUID.fromString(cursor.getString(idIndex));
        PlaceWithChange place = new PlaceWithChange(
                uuid, cursor.getString(nameIndex), cursor.getInt(changedStatusIndex));
        place.setSubdistrictCode(cursor.getString(subdistrictCodeIndex));
        PlaceSubType subType = getSubType(cursor);
        place.setType(subType.getPlaceTypeId());
        place.setSubType(subType.getId());
        place.setLocation(getLocation(cursor));
        place.setUpdateBy(cursor.getString(updateByIndex));
        place.setUpdateTimestamp(cursor.getString(updateTimeIndex));
        if (isTypeEditedIndex >= 0)
            place.setTypeEdited(cursor.getInt(isTypeEditedIndex) == 1);
        return place;
    }

    private PlaceSubType getSubType(Cursor cursor) {
        return BrokerPlaceSubTypeRepository.getInstance().findById(cursor.getInt(subtypeIndex));
    }

    private Location getLocation(Cursor cursor) {
        double lat = cursor.getDouble(latIndex);
        double lng = cursor.getDouble(lngIndex);
        return (lat != 0f && lng != 0f) ? new Location(lat, lng) : null;
    }

}

@@ -739,16 +739,81 @@
 Cursor;%0A
+import android.support.annotation.NonNull;%0Aimport java.util.UUID;
 %0Aimport 
@@ -1082,32 +1082,8 @@
 r;%0A%0A
-import java.util.UUID;%0A%0A
 clas
@@ -3084,32 +3084,45 @@
 n place;%0A    %7D%0A%0A
+    @NonNull%0A
     private Plac
@@ -3166,22 +3166,91 @@
         
-return
+int subTypeId = cursor.getInt(subtypeIndex);%0A        PlaceSubType subType =
  BrokerP
@@ -3270,16 +3270,29 @@
 pository
+%0A            
 .getInst
@@ -3301,46 +3301,182 @@
 ce()
-.findById(cursor.getInt(subtypeIndex))
+%0A            .findById(subTypeId);%0A        if (subType == null)%0A            throw new IllegalArgumentException(%22Not found place subtype %22 + subTypeId);%0A        return subType
 ;%0A  

Throw exception when place sub-type not found