/*
 * This file is part of Droidpile: a Mailpile client for Android.
 *
 * You should have recieved a copy of the LICENSE file along with Droidpile.
 * If not: see <https://github.com/maikelwever/droidpile/blob/master/LICENSE>
 */
package org.maikelwever.droidpile.models;

import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;

import net.sf.jfuzzydate.FuzzyDateFormat;
import net.sf.jfuzzydate.FuzzyDateFormatter;

import org.maikelwever.droidpile.helpers.Utilities;

import java.util.Date;
import java.util.Locale;

/**
 */
public class MailpileMessage {
    public String title;
    public String messageId;
    public String snippet;
    public String fromEmail;
    public String fromName;
    public Date datetime;

    public Spanned senderLine;
    public Spanned previewLine;

//    public int messagesInThread;
//    public boolean isFavorited;

    public MailpileMessage(String title, String snippet, String fromEmail, String fromName, String messageId, Date datetime) {
        this.title = title;
        this.snippet = snippet;
        this.messageId = messageId;
        this.fromEmail = fromEmail;
        this.fromName = fromName;
        this.datetime = datetime;
//        this.content = content;
//        this.messagesInThread = messagesInThread;
//        this.isFavorited = isFavorited;
        createLines();
    }

    public MailpileMessage(Bundle b) {
        this.title = b.getString("title");
        this.snippet = b.getString("snippet");
        this.messageId = b.getString("messageId");
        this.fromEmail = b.getString("fromEmail");
        this.fromName = b.getString("fromName");
        this.datetime = new Date(b.getLong("datetime")*1000L);
        createLines();
    }

    public void createLines() {
        FuzzyDateFormatter format = FuzzyDateFormat.getInstance();
        if (this.fromName.length() > 0) {
            this.senderLine = Html.fromHtml("<b>" + this.fromName + "</b> - <i>" +
                    format.formatDistance(this.datetime, Locale.getDefault()) + "</i> - " +
                    this.fromEmail
            );
        } else {
            this.senderLine = Html.fromHtml("<b>(No name)</b>&nbsp;&nbsp;" + this.fromEmail);
        }

        if (this.title.length() > 0) {
            this.previewLine = Html.fromHtml("<b>" + this.title + "</b>&nbsp;&nbsp;" + this.snippet);
        } else {
            this.previewLine = Html.fromHtml("<b>(No subject)</b>&nbsp;&nbsp;" + this.snippet);
        }
    }

    public static MailpileMessage generateRandomMessage() {
        return new MailpileMessage(Utilities.random(), Utilities.random(),
                Utilities.random(), Utilities.random(), "=15W", new Date());
    }

    public Bundle toBundle() {
        Bundle b = new Bundle();
        b.putString("title", this.title);
        b.putString("snippet", this.snippet);
        b.putString("messageId", this.messageId);
        b.putString("fromEmail", this.fromEmail);
        b.putString("fromName", this.fromName);
        b.putLong("datetime", this.datetime.getTime() / 1000L);
        return b;
    }
}
