<commit_before>//
//  ScriptHandler.swift
//  AtoneCon
//
//  Created by <NAME> on 7/6/17.
//  Copyright © 2017 AsianTech Inc. All rights reserved.
//

import Foundation
import WebKit

internal protocol ScriptHandlerDelegate: class {
    func scriptHandler(_ scriptHandler: ScriptHandler, didReceiveScriptEvent event: ScriptEvent)
}

private enum Message: String {
    case authenticated
    case canceled
    case succeeded
    case failed

    var name: String {
        return rawValue
    }
}

internal enum ScriptEvent {
    case authenticated(String?)
    case canceled
    case succeeded(Any?)
    case failed(Any?)

    fileprivate var messageName: Message {
        switch self {
        case .authenticated(_):
            return .authenticated
        case .canceled:
            return .canceled
        case .failed(_):
            return .failed
        case .succeeded(_):
            return .succeeded
        }
    }
}

internal final class ScriptHandler: NSObject {

    private var webView: WKWebView!
    internal weak var delegate: ScriptHandlerDelegate?
    private let messages: [Message] = [.authenticated, .canceled, .failed, .succeeded]

    internal init(forWebView webView: WKWebView) {
        self.webView = webView
    }

    internal func addEvents() {
        let controller = webView.configuration.userContentController
        for message in messages {
            controller.add(self, name: message.name)
        }
    }
}

extension ScriptHandler: WKScriptMessageHandler {
    internal func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        let event: ScriptEvent
        guard let messageName = Message(rawValue: message.name) else {
            fatalError("")
        }
        switch messageName {
        case .authenticated :
            // TODO: save authentoken
            event = ScriptEvent.authenticated(message.body as? String)
        case .canceled:
            // TODO: get respone
            event = ScriptEvent.canceled
        case .failed:<commit_msg>add comment in fatalError when receive mesage<commit_after>//
//  ScriptHandler.swift
//  AtoneCon
//
//  Created by <NAME> on 7/6/17.
//  Copyright © 2017 AsianTech Inc. All rights reserved.
//

import Foundation
import WebKit

internal protocol ScriptHandlerDelegate: class {
    func scriptHandler(_ scriptHandler: ScriptHandler, didReceiveScriptEvent event: ScriptEvent)
}

private enum Message: String {
    case authenticated
    case canceled
    case succeeded
    case failed

    var name: String {
        return rawValue
    }
}

internal enum ScriptEvent {
    case authenticated(String?)
    case canceled
    case succeeded(Any?)
    case failed(Any?)

    fileprivate var messageName: Message {
        switch self {
        case .authenticated(_):
            return .authenticated
        case .canceled:
            return .canceled
        case .failed(_):
            return .failed
        case .succeeded(_):
            return .succeeded
        }
    }
}

internal final class ScriptHandler: NSObject {

    private var webView: WKWebView!
    internal weak var delegate: ScriptHandlerDelegate?
    private let messages: [Message] = [.authenticated, .canceled, .failed, .succeeded]

    internal init(forWebView webView: WKWebView) {
        self.webView = webView
    }

    internal func addEvents() {
        let controller = webView.configuration.userContentController
        for message in messages {
            controller.add(self, name: message.name)
        }
    }
}

extension ScriptHandler: WKScriptMessageHandler {
    internal func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        let event: ScriptEvent
        guard let messageName = Message(rawValue: message.name) else {
            fatalError("no handle for event \(message.name)")
        }
        switch messageName {
        case .authenticated :
            // TODO: save authentoken
            event = ScriptEvent.authenticated(message.body as? String)
        case .canceled:
            // TODO: get respone
            event = ScriptEvent.canceled
        case .failed:
{'fraction_non_alphanumeric': 0.07533759772565743, 'fraction_numerical': 0.0037905709547500594, 'mean_word_length': 2.407586763518967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5983724', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1085, 'n_words': 335}