//
//  GlobalImports.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

@_exported import Foundation
@_exported import UIKit
@_exported import SnapKit

import AVFoundation

func k_Height_statusBar() -> CGFloat {
    var statusBarHeight: CGFloat = 0;
    if #available(iOS 13.0, *) {
        let scene = UIApplication.shared.connectedScenes.first;
        guard let windowScene = scene as? UIWindowScene else {return 0};
        guard let statusBarManager = windowScene.statusBarManager else {return 0};
        statusBarHeight = statusBarManager.statusBarFrame.height;
    } else {
        statusBarHeight = UIApplication.shared.statusBarFrame.height;
    }
    return statusBarHeight;
}
/// ②、顶部安全区高度 k_Height_safeAreaInsetsTop
func k_Height_safeAreaInsetsTop() -> CGFloat {
    if #available(iOS 13.0, *) {
        let scene = UIApplication.shared.connectedScenes.first;
        guard let windowScene = scene as? UIWindowScene else {return 0}; // guard：如果 expression 值计算为false，则执行代码块内的 guard 语句。(必须包含一个控制语句: return、 break、 continue 或 throw。)。as?：类型转换，(还有这两种：as、as!)
        guard let window = windowScene.windows.first else {return 0};
        return window.safeAreaInsets.top;
    } else if #available(iOS 11.0, *) {
        guard let window = UIApplication.shared.windows.first else {return 0};
        return window.safeAreaInsets.top;
    }
    return 0;
}

/// ③、底部安全区高度
func k_Height_safeAreaInsetsBottom() -> CGFloat {
    if #available(iOS 13.0, *) {
        let scene = UIApplication.shared.connectedScenes.first;
        guard let windowScene = scene as? UIWindowScene else {return 0};
        guard let window = windowScene.windows.first else {return 0};
        return window.safeAreaInsets.bottom;
    } else if #available(iOS 11.0, *) {
        guard let window = UIApplication.shared.windows.first else {return 0};
        return window.safeAreaInsets.bottom;
    }
    return 0;
}
/** 屏幕宽度 */
let k_ScreenWidth = UIScreen.main.bounds.size.width
/** 屏幕高度 */
let k_ScreenHeight = UIScreen.main.bounds.size.height
/* 导航栏高度 固定高度 = 44.0f */
//let k_Height_NavContentBar :CGFloat  = UINavigationBar.appearance().frame.size.height
let k_Height_NavBar :CGFloat = 44.0
/** 状态栏高度 */
let k_Height_StatusBar :CGFloat = k_Height_statusBar()
/** 状态栏+导航栏的高度 */
let k_Nav_Height: CGFloat = k_Height_NavBar + k_Height_StatusBar
/** 底部tabBar栏高度（不包含安全区，即：在 iphoneX 之前的手机） */
let k_TabBar_Height :CGFloat = 49.0
/** 底部导航栏高度（包括安全区），一般使用这个值 */
let k_Height_TabBar :CGFloat = k_Height_safeAreaInsetsBottom() + k_TabBar_Height



// MARK: - 检查系统版本 -
/* 检查系统版本 */
/// 版本号相同：
func systemVersionEqual(version:String) -> Bool {
    return UIDevice.current.systemVersion == version
}

/// 系统版本高于等于该version  测试发现只能传入带一位小数点的版本号  不然会报错    具体原因待探究
func systemVersionGreaterThan(version:String) -> Bool {
    return UIDevice.current.systemVersion.compare(version, options: .numeric, range: version.startIndex..<version.endIndex, locale: Locale(identifier:version)) != ComparisonResult.orderedAscending
}

//判断是否是 x、及x以上 系列
func isIphoneX() -> Bool {
    return k_Height_safeAreaInsetsBottom() > 0.0; // 底部安全区 > 0 时，
    
}

//设计稿为 375*812 比例,所以比例系数为 375.0/kScreenWidth
let kDesignScale = k_ScreenWidth/375.0


func kGetUIWdith(designSize:CGSize,currentW:CGFloat) -> CGFloat {
    let scale = designSize.width/currentW
    return designSize.height/scale
}
    

public func debugPrint<T>(_ messsage: T, file: String = #file, funcName: String = #function, lineNum: Int = #line) {
    #if DEBUG
    let fileName = (file as NSString).lastPathComponent
    print(Date.hmsString + " \(fileName) (\(funcName)): [\(lineNum)]- \(messsage)")
    #endif
}

public func className(from object: Any) -> String {
    return String(describing: type(of: object))
}


func appVersion() ->String{
    let short = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
    return "V" + short
}

/// 震动
func playVibration() {
    // 默认震动   kSystemSoundID_Vibrate
    // 1519     短震 3D Touch中的peek震动反馈
    // 1520     短震 3D Touch中的pop震动反馈
    // 1521     连续三次短震动
    // peek的震动反馈轻于pop
    
    let soundID: UInt32 = 1520
    AudioServicesPlaySystemSound(soundID)
}



/// 主线程延迟执行回调
/// - Parameters:
///   - delay: 延迟时间（秒）
///   - completion: 延迟后的回调
func kDelayOnMainThread(_ delay: TimeInterval, completion: @escaping () -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
        completion()
    }
}

func kDelayMainShort(completion: @escaping () -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        completion()
    }
}

/// 在主线程上执行代码
/// - Parameter block: 要执行的代码块
func kExecuteOnMainThread(_ block: @escaping () -> Void) {
    if Thread.isMainThread {
        block()
    } else {
        DispatchQueue.main.async {
            block()
        }
    }
}


func kPresentModalVC(target:UIViewController,modelVC:UIViewController,style:UIModalPresentationStyle = .overFullScreen,transitionStyle:UIModalTransitionStyle = .coverVertical){
    let navi = TSBaseNavigationC(rootViewController: modelVC)
    navi.modalPresentationStyle = style
    navi.modalTransitionStyle = transitionStyle
    target.present(navi, animated: true)
}

func kPushVC(target:UIViewController,modelVC:UIViewController){
    modelVC.hidesBottomBarWhenPushed = true
    target.navigationController?.pushViewController(modelVC, animated: true)
}
