package com.mobify.astro.helpers;

import android.webkit.WebBackForwardList;
import android.webkit.WebHistoryItem;

import com.mobify.astro.plugins.webviewplugin.AstroWebView;

import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MockAstroWebViewFactory {

    private MockAstroWebViewFactory() {};

    public static AstroWebView makeCleanWebView() {
        return mock(AstroWebView.class);
    }
    public static String mockUrl = "http://www.mobify.com";


    /**
     * Mocks an astro web view with a mock WebBackForwardList of the given length.
     * The WebBackForwardList has "http://www.mobify.com" as every entry.
     * @param historyLength the length of the mock WebBackForwardList
     * @return a mocked AstroWebView
     */
    public static AstroWebView makeWebViewWithHistoryLength(int historyLength) {
        AstroWebView webView = mock(AstroWebView.class);

        when(webView.getUrl()).thenReturn(MockAstroWebViewFactory.mockUrl);

        WebBackForwardList mockHistory = mock(WebBackForwardList.class);
        WebHistoryItem mockHistoryItem = mock(WebHistoryItem.class);

        when(webView.copyBackForwardList()).thenReturn(mockHistory);

        when(mockHistory.getSize()).thenReturn(historyLength);
        when(mockHistory.getItemAtIndex(anyInt())).thenReturn(mockHistoryItem);

        when(mockHistoryItem.getUrl()).thenReturn(MockAstroWebViewFactory.mockUrl);

        return webView;
    }
}
