package com.dsbeckham.nasaimageryfetcher.util;

import com.dsbeckham.nasaimageryfetcher.model.ApodMorphIoModel;
import com.dsbeckham.nasaimageryfetcher.model.ApodNasaGovModel;
import com.dsbeckham.nasaimageryfetcher.model.IotdRssModel;
import com.dsbeckham.nasaimageryfetcher.model.UniversalImageModel;

public class ModelUtils {
    public static final String MODEL_TYPE_IOTD = "iotd";
    public static final String MODEL_TYPE_APOD = "apod";

    public static UniversalImageModel convertIotdRssModelChannelItem(IotdRssModel.Channel.Item iotdRssModelChannelItem) {
        UniversalImageModel universalImageModel = new UniversalImageModel();
        universalImageModel.setDate(DateUtils.convertDateToCustomDateFormat(iotdRssModelChannelItem.getPubDate(), "EEE, dd MMM yyyy HH:mm zzz", "yyyy-MM-dd"));
        universalImageModel.setDescription(iotdRssModelChannelItem.getDescription());
        universalImageModel.setImageThumbnailUrl(iotdRssModelChannelItem.getEnclosure().getUrl());
        // The RSS feed always links to the image thumbnail URL. The full image URL is a few directories down.
        universalImageModel.setImageUrl(iotdRssModelChannelItem.getEnclosure().getUrl().replace("styles/full_width_feature/public/", ""));
        universalImageModel.setPageUrl(iotdRssModelChannelItem.getLink());
        universalImageModel.setTitle(iotdRssModelChannelItem.getTitle());
        universalImageModel.setType(MODEL_TYPE_IOTD);
        return universalImageModel;
    }

    public static UniversalImageModel convertApodMorphIoModel(ApodMorphIoModel apodMorphIoModel) {
        UniversalImageModel universalImageModel = new UniversalImageModel();
        universalImageModel.setCredit(apodMorphIoModel.getCredit());
        universalImageModel.setDate(apodMorphIoModel.getDate());
        universalImageModel.setDescription(apodMorphIoModel.getExplanation());
        universalImageModel.setImageThumbnailUrl(apodMorphIoModel.getPictureThumbnailUrl());
        universalImageModel.setImageUrl(apodMorphIoModel.getPictureUrl());
        universalImageModel.setPageUrl(apodMorphIoModel.getUrl());
        universalImageModel.setTitle(apodMorphIoModel.getTitle());
        universalImageModel.setType(MODEL_TYPE_APOD);
        return universalImageModel;
    }

    public static UniversalImageModel convertApodNasaGovModel(ApodNasaGovModel apodNasaGovModel) {
        UniversalImageModel universalImageModel = new UniversalImageModel();
        universalImageModel.setCredit(apodNasaGovModel.getCopyright());
        universalImageModel.setDate(apodNasaGovModel.getDate());
        universalImageModel.setDescription(apodNasaGovModel.getExplanation());
        universalImageModel.setImageThumbnailUrl(apodNasaGovModel.getMediaType().equals("image") ? apodNasaGovModel.getUrl(): "");
        universalImageModel.setImageUrl(apodNasaGovModel.getMediaType().equals("image") ? apodNasaGovModel.getHdUrl() : "");
        // The NASA API doesn't include the page URL, but it can be obtained from the date of the image.
        universalImageModel.setPageUrl(String.format("http://apod.nasa.gov/apod/ap%1$s.html", DateUtils.convertDateToCustomDateFormat(apodNasaGovModel.getDate(), "yyyy-MM-dd", "yyMMdd")));
        universalImageModel.setTitle(apodNasaGovModel.getTitle());
        universalImageModel.setType(MODEL_TYPE_APOD);
        return universalImageModel;
    }
}

@@ -915,33 +915,24 @@
 del.setImage
-Thumbnail
 Url(iotdRssM
@@ -1021,22 +1021,11 @@
 the 
-image thumbnai
+ful
 l UR
@@ -1035,18 +1035,17 @@
 The 
-full image
+thumbnail
  URL
@@ -1054,11 +1054,17 @@
 s a 
-few
+different
  dir
@@ -1072,16 +1072,9 @@
 ctor
-ies down
+y
 .%0A  
@@ -1099,32 +1099,41 @@
 geModel.setImage
+Thumbnail
 Url(iotdRssModel
@@ -1177,16 +1177,43 @@
 eplace(%22
+files/thumbnails/%22, %22files/
 styles/f
@@ -1237,20 +1237,27 @@
 /public/
-%22, %22
+thumbnails/
 %22));%0A   

Fix RSS thumbnail and full size image locations