//
//  ComposeTests.swift
//  TuskerUITests
//
//  Created by Shadowfacts on 9/12/20.
//  Copyright © 2020 Shadowfacts. All rights reserved.
//

import XCTest
import Pachyderm

class ComposeTests: TuskerUITests {

    override func setUp() {
        super.setUp()
        
//        router.allRoutes()
        
        app.launchEnvironment["UI_TESTING_LOGIN"] = "true"
        app.launch()
        
        app.tabBars.buttons["Compose"].tap()
    }
    
    func testCurrentAccount() {
        XCTAssertTrue(app.images["Admin Account avatar"].exists, "avatar image exists")
        XCTAssertTrue(app.staticTexts["Admin Account"].exists, "display name label exists")
        XCTAssertTrue(app.staticTexts["@admin"].exists, "acct label exists")
    }
    
    func testBodyPlaceholder() {
        XCTAssertTrue(app.staticTexts["What's on your mind?"].exists, "placeholder exists")
        app.textViews.firstMatch.typeText("Blah")
        XCTAssertFalse(app.staticTexts["What's on your mind?"].exists, "placeholder does not exist")
    }
    
//    func testCharacterCounter() {
//        XCTAssertTrue(app.staticTexts["500 characters remaining"].exists, "initial character count is 500")
//        let textView = app.textViews.firstMatch
//        
//        let fragments = [
//            "Hello",
//            "World",
//            "@admin",
//            "@admin@example.com",
//            "https://foo.example.com/?bar=baz#qux",
//        ]
//        
//        var remaining = 500
//        for s in fragments {
//            let length = CharacterCounter.count(text: s)
//            // add 1 for newline
//            remaining -= length + 1
//                
//            textView.typeText(s + "\n")
//            XCTAssertTrue(app.staticTexts["\(remaining) characters remaining"].exists, "subsequent character count is \(remaining)")
//        }
//    }
    
//    func testToolbarSwitching() {
//        // text view is automatically focused, so unfocus
//        app.swipeDown()
//
//        XCTAssertEqual(app.toolbars.count, 1)
//        XCTAssertEqual(app.toolbars.buttons.count, 3)
//        XCTAssertTrue(app.toolbars.buttons["CW"].exists)
//        XCTAssertTrue(app.toolbars.buttons["Visibility: Public"].exists)
//        XCTAssertTrue(app.toolbars.buttons["Drafts"].exists)
//
//    }
    
    func testContentWarning() {
        let toolbar = app.toolbars.firstMatch
        XCTAssertTrue(toolbar.waitForExistence(timeout: 0.1))
        XCTAssertEqual(app.toolbars.count, 1, "there is only 1 toolbar")
        let cwButton = toolbar.buttons["CW"]
        XCTAssertTrue(cwButton.exists, "the CW button exists")
        
        cwButton.tap()
        let cwField = app.textFields.firstMatch
        XCTAssertEqual(cwField.placeholderValue, "Write your warning here")
        
        XCTAssertTrue(app.staticTexts["500 characters remaining"].exists)
        cwField.tap()
        // on iOS 14, the first type text is typed into a SwiftUI TextField by a test has the 1st character is inexplicably dropped >.<
        let str = "fooo"
        cwField.typeText(str)
        XCTAssertTrue(app.staticTexts["497 characters remaining"].exists)
        // CharacterCounter is not used => '@' is counted
        cwField.typeText(" @bar")
        XCTAssertTrue(app.staticTexts["492 characters remaining"].exists)
    }

}
