//
//  PromptViewPluginTests.swift
//  Astro
//
//  Created by Jake Runzer on 2016-05-16.
//  Copyright © 2016 Mobify Research & Development Inc. All rights reserved.
//

import XCTest
@testable import Astro

class PromptViewPluginTests: AstroTestCase {
    var messageBus: MessageBus!
    var pluginResolver: StubPluginResolver!
    var promptViewPlugin: PromptViewPlugin!

    override func setUp() {
        messageBus = MessageBus()
        pluginResolver = StubPluginResolver()
        promptViewPlugin = PromptViewPlugin(address: "PromptViewPlugin:0", messageBus: messageBus, pluginResolver: pluginResolver, options: nil)
    }

    func testSetPrompt() {
        let prompt = "THIS IS THE PROMPT"
        promptViewPlugin.setPrompt(prompt) {_ in}

        XCTAssertEqual(promptViewPlugin.promptViewController.titleLabel.text, prompt)
    }

    func testSetPlaceholder() {
        let placeholder = "Placeholder text."
        promptViewPlugin.setPlaceholder(placeholder) {_ in}

        XCTAssertEqual(promptViewPlugin.promptViewController.textArea.text, placeholder)
        XCTAssertEqual(promptViewPlugin.promptViewController.textArea.textColor, UIColor.lightGray)
    }

    func testSetSubmitButton() {
        let label = "Submit!!!"
        promptViewPlugin.setSubmitButtonLabel(label) {_ in}

        XCTAssertEqual(promptViewPlugin.promptViewController.submitButton.currentTitle, label)
    }

    func testSetCancelButton() {
        let label = "Cancel!!"
        promptViewPlugin.setCancelButtonLabel(label) {_ in}

        XCTAssertEqual(promptViewPlugin.promptViewController.cancelButton.currentTitle, label)
    }

    func testSubmittingText() {
        let text = "This is some text to submit"

        self.promptViewPlugin.promptViewController.textArea.text = text
        expectAssertionWithDelay { expectation in
            self.promptViewPlugin.show(nil, respond: CapturedRpcMethodResult.generateSuccessCallback { value in
                XCTAssertNotNil(value)
                if let json: JSONObject = value as? JSONObject {
                    AssertEqualType(json["cancelled"], b: false)
                    AssertEqualType(json["text"], b: text)
                } else {
                    XCTFail("Resolved value should be a JSONObject")
                }

                expectation.fulfill()
            })
            self.promptViewPlugin.promptViewController.submitButtonDidTouch()
        }
    }

    func testCancellingText() {
        let text = "This is something I have put in"

        self.promptViewPlugin.promptViewController.textArea.text = text
        expectAssertionWithDelay { expectation in
            self.promptViewPlugin.show(nil, respond: CapturedRpcMethodResult.generateSuccessCallback { value in
                XCTAssertNotNil(value)
                if let json: JSONObject = value as? JSONObject {
                    AssertEqualType(json["cancelled"], b: true)
                } else {
                    XCTFail("Resolved value should be a JSONObject")
                }

                expectation.fulfill()
            })
            self.promptViewPlugin.promptViewController.cancelButtonDidTouch()
        }
    }

}
