//
//  FirebaseConfigTests.swift
//  edXTests
//
//  Created by Saeed Bashir on 8/28/18.
//  Copyright © 2018 edX. All rights reserved.
//

import Foundation

@testable import edX

let apiKey = "APebSdWSu456EDkUk0imSGqetnOznbZv22QRiq1"
let clientID = "302611111829-s11900000000tdhcbj9876548888qur3.apps.googleusercontent.com"
let googleAppID = "3:902600000000:ios:c00089xx00000266"
let gcmSenderID = "303600005829"

class FirebaseConfigTests: XCTestCase {

    func testNoFirebaseConfig() {
        let config = OEXConfig(dictionary:[:])
        XCTAssertFalse(config.firebaseConfig.enabled)
        XCTAssertFalse(config.firebaseConfig.analyticsEnabled)
        XCTAssertFalse(config.firebaseConfig.cloudMessagingEnabled)
        
    }

    func testEmptyFirebaseConfig() {
        let config = OEXConfig(dictionary:["FIREBASE":[:]])
        XCTAssertFalse(config.firebaseConfig.enabled)
        XCTAssertFalse(config.firebaseConfig.analyticsEnabled)
        XCTAssertFalse(config.firebaseConfig.cloudMessagingEnabled)
    }

    func testFirebaseConfig() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": true,
                "CLOUD_MESSAGING_ENABLED": true,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]

        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertTrue(config.firebaseConfig.enabled)
        XCTAssertTrue(config.firebaseConfig.analyticsEnabled)
        XCTAssertTrue(config.firebaseConfig.cloudMessagingEnabled)
    }

    func testFirebaseDisableConfig() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": false,
                "ANALYTICS_ENABLED": true,
                "CLOUD_MESSAGING_ENABLED": true,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]

        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertFalse(config.firebaseConfig.enabled)
        XCTAssertFalse(config.firebaseConfig.analyticsEnabled)
        XCTAssertFalse(config.firebaseConfig.cloudMessagingEnabled)
    }

    func testFirebaseDisableAnalytics() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": false,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]

        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertTrue(config.firebaseConfig.enabled)
        XCTAssertFalse(config.firebaseConfig.analyticsEnabled)
        XCTAssertFalse(config.firebaseConfig.cloudMessagingEnabled)
    }

    func testFirebaseDisableCloudMessaging() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": true,
                "CLOUD_MESSAGING_ENABLED": false,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]

        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertTrue(config.firebaseConfig.enabled)
        XCTAssertTrue(config.firebaseConfig.analyticsEnabled)
        XCTAssertFalse(config.firebaseConfig.cloudMessagingEnabled)
    }
    
    func testFirebaseAPIKey() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": true,
                "CLOUD_MESSAGING_ENABLED": true,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]
        
        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertTrue(config.firebaseConfig.enabled)
        XCTAssertTrue(config.firebaseConfig.analyticsEnabled)
        XCTAssertTrue(config.firebaseConfig.cloudMessagingEnabled)
        XCTAssertEqual(config.firebaseConfig.apiKey, apiKey)
    }
    
    func testFirebaseClientID() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": true,
                "CLOUD_MESSAGING_ENABLED": true,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]
        
        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertTrue(config.firebaseConfig.enabled)
        XCTAssertTrue(config.firebaseConfig.analyticsEnabled)
        XCTAssertTrue(config.firebaseConfig.cloudMessagingEnabled)
        XCTAssertEqual(config.firebaseConfig.cliendID, clientID)
    }
    
    func testFirebaseGoogleAppID() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": true,
                "CLOUD_MESSAGING_ENABLED": true,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]
        
        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertTrue(config.firebaseConfig.enabled)
        XCTAssertTrue(config.firebaseConfig.analyticsEnabled)
        XCTAssertTrue(config.firebaseConfig.cloudMessagingEnabled)
        XCTAssertEqual(config.firebaseConfig.googleAppID, googleAppID)
    }
    
    func testFirebaseGCMSenderID() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": true,
                "CLOUD_MESSAGING_ENABLED": true,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]
        
        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertTrue(config.firebaseConfig.enabled)
        XCTAssertTrue(config.firebaseConfig.analyticsEnabled)
        XCTAssertTrue(config.firebaseConfig.cloudMessagingEnabled)
        XCTAssertEqual(config.firebaseConfig.gcmSenderID, gcmSenderID)
    }
    
    func testFirebaseRequiredKeysAvailable() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": true,
                "CLOUD_MESSAGING_ENABLED": true,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]
        
        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertTrue(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertTrue(config.firebaseConfig.enabled)
        XCTAssertTrue(config.firebaseConfig.analyticsEnabled)
        XCTAssertTrue(config.firebaseConfig.cloudMessagingEnabled)
    }
    
    func testFirebaseRequiredKeysNotAvailable() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": false,
                "CLOUD_MESSAGING_ENABLED": false,
            ]
        ]
        
        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertFalse(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertFalse(config.firebaseConfig.enabled)
        XCTAssertFalse(config.firebaseConfig.analyticsEnabled)
        XCTAssertFalse(config.firebaseConfig.cloudMessagingEnabled)
    }
    
    func testFirebaseRequiredKeyMissing() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": false,
                "CLOUD_MESSAGING_ENABLED": true,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ]
        ]
        
        let config = OEXConfig(dictionary: configDictionary)
        XCTAssertFalse(config.firebaseConfig.requiredKeysAvailable)
        XCTAssertFalse(config.firebaseConfig.enabled)
        XCTAssertFalse(config.firebaseConfig.analyticsEnabled)
        XCTAssertFalse(config.firebaseConfig.cloudMessagingEnabled)
    }
    
    func testFirebaseDisableIfSegmentEnable() {
        let configDictionary = [
            "FIREBASE" : [
                "ENABLED": true,
                "ANALYTICS_ENABLED": false,
                "CLOUD_MESSAGING_ENABLED": true,
                "API_KEY" : apiKey,
                "CLIENT_ID" : clientID,
                "GOOGLE_APP_ID" : googleAppID,
                "GCM_SENDER_ID" : gcmSenderID
            ],
            
            "SEGMENT_IO": [
                "ENABLED": true,
                "SEGMENT_IO_WRITE_KEY": "p910192UHD101010nY0000001Kb00GFcz'"
            ]
        ]
        
        let config = OEXConfig(dictionary: configDictionary)
        let firebaseEnable = config.firebaseConfig.enabled && !(config.segmentConfig?.isEnabled ?? false)
        XCTAssertTrue(config.segmentConfig?.isEnabled ?? false)
        XCTAssertFalse(firebaseEnable)
    }
}
