package com.mobify.astro.plugins;

import android.content.Intent;

import com.mobify.astro.AstroActivity;
import com.mobify.astro.PluginResolver;
import com.mobify.astro.messaging.EventManager;
import com.mobify.astro.messaging.MessageSender;

import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class SharingPluginTest {
    private SharingPlugin sharingPlugin;
    private AstroActivity mockActivity;

    final String testMessage = "some message";
    final String testUrl = "http://coolurl.com";
    final String emptyString = "";
    final String testSharingText = "some text to share";
    final double testNumber = 12345.12;

    @Before
    public void setup() {
        // Spying on the AstroActivity does work but leaves the chooser
        // dialog open on the test app
        // This hangs the tests next time they are run
        mockActivity = mock(AstroActivity.class);

        PluginResolver mockPluginResolver = mock(PluginResolver.class);
        EventManager mockEventManager = mock(EventManager.class);
        MessageSender mockMessageSender = mock(MessageSender.class);
        sharingPlugin = spy(new SharingPlugin(mockActivity, mockPluginResolver, mockEventManager, mockMessageSender));

        // Need to mock the getChooserTitle method so that the mock
        // activity isn't accessed when looking for the string resource
        doReturn("Share").when(sharingPlugin).getChooserTitle();
    }

    @Test
    public void testShow() throws Exception {
        JSONObject options = new JSONObject();
        options.put("message", testMessage);
        options.put("url", testUrl);

        sharingPlugin.share(options);

        ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
        verify(mockActivity, times(1)).startActivity(intentArgumentCaptor.capture());

        Intent chooserIntent = intentArgumentCaptor.getValue();
        assertEquals(Intent.ACTION_CHOOSER, chooserIntent.getAction());

        Intent sharingIntent = chooserIntent.getParcelableExtra(Intent.EXTRA_INTENT);
        assertEquals(Intent.ACTION_SEND, sharingIntent.getAction());
        assertEquals(testMessage + " " + testUrl, sharingIntent.getStringExtra(Intent.EXTRA_TEXT));
        assertEquals("text/plain", sharingIntent.getType());
    }

    @Test
    public void testShowEmptyOptions() throws Exception {
        JSONObject options = new JSONObject();

        sharingPlugin.share(options);

        ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
        verify(mockActivity, never()).startActivity(intentArgumentCaptor.capture());
    }

    @Test
    public void testRetrieveMessageOption() throws Exception{
        JSONObject options = new JSONObject();
        options.put("message", testMessage);

        String message = SharingPlugin.retrieveOption(options, SharingPlugin.messageKey);
        assertEquals(testMessage, message);
    }

    @Test
    public void testRetrieveUrlOption() throws Exception{
        JSONObject options = new JSONObject();
        options.put("url", testUrl);

        String url = SharingPlugin.retrieveOption(options, SharingPlugin.urlKey);
        assertEquals(testUrl, url);
    }

    @Test
    public void testRetrieveUrlOptionNoUrl() throws Exception{
        JSONObject options = new JSONObject();

        String url = SharingPlugin.retrieveOption(options, SharingPlugin.urlKey);
        assertEquals(emptyString, url);
    }

    @Test
    public void testRetrieveUrlOptionNumber() throws Exception{
        JSONObject options = new JSONObject();
        options.put("url", testNumber);

        String url = SharingPlugin.retrieveOption(options, SharingPlugin.urlKey);
        assertEquals(testNumber + "", url);
    }

    @Test
    public void testCreateSharingIntent() {
        Intent intent = SharingPlugin.createSharingIntent(testSharingText);
        assertEquals(Intent.ACTION_SEND, intent.getAction());
        assertEquals(testSharingText, intent.getStringExtra(Intent.EXTRA_TEXT));
        assertEquals("text/plain", intent.getType());
    }
}
