//
//  Copyright (c) 2015 Mobify Research & Development Inc. All rights reserved.
//

import Foundation

public class AlertViewPlugin: Plugin {

    @objc private(set) var title = ""
    @objc private(set) var text = ""

    @objc private(set) var shouldShowOKButton = false
    @objc private(set) var okButtonLocalizationKey = "alert_ok"

    @objc private(set) var shouldShowCancelButton = false
    @objc private(set) var cancelButtonLocalizationKey = "alert_cancel"

    public required init(address: MessageAddress, messageBus: MessageBus, pluginResolver: PluginResolver, options: JSONObject?) {
        super.init(address: address, messageBus: messageBus, pluginResolver: pluginResolver, options: options)

        // Rpc Methods should be automated
        self.addRpcMethodShim("setTitle") { params, respond in
            if let title: String = MethodShimUtils.getArg(params, key: "title", respond: respond) {
                self.setTitle(title, respond: respond)
            }
        }

        self.addRpcMethodShim("setText") { params, respond in
            if let text: String = MethodShimUtils.getArg(params, key: "text", respond: respond) {
                self.setText(text, respond: respond)
            }
        }

        self.addRpcMethodShim("addOkButton") { params, respond in
            if let label: String? = MethodShimUtils.getOptionalArg(params, key: "label", respond: respond) {
                self.setOKButtonTitleKey(label, respond: respond)
            }
        }

        self.addRpcMethodShim("addCancelButton") { params, respond in
            if let label: String? = MethodShimUtils.getOptionalArg(params, key: "label", respond: respond) {
                self.setCancelButtonTitleKey(label, respond: respond)
            }
        }

        self.addRpcMethodShim("removeOkButton") { _, respond in
            self.removeOkButton(respond: respond)
        }

        self.addRpcMethodShim("removeCancelButton") { _, respond in
            self.removeCancelButton(respond: respond)
        }

        self.addAsyncRpcMethodShim("show") { params, respond in
            if let options: JSONObject? = MethodShimUtils.getOptionalArg(params, key: "options", respond: respond) {
                self.show(options, respond: respond)
            }
        }
    }

    func setTitle(_ title: String, respond: RPCMethodCallback) {
        self.title = title
    }

    func setText(_ text: String, respond: RPCMethodCallback) {
        self.text = text
    }

    // @RpcMethod
    func setOKButtonTitleKey(_ label: String?, respond: RPCMethodCallback) {
        if let label = label {
            okButtonLocalizationKey = label
        }
        shouldShowOKButton = true
    }

    // @RpcMethod
    func setCancelButtonTitleKey(_ label: String?, respond: RPCMethodCallback) {
        if let label = label {
            cancelButtonLocalizationKey = label
        }
        shouldShowCancelButton = true
    }

    // @RpcMethod
    func removeOkButton(respond: RPCMethodCallback) {
        shouldShowOKButton = false
        okButtonLocalizationKey = "alert_ok"
    }

    // @RpcMethod
    func removeCancelButton(respond: RPCMethodCallback) {
        shouldShowCancelButton = false
        cancelButtonLocalizationKey = "alert_cancel"
    }

    // @AsyncRpcMethod
    func show(_ options: JSONObject?, respond: @escaping RPCMethodCallback) {

        let alertViewController = UIAlertController(title: Localization.translate(title), message: Localization.translate(text), preferredStyle: .alert)

        // Create actions for ok and cancel buttons
        if shouldShowOKButton {
            let okAction = UIAlertAction(title: Localization.translate(okButtonLocalizationKey), style: .default) {_ in
                respond(.result(true))
            }
            alertViewController.addAction(okAction)
        }
        if shouldShowCancelButton {
            let cancelAction = UIAlertAction(title: Localization.translate(cancelButtonLocalizationKey), style: .cancel) {_ in
                respond(.result(false))
            }
            alertViewController.addAction(cancelAction)
        }

        // Show blocking alert view
        if let currentViewController = UIApplication.shared.currentViewController {
            currentViewController.present(alertViewController, animated: true)
        }
    }
}
