//
//  WebsiteAuthTest.swift
//  BeamUITests
//
//  Created by Andrii on 06.08.2021.
//

import Foundation
import XCTest

class WebsiteAuthTest: BaseTest {
    
    let searchAppWebsite = "rails.beamapp.co"
    let correctLogin = "beam"
    let correctPass = "jiUJDLr>3Dxx"
    let journalView = JournalTestView()
    var appWebsiteView: AppWebsiteTestView?
    let websiteAuthPopupTestView = WebsiteAuthPopupTestView()
    
    func testAuthPopUpView() {
        uiMenu.invoke(.omniboxFillHistory)

        step("Given I open \(searchAppWebsite) link"){
            journalView.openWebsite(searchAppWebsite)
        }
        
        step("Then I see Website Auth Popup with the following elements on it:"){
            XCTAssertTrue(websiteAuthPopupTestView.staticText(WebsiteAuthPopupViewLocators.Labels.titleLabel.accessibilityIdentifier).waitForExistence(timeout: BaseTest.implicitWaitTimeout))
            XCTAssertTrue(websiteAuthPopupTestView.staticText(WebsiteAuthPopupViewLocators.Labels.descriptionLabel.accessibilityIdentifier).exists)
            XCTAssertTrue(websiteAuthPopupTestView.checkBox(WebsiteAuthPopupViewLocators.Checkboxes.savePasswordCheckbox.accessibilityIdentifier).exists)
            XCTAssertTrue(websiteAuthPopupTestView.staticText(WebsiteAuthPopupViewLocators.Buttons.connectButton.accessibilityIdentifier).exists)
            XCTAssertTrue(websiteAuthPopupTestView.staticText(WebsiteAuthPopupViewLocators.Buttons.cancelButton.accessibilityIdentifier).exists)
            XCTAssertTrue(websiteAuthPopupTestView.textField(WebsiteAuthPopupViewLocators.TextFields.loginField.accessibilityIdentifier).exists)
            XCTAssertTrue(websiteAuthPopupTestView.secureTextField(WebsiteAuthPopupViewLocators.TextFields.passwordField.accessibilityIdentifier).exists)
        }
        
        step("When I click Cancel button"){
            appWebsiteView = websiteAuthPopupTestView.cancelAuthentication()
        }
        
        step("Then I see access denied message"){
            XCTAssertTrue(appWebsiteView!.staticText(AppWebsiteViewLocators.Labels.accessDeniedLabel.accessibilityIdentifier).waitForExistence(timeout: BaseTest.implicitWaitTimeout))
        }
    }
    
    func testAuthenticateSuccessfully() {
        uiMenu.invoke(.omniboxFillHistory)
        step("Given I open \(searchAppWebsite) link"){
            journalView.openWebsite(searchAppWebsite)
        }

        step("When I populate auth pop-up with correct credentials"){
            appWebsiteView = websiteAuthPopupTestView.authenticate(correctLogin, correctPass)
        }
        
        step("Then I'm successfully redirected to the website and pop-up is closed"){
            XCTAssertTrue(appWebsiteView!.staticText("Beam Objects").waitForExistence(timeout: BaseTest.implicitWaitTimeout) || appWebsiteView!.staticText("Beam is where ideas take shape").waitForExistence(timeout: BaseTest.implicitWaitTimeout))
        }

    }
    
    func SKIPtestAuthenticationFailure() throws {
        try XCTSkipIf(true, "Skipped due to unknown false failure on server side. To be fixed soon")
        uiMenu.invoke(.omniboxFillHistory)
        
        step("Given I open \(searchAppWebsite) link"){
            journalView.openWebsite(searchAppWebsite)
        }
        
        step("When I populate auth pop-up with correct credentials"){
            appWebsiteView = websiteAuthPopupTestView.authenticate("somelogin", "somepass")
        }
        
        step("Then I see access denied message"){
            XCTAssertTrue(appWebsiteView!.staticText(AppWebsiteViewLocators.Labels.accessDeniedLabel.accessibilityIdentifier).waitForExistence(timeout: BaseTest.implicitWaitTimeout))
        }
    }
    
}
