// Generated by ResgenSwift.Analytics 1.2

import MatomoTracker
import FirebaseAnalytics

// MARK: - Protocol

protocol AnalyticsManagerProtocol {
    func logScreen(name: String, path: String)
    func logEvent(
        name: String,
        action: String,
        category: String,
        params: [String: Any]?
    )
}

// MARK: - Matomo

class MatomoAnalyticsManager: AnalyticsManagerProtocol {
    
    // MARK: - Properties

    private var tracker: MatomoTracker

    // MARK: - Init

    init(siteId: String, url: String) {
        debugPrint("[Matomo service] Server URL: \(url)")
        debugPrint("[Matomo service] Site ID: \(siteId)")
        tracker = MatomoTracker(
            siteId: siteId,
            baseURL: URL(string: url)!
        )
        
        #if DEBUG
            tracker.dispatchInterval = 5
        #endif
        
        #if DEBUG
            tracker.logger = DefaultLogger(minLevel: .verbose)
        #endif

        debugPrint("[Matomo service] Configured with content base: \(tracker.contentBase?.absoluteString ?? "-")")
        debugPrint("[Matomo service] Opt out: \(tracker.isOptedOut)")
    }
    
    // MARK: - Methods

    func logScreen(name: String, path: String) {
        guard !tracker.isOptedOut else { return }
        guard let trackerUrl = tracker.contentBase?.absoluteString else { return }

        let urlString = URL(string: "\(trackerUrl)" + "/" + "\(path)" + "iOS")
        tracker.track(
            view: [name],
            url: urlString
        )
    }

    func logEvent(
        name: String,
        action: String,
        category: String,
        params: [String: Any]?
    ) {
        guard !tracker.isOptedOut else { return }

        tracker.track(
            eventWithCategory: category,
            action: action,
            name: name,
            number: nil,
            url: nil
        )
    }
}

// MARK: - Firebase

class FirebaseAnalyticsManager: AnalyticsManagerProtocol {
    func logScreen(name: String, path: String) {
        var parameters = [
            AnalyticsParameterScreenName: name as NSObject
        ]

        Analytics.logEvent(
            AnalyticsEventScreenView,
            parameters: parameters
        )
    }

    func logEvent(
        name: String,
        action: String,
        category: String,
        params: [String: Any]?
    ) {
        var parameters: [String:NSObject] = [
            "action": action as NSObject,
            "category": category 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(
            name.replacingOccurrences(of: [" "], with: "_"),
            parameters: parameters
        )
    }
}

// MARK: - Manager

class AnalyticsManager {
    static var shared = AnalyticsManager()
    
    // MARK: - Properties

    var managers: [AnalyticsManagerProtocol] = []

    private var isEnabled: Bool = true

    // MARK: - Methods

    func setAnalyticsEnabled(_ enable: Bool) {
        isEnabled = enable
    }

    func configure(siteId: String, url: String) {
        managers.append(
            MatomoAnalyticsManager(
                siteId: siteId,
                url: url
            )
        )
        managers.append(FirebaseAnalyticsManager())
    }
        
    private func logScreen(name: String, path: String) {
        guard isEnabled else { return }

        managers.forEach { manager in
            manager.logScreen(name: name, path: path)
        }
    }
    
    private func logEvent(
        name: String,
        action: String,
        category: String,
        params: [String: Any]?
    ) {
        guard isEnabled else { return }

        managers.forEach { manager in
            manager.logEvent(
                name: name,
                action: action,
                category: category,
                params: params
            )
        }
    }

    // MARK: - section_one

    func logScreenS1DefOne(title: String) {
        logScreen(
            name: "s1 def one \(title)",
            path: "s1_def_one/\(title)"
        )
    }

    func logEventS1DefTwo(title: String, count: String) {
        logEvent(
            name: "s1 def two",
            action: "test",
            category: "test",
            params: [
                "title": title,
				"count": count
            ]
        )
    }

    // MARK: - section_two

    func logScreenS2DefOne() {
        logScreen(
            name: "s2 def one",
            path: "s2_def_one/"
        )
    }
}
