//
//  SignUpAutocomplete.swift
//  BeamUITests
//
//  Created by Andrii on 28/09/2021.
//

import Foundation
import XCTest

class SignUpAutocompleteTests: BaseTest {
    
    let passwordPopup = PasswordSuggestionPopupTestView()

    let signUpPageURL = MockHTTPWebPages().getMockPageUrl(.signupShortForm)
    let signInPageURL = MockHTTPWebPages().getMockPageUrl(.signinShortForm)
    let signUpViewAccessibility = "Sign Up"
    
    override func setUp() {
        super.setUp()
        uiMenu.invoke(.startMockHttpServer)
    }
    
    func testUseAutogeneratedPassword() {
        testrailId("C1078, C1079")
        mockPage.openMockPage(.signupForm)
        
        step("WHEN I click in the password field"){
            mockPage.getPasswordFieldElement().tapInTheMiddle()
        }
        
        step("THEN I see autogenerated password suggestion pop-up"){
            XCTAssertTrue(passwordPopup.doesTitleExist())
            XCTAssertTrue(passwordPopup.doesDescriptionExist())
        }

        step("WHEN I click Use autogenerated password option"){
            passwordPopup.getUsePasswordButton().clickOnHittable()
        }

        step("THEN I see password fields are populated with chars and password pop-up disappears"){
            XCTAssertGreaterThan(mockPage.getPasswordFieldElement().getStringValue().count, 8)
            XCTAssertGreaterThan(mockPage.getConfirmPasswordFieldElement().getStringValue().count, 8)
            XCTAssertFalse(passwordPopup.doesTitleExist())
        }

        step("WHEN I click Don't Use autogenerated password option"){
            shortcutHelper.shortcutActionInvoke(action: .reloadPage)
            mockPage.getPasswordFieldElement().tapInTheMiddle()
            passwordPopup.getDontUseButton().clickOnHittable()
        }

        step("THEN I see autogenerated password pop-up disappears and password fields are empty"){
            XCTAssertEqual(mockPage.getPasswordFieldElement().getStringValue(), emptyString)
            XCTAssertEqual(mockPage.getConfirmPasswordFieldElement().getStringValue(), emptyString)
            XCTAssertFalse(passwordPopup.doesTitleExist())
        }
    }
    
    func testPasswordSuggestionIsNotDisplayedOnUsernameField() {
        testrailId("C1076")
        mockPage.openMockPage(.signupForm)

        step("WHEN I click on Password field"){
            mockPage.getUsernameFieldElement(title: "Username: ", inView: signUpViewAccessibility).tapInTheMiddle()
        }

        step("THEN no password suggestion appears"){
            XCTAssertFalse(passwordPopup.doesTitleExist())
            XCTAssertEqual(mockPage.getPasswordFieldElement().getStringValue(), emptyString)
        }
    }

    func testPasswordSuggestionIsUnavailableForSignInPages() {
        testrailId("C1077")
        mockPage.openMockPage(.signinForm)
        
        step("WHEN I click on Password field"){
            mockPage.getPasswordFieldElement(false).tapInTheMiddle()
        }
        
        step("THEN no password suggestion appears"){
            XCTAssertFalse(passwordPopup.doesTitleExist())
            XCTAssertEqual(mockPage.getPasswordFieldElement(false).getStringValue(), emptyString)
        }
    }
    
}
