package net.somethingdreadful.MAL.api.MALModels;

import com.google.gson.annotations.SerializedName;

import net.somethingdreadful.MAL.api.BaseModels.AnimeManga.Anime;
import net.somethingdreadful.MAL.api.BaseModels.AnimeManga.Manga;

import java.io.Serializable;
import java.util.ArrayList;

import lombok.Getter;
import lombok.Setter;

public class History implements Serializable {

    /**
     * The anime/manga details.
     */
    @Getter
    @Setter
    private Series item;

    /**
     * The type of record.
     * <p/>
     * It indicates what kind of record the item is.
     * Example: "anime" or "manga"
     */
    @Getter
    @Setter
    private String type;

    /**
     * The time when the anime or manga was updated
     */
    @Getter
    @Setter
    @SerializedName("time_updated")
    private String timeUpdated;

    class Series {

        /**
         * The record of the ID.
         */
        @Getter
        @Setter
        private int id;

        /**
         * Total number of chapters of the manga.
         * <p/>
         * This value is the number of chapters of the manga, or null if unknown.
         */
        @Getter
        @Setter
        private int chapters;

        /**
         * Total number of episodes of the anime.
         * <p/>
         * This value is the number of episodes of the anime, or null if unknown.
         */
        @Getter
        @Setter
        private int episodes;

        /**
         * Title of a record.
         **/
        @Getter
        @Setter
        private String title;

        /**
         * The time when the record has been updated.
         **/
        @Getter
        @Setter
        @SerializedName("time_updated")
        private String timeUpdated;
    }

    public net.somethingdreadful.MAL.api.BaseModels.History createBaseModel() {
        net.somethingdreadful.MAL.api.BaseModels.History model = new net.somethingdreadful.MAL.api.BaseModels.History();
        if (type.equals("anime")) {
            model.setAnime(new Anime());
            model.getAnime().setId(getItem().getId());
            model.getAnime().setEpisodes(getItem().getEpisodes());
            model.getAnime().setTitle(getItem().getTitle());
        } else {
            model.setManga(new Manga());
            model.getManga().setId(getItem().getId());
            model.getManga().setChapters(getItem().getChapters());
            model.getManga().setTitle(getItem().getTitle());
        }
        model.setCreatedAt(getTimeUpdated());
        return model;
    }

    public static ArrayList<net.somethingdreadful.MAL.api.BaseModels.History> convertBaseHistoryList(ArrayList<History> histories) {
        ArrayList<net.somethingdreadful.MAL.api.BaseModels.History> historyArrayList = new ArrayList<>();
        for (History history: histories) {
            historyArrayList.add(history.createBaseModel());
        }
        return historyArrayList;
    }
}

@@ -95,16 +95,73 @@
 dName;%0A%0A
+import net.somethingdreadful.MAL.account.AccountService;%0A
 import n
@@ -283,16 +283,73 @@
 a.Manga;
+%0Aimport net.somethingdreadful.MAL.api.BaseModels.Profile;
 %0A%0Aimport
@@ -2216,30 +2216,31 @@
 del.
-getAnime().setEpisodes
+setValue(String.valueOf
 (get
@@ -2256,24 +2256,73 @@
 tEpisodes())
+);%0A            model.setStatus(%22watched episode%22)
 ;%0A          
@@ -2364,32 +2364,125 @@
 ().getTitle());%0A
+            model.getAnime().setImageUrl(%22http://cdn.myanimelist.net/images/na_series.gif%22);%0A
         %7D else %7B
@@ -2600,30 +2600,31 @@
 del.
-getManga().setChapters
+setValue(String.valueOf
 (get
@@ -2644,16 +2644,62 @@
 pters())
+);%0A            model.setStatus(%22read chapter%22)
 ;%0A      
@@ -2765,53 +2765,410 @@
     
-%7D%0A        model.setCreatedAt(getTimeUpdated()
+    model.getManga().setImageUrl(%22http://cdn.myanimelist.net/images/na_series.gif%22);%0A        %7D%0A        model.setCreatedAt(getTimeUpdated());%0A        model.setActivityType(%22list%22);%0A%0A        // set User%0A        ArrayList%3CProfile%3E users = new ArrayList%3C%3E();%0A        Profile user = new Profile();%0A        user.setUsername(AccountService.getUsername());%0A        users.add(user);%0A        model.setUsers(users
 );%0A 

Fix history crash caused by missing values