//
//  AnalyticsDefinitionTests.swift
//
//
//  Created by Loris Perret on 06/12/2023.
//

import Foundation
import XCTest

@testable import ResgenSwift

final class AnalyticsDefinitionTests: XCTestCase {
    
    // MARK: - Matching tags
    
    func testMatchingAnalyticss() {
        // Given
        let definition = AnalyticsDefinition(id: "definition_name", name: "", type: .screen)
        definition.tags = ["ios","iosonly","notranslation"]
        
        // When
        let match1 = definition.hasOneOrMoreMatchingTags(inputTags: ["ios"])
        let match2 = definition.hasOneOrMoreMatchingTags(inputTags: ["iosonly"])
        let match3 = definition.hasOneOrMoreMatchingTags(inputTags: ["notranslation"])
        
        
        // Expect
        XCTAssertTrue(match1)
        XCTAssertTrue(match2)
        XCTAssertTrue(match3)
    }
    
    func testNotMatchingAnalyticss() {
        // Given
        let definition = AnalyticsDefinition(id: "definition_name", name: "", type: .screen)
        definition.tags = ["ios","iosonly","notranslation"]
        
        // When
        let match1 = definition.hasOneOrMoreMatchingTags(inputTags: ["droid"])
        let match2 = definition.hasOneOrMoreMatchingTags(inputTags: ["droidonly"])
        let match3 = definition.hasOneOrMoreMatchingTags(inputTags: ["azerty"])
        
        // Expect
        XCTAssertFalse(match1)
        XCTAssertFalse(match2)
        XCTAssertFalse(match3)
    }
    
    // MARK: - Raw properties
    
    func testGeneratedRawPropertyScreen() {
        // Given
        let definition = AnalyticsDefinition(id: "definition_name", name: "Ecran un", type: .screen)
        definition.path = "ecran_un/"
        
        // When
        let propertyScreen = definition.getProperty(visibility: .internal)

        // Expect
        let expectScreen = """
            internal func logScreenDefinitionName() {
                logScreen(
                    name: "Ecran un",
                    path: "ecran_un/",
                    params: nil
                )
            }
        """
        
        XCTAssertEqual(propertyScreen.adaptForXCTest(), expectScreen.adaptForXCTest())
    }
    
    func testGeneratedRawPropertyEvent() {
        // Given
        let definition = AnalyticsDefinition(id: "definition_name", name: "Ecran un", type: .event)
        
        // When
        let propertyEvent = definition.getProperty(visibility: .public)

        // Expect
        let expectEvent = """
            public func logEventDefinitionName() {
                logEvent(
                    name: "Ecran un",
                    action: "",
                    category: "",
                    params: nil
                )
            }
        """
        
        XCTAssertEqual(propertyEvent.adaptForXCTest(), expectEvent.adaptForXCTest())
    }
    
    func testGeneratedRawStaticPropertyScreen() {
        // Given
        let definition = AnalyticsDefinition(id: "definition_name", name: "Ecran un", type: .screen)
        definition.path = "ecran_un/"
        
        // When
        let propertyScreen = definition.getStaticProperty(visibility: .private)

        // Expect
        let expectScreen = """
            private static func logScreenDefinitionName() {
                AnalyticsManager.shared.logScreen(
                    name: "Ecran un",
                    path: "ecran_un/",
                    params: nil
                )
            }
        """
        
        XCTAssertEqual(propertyScreen.adaptForXCTest(), expectScreen.adaptForXCTest())
    }
    
    func testGeneratedRawStaticPropertyEvent() {
        // Given
        let definition = AnalyticsDefinition(id: "definition_name", name: "Ecran un", type: .event)
        
        // When
        let propertyEvent = definition.getStaticProperty(visibility: .package)

        // Expect
        let expectEvent = """
            package static func logEventDefinitionName() {
                AnalyticsManager.shared.logEvent(
                    name: "Ecran un",
                    action: "",
                    category: "",
                    params: nil
                )
            }
        """
        
        XCTAssertEqual(propertyEvent.adaptForXCTest(), expectEvent.adaptForXCTest())
    }
}
