/*
 * Copyright (c) 2004-2018, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.hisp.dhis.android.core.dataset;

import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.gabrielittner.auto.value.cursor.ColumnAdapter;
import com.google.auto.value.AutoValue;

import org.hisp.dhis.android.core.common.BaseDataModel;
import org.hisp.dhis.android.core.common.ObjectWithUid;
import org.hisp.dhis.android.core.data.database.DbDateColumnAdapter;
import org.hisp.dhis.android.core.data.database.ObjectWithUidColumnAdapter;

import java.util.Date;

@AutoValue
@JsonDeserialize(builder = AutoValue_DataInputPeriod.Builder.class)
public abstract class DataInputPeriod extends BaseDataModel {

    @JsonIgnore
    @ColumnAdapter(ObjectWithUidColumnAdapter.class)
    public abstract ObjectWithUid dataSet();

    String dataSetUid() {
        ObjectWithUid dataSet = dataSet();
        return dataSet == null ? null : dataSet.uid();
    }

    @JsonProperty
    @ColumnAdapter(ObjectWithUidColumnAdapter.class)
    public abstract ObjectWithUid period();

    String periodUid() {
        ObjectWithUid period = period();
        return period == null ? null : period.uid();
    }

    @Nullable
    @JsonProperty
    @ColumnAdapter(DbDateColumnAdapter.class)
    public abstract Date openingDate();

    @Nullable
    @JsonProperty
    @ColumnAdapter(DbDateColumnAdapter.class)
    public abstract Date closingDate();

    @NonNull
    static DataInputPeriod create(Cursor cursor) {
        return $AutoValue_DataInputPeriod.createFromCursor(cursor);
    }

    public abstract DataInputPeriod.Builder toBuilder();

    public static DataInputPeriod.Builder builder() {
        return new $$AutoValue_DataInputPeriod.Builder();
    }


    @AutoValue.Builder
    @JsonPOJOBuilder(withPrefix = "")
    public abstract static class Builder extends BaseDataModel.Builder<DataInputPeriod.Builder> {

        public abstract Builder dataSet(@NonNull ObjectWithUid dataSet);

        public abstract Builder period(@NonNull ObjectWithUid period);

        public abstract Builder openingDate(@NonNull Date openingDate);

        public abstract Builder closingDate(@NonNull Date closingDate);

        public abstract DataInputPeriod build();
    }
}

@@ -2622,51 +2622,8 @@
 ) %7B%0A
-        ObjectWithUid dataSet = dataSet();%0A
     
@@ -2640,16 +2640,18 @@
  dataSet
+()
  == null
@@ -2667,16 +2667,18 @@
  dataSet
+()
 .uid();%0A
@@ -2829,49 +2829,8 @@
 ) %7B%0A
-        ObjectWithUid period = period();%0A
     
@@ -2846,16 +2846,18 @@
 n period
+()
  == null
@@ -2872,16 +2872,18 @@
 : period
+()
 .uid();%0A

Refactor UID getters in DataInputPeriod