package com.setiawanpaiman.sunnyreader.data.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class Story implements Parcelable {

    @SerializedName("id")
    private long mId;

    @SerializedName("time")
    private long mTimestamp;

    @SerializedName("by")
    private String mAuthor;

    @SerializedName("title")
    private String mTitle;

    @SerializedName("url")
    private String mUrl;

    @SerializedName("score")
    private long mScore;

    @SerializedName("kids")
    private List<Long> mCommentIds;

    public static final Parcelable.Creator<Story> CREATOR = new Parcelable.Creator<Story>() {
        @Override
        public Story createFromParcel(Parcel source) {
            return new Story(source);
        }

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

    protected Story(Builder builder) {
        mId = builder.mId;
        mTimestamp = builder.mTimestamp;
        mAuthor = builder.mAuthor;
        mTitle = builder.mTitle;
        mUrl = builder.mUrl;
        mScore = builder.mScore;
        mCommentIds = builder.mCommentIds;
    }

    protected Story(Parcel in) {
        this.mId = in.readLong();
        this.mTimestamp = in.readLong();
        this.mAuthor = in.readString();
        this.mTitle = in.readString();
        this.mUrl = in.readString();
        this.mScore = in.readLong();
        this.mCommentIds = new ArrayList<>();
        in.readList(this.mCommentIds, Long.class.getClassLoader());
    }

    @Override
    public int describeContents() { return 0; }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.mId);
        dest.writeLong(this.mTimestamp);
        dest.writeString(this.mAuthor);
        dest.writeString(this.mTitle);
        dest.writeString(this.mUrl);
        dest.writeLong(this.mScore);
        dest.writeList(this.mCommentIds);
    }

    public long getId() {
        return mId;
    }

    public long getTimestamp() {
        return mTimestamp;
    }

    public String getAuthor() {
        return mAuthor;
    }

    public String getTitle() {
        return mTitle;
    }

    public String getUrl() {
        return mUrl;
    }

    public long getScore() {
        return mScore;
    }

    @NonNull
    public List<Long> getCommentIds() {
        // defensive copy
        if (mCommentIds == null) return new ArrayList<>();
        else return new ArrayList<>(mCommentIds);
    }

    public int getTotalComments() {
        if (mCommentIds == null) return 0;
        else return mCommentIds.size();
    }

    public static Builder newBuilder(long id) {
        return new Builder(id);
    }

    public static class Builder {
        private long mId;
        private long mTimestamp;
        private String mAuthor;
        private String mTitle;
        private String mUrl;
        private long mScore;
        private List<Long> mCommentIds;

        public Builder(long id) {
            mId = id;
            mCommentIds = new ArrayList<>();
        }

        public Builder setTimestamp(long timestamp) {
            mTimestamp = timestamp;
            return this;
        }

        public Builder setAuthor(String author) {
            mAuthor = author;
            return this;
        }

        public Builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        public Builder setUrl(String url) {
            mUrl = url;
            return this;
        }

        public Builder setScore(long score) {
            mScore = score;
            return this;
        }

        public Builder setCommentIds(List<Long> commentIds) {
            mCommentIds.clear();
            if (commentIds != null) mCommentIds.addAll(commentIds);
            return this;
        }

        public Story build() {
            return new Story(this);
        }
    }
}
@@ -508,24 +508,79 @@
 ng mTitle;%0A%0A
+    @SerializedName(%22text%22)%0A    private String mText;%0A%0A
     @Seriali
@@ -594,16 +594,16 @@
 (%22url%22)%0A
-
     priv
@@ -1239,32 +1239,63 @@
 builder.mTitle;%0A
+        mText = builder.mText;%0A
         mUrl = b
@@ -1569,32 +1569,70 @@
 n.readString();%0A
+        this.mText = in.readString();%0A
         this.mUr
@@ -2093,24 +2093,62 @@
 is.mTitle);%0A
+        dest.writeString(this.mText);%0A
         dest
@@ -2494,32 +2494,91 @@
  mTitle;%0A    %7D%0A%0A
+    public String getText() %7B%0A        return mText;%0A    %7D%0A%0A
     public Strin
@@ -3233,32 +3233,62 @@
  String mTitle;%0A
+        private String mText;%0A
         private 
@@ -3768,24 +3768,24 @@
 ng title) %7B%0A
-
             
@@ -3828,32 +3828,140 @@
 his;%0A        %7D%0A%0A
+        public Builder setText(String text) %7B%0A            mText = text;%0A            return this;%0A        %7D%0A%0A
         public B

Add text attribute to Story