package com.bam.relationshipcorelibrary.Core.DatabaseObjects;

import android.os.Parcel;
import android.os.Parcelable;

import com.bam.relationshipcorelibrary.Core.DatabaseObjects.LookupTables.PointType;
import com.bam.relationshipcorelibrary.Core.SQL.SQLdataReconstruction;
import com.bam.relationshipcorelibrary.Core.TimeRelated.DateTimeUTC;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by bahram on 25.09.2014.
 */
public class RelationshipPoint extends ObjectTimestamp implements Parcelable
{
    private String pointDB_ID;
    private String pointEventDB_ID;
    private int pointValue;
    private int pointType;
    private String pointComment;
    private AtomicInteger pointChainPosition;

    public static final String POINTS_TABLE_PREFIX = SQLdataReconstruction.POINTSINFO_PREFIX;
    public static final String POINTS_TABLE_NAME = "Point";
    public static final String POINTS_DB_ID_FIELD = "PointDB_ID";
    public static final String POINTS_POINTEVENT_DB_ID_FIELD = PointEvent.POINTEVENT_DB_ID_FIELD;
    public static final String POINTS_POINTVALUE_FIELD = "Point_Value";
    public static final String POINTS_POINT_TYPE_FIELD = PointType.POINTTYPE_NUMBER_FIELD;
    public static final String POINTS_POINT_COMMENT_FIELD = "Point_Comment";
    public static final String POINTS_POINTCHAIN_POSITION_FIELD = "Point_ChainPosition";

    /**
     * @param DB_ID
     * @param value
     * @param pointType
     * @param comment
     */
    public RelationshipPoint(String DB_ID, String pEventID, int pointType, int value, String comment, int chainPosition)
    {
        pointDB_ID = DB_ID;
        pointEventDB_ID = pEventID;
        pointValue = value;
        this.pointType = pointType;
        pointComment = comment;
        pointChainPosition = new AtomicInteger(chainPosition);
    }

    /************************
     * GETTERS
     ************************/

    /**
     * @return the relPointDB_ID
     */
    public String getPointDB_ID()
    {
        return pointDB_ID;
    }

    /**
     * @return the pointEventDB_ID
     */
    public String getPointEventDB_ID()
    {
        return pointEventDB_ID;
    }

    /**
     * @return the pointValue
     */
    public int getPointValue()
    {
        return pointValue;
    }

    /**
     * @return the pointType
     */
    public int getPointType()
    {
        return pointType;
    }

    /**
     * @return the pointComment
     */
    public String getPointComment()
    {
        return pointComment;
    }

    /**
     * @return the pointChainPosition
     */
    public int getPointChainPosition()
    {
        return pointChainPosition.get();
    }

    /************************
     * SETTERS
     ************************/

    /**
     * @param relPointDB_ID the relPointDB_ID to set
     */
    public void setRelPointDB_ID(String relPointDB_ID)
    {
        this.pointDB_ID = relPointDB_ID;
    }

    /**
     * @param pointEventDB_ID the pointEventDB_ID to set
     */
    public void setPointEventDB_ID(String pointEventDB_ID)
    {
        this.pointEventDB_ID = pointEventDB_ID;
    }

    /**
     * @param pointValue the pointValue to set
     */
    public void setPointValue(int pointValue)
    {
        this.pointValue = pointValue;
    }

    /**
     * @param pointType the pointType to set
     */
    public void setPointType(int pointType)
    {
        this.pointType = pointType;
    }

    /**
     * @param pointComment the pointComment to set
     */
    public void setPointComment(String pointComment)
    {
        this.pointComment = pointComment;
    }

    /**
     * @param pointChainPosition the pointChainPosition to set
     */
    public void setPointChainPosition(int pointChainPosition)
    {
        this.pointChainPosition.set(pointChainPosition);
    }


    // *********************************
    // Parcelable Implementation
    // *********************************

    /**
     * Describe the kinds of special objects contained in this Parcelable's
     * marshalled representation.
     *
     * @return a bitmask indicating the set of special object types marshalled
     * by the Parcelable.
     */
    @Override
    public int describeContents()
    {
        return 0;
    }

    /**
     * Flatten this object in to a Parcel.
     *
     * @param dest  The Parcel in which the object should be written.
     * @param flags Additional flags about how the object should be written.
     *              May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
     */
    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeString(pointDB_ID);
        dest.writeString(pointEventDB_ID);
        dest.writeInt(pointValue);
        dest.writeInt(pointType);
        dest.writeString(pointComment);
        dest.writeInt(pointChainPosition.get());


        // CREATED AND MODIFIED
        dest.writeString(DateTimeUTC.getDateTimeFormatted(create,
                DateTimeUTC.getTimestampStorageFormat())); // User created
        dest.writeString(DateTimeUTC.getDateTimeFormatted(lastModified,
                DateTimeUTC.getTimestampStorageFormat())); // User Last Modified
    }


    public static final Creator<RelationshipPoint> CREATOR = new Creator<RelationshipPoint>()
    {
        @Override
        public RelationshipPoint createFromParcel(Parcel source)
        {
            RelationshipPoint point = new RelationshipPoint(
                    source.readString(), // pointDB_ID
                    source.readString(), // pointEventDB_ID
                    source.readInt(), // pointValue
                    source.readInt(), // pointType
                    source.readString(), // pointComment
                    source.readInt() // pointChainPosition
            );


            // CREATED AND MODIFIED
            // Gets the created string
            String created = source.readString();
            if( created != null && created != "" )
            {
                point.setCreate(DateTimeUTC.getDateTimeFormatted(created,
                        DateTimeUTC.getTimestampStorageFormat()));
            }


            // Gets the last modified string
            String modified = source.readString();
            if( modified != null && modified != "" )
            {
                point.setLastModified(DateTimeUTC.getDateTimeFormatted(modified,
                        DateTimeUTC.getTimestampStorageFormat()));
            }

            return point;
        }

        @Override
        public RelationshipPoint[] newArray(int size)
        {
            return new RelationshipPoint[size];
        }
    };

}
