package com.avg.zaap.crashlibrary.offline.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.avg.zaap.crashlibrary.ConnectionStatus;
import com.avg.zaap.crashlibrary.offline.IConnectivityHelper;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * A test class for {@link CrashReportingServiceBroadcastReceiver}.
 * <p/>
 * Created by marek.osvald on 7.9.2016.
 */
@SuppressWarnings("SameParameterValue")
public class CrashReportingServiceBroadcastReceiverTests {

    private BroadcastReceiver mCrashReportingServiceBroadcastReceiver;
    private ICrashReportingServiceBroadcastReceiverCallback mCallback;
    private Context mContext;
    private Intent mIntent;
    private IConnectivityHelper mHelper;
    private ConnectionStatus mConnectionStatus;
    private ConnectionStatus mDisconnectedConnectionStatus;

    @Before
    public void setUp() throws Exception {
        mCallback = mock(ICrashReportingServiceBroadcastReceiverCallback.class);
        mContext = mock(Context.class);
        mIntent = mock(Intent.class);
        mConnectionStatus = mock(ConnectionStatus.class);
        mDisconnectedConnectionStatus = mock(ConnectionStatus.class);
        mHelper = mock(IConnectivityHelper.class);

        mCrashReportingServiceBroadcastReceiver = new CrashReportingServiceBroadcastReceiver(mCallback, new ICrashReportingServiceBroadcastReceiverCallbackFactory() {

            @Override
            public IConnectivityHelper getConnectivityHelper(Context context) {
                return mHelper;
            }

        });

        initializeIntent(Result.OK);
        initializeHelper(Result.OK);
    }

    @Test
    public void onReceive_connectionOK_callsCallback() throws Exception {
        // act
        mCrashReportingServiceBroadcastReceiver.onReceive(mContext, mIntent);

        // assert
        verify(mCallback, times(1)).onConnectionChange(mConnectionStatus);
    }

    @Test
    public void onReceive_connectionDisconnected_callsCallback() throws Exception {
        // arrange
        initializeHelper(Result.NULL);

        // act
        mCrashReportingServiceBroadcastReceiver.onReceive(mContext, mIntent);

        // assert
        verify(mCallback, times(1)).onConnectionChange(mDisconnectedConnectionStatus);
    }

    private void initializeIntent(Result result) {
        switch (result) {
            case OK:
                break;

            case KEY_NOT_PRESENT:
                break;

            case NULL:
                break;

            default:
                fail();
        }
    }

    private void initializeHelper(Result result) {
        switch (result) {
            case OK:
                when(mHelper.getConnectionStatus()).thenReturn(mConnectionStatus);
                break;

            case NULL:
                when(mHelper.getConnectionStatus()).thenReturn(mDisconnectedConnectionStatus);
                break;

            default:
                fail();
        }
    }

    enum Result {
        NULL, KEY_NOT_PRESENT, OK
    }

}
