//
//  FirebaseGenerator.swift
//
//
//  Created by Loris Perret on 05/12/2023.
//

// CPD-OFF

import Foundation

enum FirebaseGenerator {

    static var service: String {
        [
            Self.header,
            Self.logScreen,
            Self.logEvent,
            Self.enable,
            Self.footer
        ]
            .joined(separator: "\n")
    }

    // MARK: - Private vars

    private static var header: String {
        """
        // MARK: - Firebase

        class FirebaseAnalyticsManager: AnalyticsManagerProtocol {

            // MARK: - Methods

        """
    }

    private static var logScreen: String {
        """
            func logScreen(
                name: String,
                path: String,
                params: [String: Any]?
            ) {
                var parameters = [
                    AnalyticsParameterScreenName: name as NSObject
                ]

                if path.isEmpty == false {
                    parameters["path"] = path + "/iOS" as NSObject
                }

                if let supplementaryParameters = params {
                    for (newKey, newValue) in supplementaryParameters {
                        if parameters.contains(where: { (key: String, value: NSObject) in
                            key == newKey
                        }) {
                            continue
                        }

                        parameters[newKey] = newValue as? NSObject
                    }
                }

                Analytics.logEvent(
                    AnalyticsEventScreenView,
                    parameters: parameters
                )
            }

        """
    }

    private static var logEvent: String {
        """
            func logEvent(
                name: String,
                action: String,
                category: String,
                params: [String: Any]?
            ) {
                var parameters: [String: NSObject] = [
                    AnalyticsParameterItemName: name.replacingOccurrences(of: " ", with: "_") as NSObject
                ]

                if category.isEmpty == false {
                    parameters["AnalyticsParameterItemCategory"] = category as NSObject
                }

                if action.isEmpty == false {
                    parameters["action"] = action as NSObject
                }

                if let supplementaryParameters = params {
                    for (newKey, newValue) in supplementaryParameters {
                        if parameters.contains(where: { (key: String, value: NSObject) in
                            key == newKey
                        }) {
                            continue
                        }

                        parameters[newKey] = newValue as? NSObject
                    }
                }

                Analytics.logEvent(
                    AnalyticsEventSelectContent,
                    parameters: parameters
                )
            }

        """
    }

    private static var enable: String {
        """
            func setEnable(_ enable: Bool) {
                Analytics.setAnalyticsCollectionEnabled(enable)
            }
        """
    }

    private static var footer: String {
        """
        }
        """
    }
}

// CPD-ON
