//
//  INAppConfigUtil.swift
//  Introduce
//
//  Created by SanW on 2021/12/22.
//  Copyright © 2021 BytesFlow. All rights reserved.
//

import BFAnalyzeKit
import BFCommonKit
import BFMaterialKit
import UIKit

/// <#Description#>
class INAppConfigUtil: NSObject {
    class func pageConfig() {
        BFConfig.shared.appType = "20"
        BFConfig.shared.appId = "1599191466"
        BFConfig.shared.styleColor = .intrGreen
        BFConfig.shared.statusBarStyle = .light
        BFConfig.shared.styleBackGroundColor = UIColor.black
        BFConfig.shared.styleTitleColor = UIColor.white
        BFConfig.shared.cutViewStyleColor = UIColor.white
        BFConfig.shared.cutViewTintColor = UIColor.black
        BFConfig.shared.pointEditNamalBackgroundColor = UIColor.hexColor(hexadecimal: "#1A1A1A")
        BFMaterialConfig.shared.materialDeleteImage = UIImage.moduleImage(named: "icon_search_delete", moduleName: "BFMaterialKit") ?? UIImage()
        BFConfig.shared.cutDurationColor = UIColor(red: 238.0 / 255.0, green: 0 / 255.0, blue: 81.0 / 255.0, alpha: 0.1)
        BFConfig.shared.hiddenMusicMask = false
        BFConfig.shared.otherTintColor = UIColor.hexColor(hexadecimal: "#333333")
        BFConfig.shared.statusBarStyle = .light
        PQSingletoMemoryUtil.shared.createSesstionId()
        BFConfig.shared.sessionId = PQSingletoMemoryUtil.shared.sessionId
        BFConfig.shared.subSessionId = PQSingletoMemoryUtil.shared.sessionId
    }

    class func applicationConfig(launchOptions _: [UIApplication.LaunchOptionsKey: Any]?) {
        // 初始化友盟 61c3e145e0f9bb492ba6e4e4
        registerUM(umAppkey: "61c3e145e0f9bb492ba6e4e4")
        // 初始化Bugly
        registerBugly(appID: "80af5e913c", deviceId: getMachineCode())
        // 冷热启动上报
        appLaunchReportUpload()
    }

    /// 初始化友盟
    /// - Parameters:
    ///   - umAppkey: <#umAppkey description#>
    ///   - channel: <#channel description#>
    class func registerUM(umAppkey: String, channel: String = "AppStore") {
        #if DEBUG
            UMConfigure.setLogEnabled(true)
        #else
            // 注册友盟
            UMConfigure.setEncryptEnabled(true)
            // 初始化友盟所有组件产品
            UMConfigure.initWithAppkey(umAppkey, channel: channel)
        #endif
    }

    /// 初始化Bugly
    /// - Parameters:
    ///   - appID: <#appID description#>
    ///   - channel: <#channel description#>
    ///   - deviceId: <#deviceId description#>
    class func registerBugly(appID: String, channel: String = "AppStore", deviceId: String? = nil) {
        let versionName = "\(Bundle.main.infoDictionary?["CFBundleShortVersionString"] ?? "1.0.0")"
        // 注册bugly
        let buglyConfig = BuglyConfig()
        buglyConfig.reportLogLevel = .warn
        #if DEBUG
            buglyConfig.version = versionName + ".6666"
            buglyConfig.debugMode = true
        #else
            buglyConfig.version = versionName + "." + versionCode
        #endif
        buglyConfig.channel = channel
        buglyConfig.deviceIdentifier = deviceId ?? ""
        buglyConfig.unexpectedTerminatingDetectionEnable = true
        buglyConfig.blockMonitorEnable = true
        buglyConfig.blockMonitorTimeout = 2
        Bugly.start(withAppId: appID, config: buglyConfig)
    }

    // 冷热启动上报
    class func appLaunchReportUpload(isHotLaunch: Bool = false) {
        // 冷启动
        PQSingletoMemoryUtil.shared.isColdLaunch = true
        // 是否第一次安装
        var isFirstLaunch: Int = 1
        let firstInstall: String? = getUserDefaults(key: cFirstInstall) as? String
        if firstInstall == nil || (firstInstall?.count ?? 0 <= 0) || firstInstall != "1" {
            isFirstLaunch = 1
        } else {
            isFirstLaunch = 0
        }
        // 冷启动上报
        BFEventTrackAdaptor.appLaunchReportUpload(isFirstLaunch: isFirstLaunch, firstParams: nil, isHotLaunch: isHotLaunch, launchType: PQSingletoMemoryUtil.shared.coldLaunchType?.rawValue ?? "userActiveOpen", commonParams: commonParams())
        // 清空启动数据
        PQSingletoMemoryUtil.shared.coldLaunchType = nil
        saveUserDefaults(key: cFirstInstall, value: "1")
    }
}
