package blogr.vpm.fr.blogr.publish;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import blogr.vpm.fr.blogr.bean.Blog;
import blogr.vpm.fr.blogr.bean.Post;

/**
 * Created by vince on 16/10/14.
 */
public class StdEmailPostPublisher implements PostPublisher {

    private final Context context;

    private Formatter formatter;

    public StdEmailPostPublisher(Context context) {
        this.context = context;
        this.formatter = new HtmlFormatter();
    }


    @Override
    public void publish(Blog blog, Post post) {
        Intent intent;
        if (!post.getPicturesAsMediaContent().isEmpty()) {
            intent = emailIntentWithAttachments(blog, post);
        } else {
            intent = emailIntentWithoutAttachments(blog, post);
        }
        context.startActivity(intent);
    }

    private Intent emailIntentWithAttachments(Blog blog, Post post) {
        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{blog.getEmailAddress()});
        intent.putExtra(Intent.EXTRA_SUBJECT, post.getTitle());
        intent.putExtra(Intent.EXTRA_TEXT, this.formatter.format(post.getContent()));
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, post.getPicturesAsFiles(context));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    public Intent emailIntentWithoutAttachments(Blog blog, Post post) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, post.getTitle());
        intent.putExtra(Intent.EXTRA_TEXT, this.formatter.format(post.getContent()));
        intent.setData(Uri.parse("mailto:" + blog.getEmailAddress()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    @Override
    public void setFormatter(Formatter formatter) {

    }
}

@@ -116,16 +116,42 @@
 net.Uri;
+%0Aimport android.text.Html;
 %0A%0Aimport
@@ -1185,32 +1185,103 @@
 st.getTitle());%0A
+        String htmlContent = this.formatter.format(post.getContent());%0A
         intent.p
@@ -1311,47 +1311,33 @@
 XT, 
-this.formatter.format(post.get
+Html.fromHtml(html
 Content
-()
 ));%0A
@@ -1745,32 +1745,103 @@
 st.getTitle());%0A
+        String htmlContent = this.formatter.format(post.getContent());%0A
         intent.p
@@ -1871,47 +1871,33 @@
 XT, 
-this.formatter.format(post.get
+Html.fromHtml(html
 Content
-()
 ));%0A

format email as html