//
//  TestWebClientDelegate.swift
//  Astro
//
//  Created by Kerr Miller on 2016-11-10.
//  Copyright © 2016 Mobify Research & Development Inc. All rights reserved.
//

import WebKit
@testable import Astro

class TestWebClientDelegate: WebClientDelegate {
    var numberOfPageDidStartLoadingCalls = 0
    var numberOfPageLoads = 0
    var navigationEvents = [NavigationEvent]()

    var didFinishLoadingClosure: (() -> Void)?
    var didFailToLoadClosure: (() -> Void)?
    var handleNavigationClosure: (() -> Void)?

    func handleNavigationRequest(_ request: URLRequest) {
        navigationEvents.append(.success(request.url!))
        handleNavigationClosure?()
    }

    func pageDidStartLoading(_ url: URL) {
        numberOfPageDidStartLoadingCalls += 1
    }

    func pageDidFinishLoading() {
        numberOfPageLoads += 1
        didFinishLoadingClosure?()
    }

    func notifyAppStoreLinkOpened() {
    }

    func notifyNoInternetConnection(_ url: String) {
    }

    func showAlertPanelWithMessage(_ message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
    }

    func showConfirmPanelWithMessage(_ message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
    }

    func showTextInputPanelWithPrompt(_ prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
    }

    func pageDidFailLoadWithError(_ error: NSError) {
        numberOfPageLoads += 1
        if let url = error.userInfo[NSURLErrorFailingURLErrorKey] as? URL {
            navigationEvents.append(.failure(url))
        }
        didFailToLoadClosure?()
    }
}
