//
//  HealthKitAccessPage.swift 
//  Pi 
//  Copyright © 2021 Evidation Health Inc. All rights reserved.
//
//  Contains the variables and methods for health kit access page

import XCTest

class HealthKitAccessPage: BasePage {
    /*
     * Health kit page constants
     */
    let HEALTH_KIT_ACCESS_TEXT = "Health Access"
    let TURN_ON_CATEGORIES_TEXT = "Turn All Categories On"
    let TURN_ON_CATEGORIES_TEXT_IOS_15 = "Turn On All"
    let ALLOW_BUTTON = "Allow"

    /*
     * Method allowHealthkitAccess is to give access to health kit
     */
    func allowHealthkitAccess() {
        if app.navigationBars[HEALTH_KIT_ACCESS_TEXT].waitForExistence(timeout: 15) {
            var healthKitCategoriesButton: XCUIElement
            if PiTestBase().iOSVersion >= "15.0" {
                healthKitCategoriesButton = app.staticTexts[TURN_ON_CATEGORIES_TEXT_IOS_15]
            } else {
                healthKitCategoriesButton = app.staticTexts[TURN_ON_CATEGORIES_TEXT]
            }
            let healthKitAllowButton = app.navigationBars[HEALTH_KIT_ACCESS_TEXT].buttons[ALLOW_BUTTON]
            // Check if already allowed
            if healthKitCategoriesButton.exists, healthKitAllowButton.exists {
                healthKitCategoriesButton.tap()
                healthKitAllowButton.tap()
            }
        } else {
            print("HealthKit access was set already before")
        }
    }
}
