//
//  YHConfig.swift
//  galaxy
//
//  Created by EDY on 2023/12/26.
//

import Foundation
import UIKit

let APP_NAME = Bundle.main.infoDictionary!["CFBundleName"] as! String

func YHLOG<T>(message:T,file:String = #file,funcName:String = #function,lineName:Int = #line){
    //这个DEBUG 是一个debug时候的一个flag需要自己去xcode设置项设置。
#if DEBUG
    let flieName = (file as NSString).lastPathComponent
    print("\(APP_NAME)/\(flieName)/\(funcName)/\(lineName):\(message)")
#endif
}

///屏幕宽度
let KScreenWidth = UIScreen.main.bounds.size.width
///屏幕高度
let KScreenHeight = UIScreen.main.bounds.size.height
///导航栏高度
let k_Height_NavContentBar :CGFloat = 44.0
/** 状态栏高度 */
let k_Height_StatusBar :CGFloat = k_Height_statusBar()
/** 状态栏+导航栏的高度 */
let k_Height_NavigationtBarAndStatuBar: CGFloat = k_Height_NavContentBar + k_Height_StatusBar
/** 底部tabBar栏高度（不包含安全区，即：在 iphoneX 之前的手机） */
let k_TabBar_Height :CGFloat = 49.0
/** 底部导航栏高度（包括安全区），一般使用这个值 */
let k_Height_TabBar :CGFloat = k_Height_safeAreaInsetsBottom() + k_TabBar_Height

// MARK: - 尺寸信息： -
/* 状态栏高度  20 或 44 */
// 然而从iOS 14开始，全面屏iPhone的状态栏高度不一定是 44 或 20 了，比如下面就是这些设备在iOS 14.1上的状态栏高度。（还可能时47、48）
/// ①、顶部状态栏高度（包括安全区）
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;
    }
    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;
    }
    return 0;
}

///获取系统版本号
func IOSVersion() -> Double {
    return (UIDevice.current.systemVersion as NSString).doubleValue
}
///组件化用类名创建控制器
func ClassFromString(className: String) -> UIViewController? {
    let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName")
    
    let name = "\(appName!).\(className)"
    if let tmpClass = NSClassFromString(name) as? UIViewController.Type {
        return tmpClass.init()
    } else {
        return nil
    }
}

/** 判断字符串是否为空 */
func kIsEmpty(string: String) -> Bool {
    if string.isEmpty || string == "" {
        return true
    }else {
        return false
    }
}

/**  文字大小设置
 *  parameter name 字体名字 (为空时默认字体)
 *  parameter size 字体大小
 */
func kFont(name: String = "", size: CGFloat) -> UIFont {
    if !kIsEmpty(string: name) {
        return UIFont.init(name: name, size: size)!
    }else {
        return UIFont.systemFont(ofSize: size)
    }
}

func kBoldFont(name: String = "", size: CGFloat) -> UIFont {
    if !kIsEmpty(string: name) {
        return UIFont.init(name: name, size: size)!
    }else {
        return UIFont.boldSystemFont(ofSize: size)
    }
}

/// 版本号相同：
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 时，
}

/// 根据RGBA生成颜色(格式为：22,22,22,0.5)
var YHRGBAColor: (CGFloat, CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue, alpha in
    return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: alpha)
}

/// 根据RGB生成颜色(格式为：22,22,22)
var YHRGBColor: (CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue in
    return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1)
}

/// 根据色值生成颜色(无透明度)(格式为0xffffff)
var YHColorWithHex: (NSInteger) -> UIColor = {hex in
    return UIColor(red: ((CGFloat)((hex & 0xFF0000) >> 16)) / 255.0, green: ((CGFloat)((hex & 0xFF00) >> 8)) / 255.0, blue: ((CGFloat)(hex & 0xFF)) / 255.0, alpha: 1)
}
