package com.tuvistavie.meetup.event.model;

import com.tuvistavie.meetup.contact.model.Contact;
import com.tuvistavie.meetup.contact.model.ContactList;
import com.tuvistavie.meetup.model.AbstractEntity;
import com.tuvistavie.meetup.util.DateTimeUtil;
import com.tuvistavie.meetup.util.Routes;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by daniel on 8/31/13.
 */
public class Event extends AbstractEntity {
    protected String name;
    protected String description;
    protected EventDateCollection eventDateCollection;
    protected ContactList participants;

    protected Participant creator;

    public Event() {
        super();
        this.remoteUri = Routes.EVENTS.getRoute();
    }

    public Event(String name, String description, EventDateCollection eventDateCollection, ContactList participants) {
        this();
        this.name = name;
        this.description = description;
        this.eventDateCollection = eventDateCollection;
        this.participants = participants;
    }

    public Event(JSONObject jsonObject) {
        super(jsonObject);
        this.remoteUri = Routes.EVENTS.getRoute();
    }

    public Event(int id) {
        super(id);
        this.remoteUri = Routes.EVENTS.getRoute();
    }

    @Override
    public void fromJSON(JSONObject jsonObject) {
        try {
            id = jsonObject.getInt("id");
            name = jsonObject.getString("name");
            description = jsonObject.getString("description");
            creator = new Participant(jsonObject.getJSONObject("creator"));
            this.eventDateCollection = new EventDateCollection();
            eventDateCollection.fromJSON(jsonObject.getJSONArray("event_dates"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonEvent = new JSONObject();
        try {
            jsonEvent.put("name", name);
            jsonEvent.put("description", description);
            jsonEvent.put("event_dates_attributes", eventDateCollection.toJSONArray());
            JSONArray users = new JSONArray();
            for(Contact contact: participants.getEntities()) {
                List<String> emails = contact.getEmails();
                if(!emails.isEmpty()) {
                    users.put(emails.get(0));
                }
            }
            jsonEvent.put("users", users);
            return jsonEvent;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public Participant getCreator() {
        return creator;
    }

    public String getStartDateString() {
        if(eventDateCollection.getEntities().isEmpty()) {
            return "";
        }
        EventDate e = eventDateCollection.getEntities().get(0);
        return DateTimeUtil.formatDate(eventDateCollection.getEntities().get(0).getStartDateTime());
    }

    public String getEndDateString() {
        if(eventDateCollection.getEntities().isEmpty()) {
            return "";
        }
        return DateTimeUtil.formatDate(eventDateCollection.getEntities().get(0).getEndDateTime());
    }
}

@@ -374,16 +374,61 @@
 bject;%0A%0A
+import java.io.UnsupportedEncodingException;%0A
 import j
@@ -1493,16 +1493,27 @@
  name = 
+new String(
 jsonObje
@@ -1532,16 +1532,49 @@
 (%22name%22)
+.getBytes(%22ISO-8859-1%22), %22UTF-8%22)
 ;%0A      
@@ -1593,16 +1593,27 @@
 ption = 
+new String(
 jsonObje
@@ -1639,16 +1639,49 @@
 iption%22)
+.getBytes(%22ISO-8859-1%22), %22UTF-8%22)
 ;%0A      
@@ -1894,24 +1894,24 @@
 t_dates%22));%0A
-
         %7D ca
@@ -1959,32 +1959,116 @@
 ntStackTrace();%0A
+        %7D catch (UnsupportedEncodingException e) %7B%0A            e.printStackTrace();%0A
         %7D%0A    %7D%0A

Fix event encoding.