package com.jamesdube.laravelnewsapp.util;

import android.util.Log;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.jamesdube.laravelnewsapp.App;
import com.jamesdube.laravelnewsapp.models.Author;
import com.jamesdube.laravelnewsapp.models.Category;
import com.jamesdube.laravelnewsapp.models.Post;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.realm.RealmList;

import static com.jamesdube.laravelnewsapp.util.CustomDateSerializer.dateFormat;


public class PostDeserializer implements JsonDeserializer<Post> {

    private Date pubDate = null;
    private JsonElement json;
    private String title;
    private String link;
    private String description;
    private String coverImage;
    private RealmList<Author> authors = new RealmList<>();
    private RealmList<Category> categories = new RealmList<>();

    public static final String CATEGORY = "category";
    public static final String LINK = "link";
    public static final String TITLE = "title";
    public static final String AUTHOR = "dc:creator";
    public static final String DESCRIPTION = "description";


    @Override
    public Post deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

        this.json = json;
        Post post = new Post();
        authors = new RealmList<>();
        categories = new RealmList<>();



        //if there is one category
        parseCategory().parseAuthor()
                .parseDate()
                .parseTitle()
                .parseLink()
                .parseDescription()
                .parseCoverImage();

        post.setPubDate(pubDate);
        post.setTitle(title);
        post.setLink(link);
        post.setCategories(categories);
        post.setAuthors(authors);
        post.setCoverImage(coverImage);
        post.setDescription(prepHtml());

        post.setActive(true);
        post.setSeen(false);
        

        return post;


    }

    private PostDeserializer parseCategory() {

        if(json.getAsJsonObject().get(CATEGORY).isJsonPrimitive()){
            String name = json.getAsJsonObject().get(CATEGORY).getAsString();
            categories.add(new Category(name));
        }else if(json.getAsJsonObject().get(CATEGORY).isJsonArray()){
            for(int i = 0; i < json.getAsJsonObject().get(CATEGORY).getAsJsonArray().size(); i ++){
                categories.add(new Category(json.getAsJsonObject()
                        .get(CATEGORY)
                        .getAsJsonArray()
                        .get(i)
                        .toString()));
            }
        }

        return this;
    }

    private PostDeserializer parseAuthor() {
        if(json.getAsJsonObject().get(AUTHOR).isJsonPrimitive()){
            String name = json.getAsJsonObject().get(AUTHOR).getAsString();
            authors.add(new Author(name));
        }else if(json.getAsJsonObject().get(AUTHOR).isJsonArray()){
            for(int i = 0; i < json.getAsJsonObject().get(AUTHOR).getAsJsonArray().size(); i ++){
                authors.add(new Author(json.getAsJsonObject()
                        .get(AUTHOR)
                        .getAsJsonArray()
                        .get(i)
                        .toString()));
            }
        }

        return this;
    }

    private PostDeserializer parseDate() {
        try {
            pubDate = dateFormat.parse(json
                    .getAsJsonObject()
                    .get("pubDate")
                    .getAsString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return this;
    }

    private PostDeserializer parseLink() {

        if (json.getAsJsonObject().get(LINK).isJsonPrimitive()) {
            link = json.getAsJsonObject().get(LINK).getAsString();
        }

        return this;
    }

    private PostDeserializer parseTitle() {

        if (json.getAsJsonObject().get(TITLE).isJsonPrimitive()) {
            title = json.getAsJsonObject().get(TITLE).getAsString();
        }

        return this;
    }

    private PostDeserializer parseDescription() {

        if (json.getAsJsonObject().get(DESCRIPTION).isJsonPrimitive()) {
            description = json.getAsJsonObject().get(DESCRIPTION).getAsString();
        }

        return this;
    }
    
    private PostDeserializer parseCoverImage(){
        
        if(description != null){
            Pattern pattern = Pattern.compile("<img src=\"(.*?)\">");
            Matcher matcher = pattern.matcher(description);
            if(matcher.find()){
                coverImage = matcher.group(1);
            }
        }

        return this;
        
    }

    private String prepHtml(){
        return "<HTML><HEAD><link href=\"style.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>" +
                "<div class=\"container-fluid\"><div class=\"row\"> <div class=\"col-lg-12\">" +
                description.replaceFirst("<img([^<]*)>", "") +
                " </div></div></div></body></HTML>";
    }
}

@@ -1147,29 +1147,30 @@
 %3C%3E();%0A%0A    p
-ublic
+rivate
  static fina
@@ -1198,37 +1198,38 @@
 category%22;%0A    p
-ublic
+rivate
  static final St
@@ -1245,37 +1245,38 @@
  = %22link%22;%0A    p
-ublic
+rivate
  static final St
@@ -1294,37 +1294,38 @@
 = %22title%22;%0A    p
-ublic
+rivate
  static final St
@@ -1357,21 +1357,22 @@
 %22;%0A    p
-ublic
+rivate
  static 
@@ -1413,16 +1413,76 @@
 ption%22;%0A
+    private static final String COVER_IMAGE = %22coverImage%22;%0A
 %0A%0A    @O
@@ -4730,16 +4730,180 @@
 Image()%7B
+%0A%0A        if (json.getAsJsonObject().get(COVER_IMAGE).isJsonPrimitive()) %7B%0A            coverImage = json.getAsJsonObject().get(COVER_IMAGE).getAsString();%0A        %7D
 %0A       
@@ -4912,16 +4912,21 @@
         
+else 
 if(descr

include cover Image parsing of existing post