//
//  Macro.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/3/25.
//
import UIKit
import Foundation
import AVFoundation
//MARK: - 常用宏定义
///屏幕宽
let screen_Width = UIScreen.main.bounds.width
///屏幕高
let screen_Height = UIScreen.main.bounds.height
///像素比值
let width = screen_Width / 375
///状态栏高度
#if __IPHONE_13_0
let statusBarHeight:CGFloat = UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.size.height
#else
let statusBarHeight:CGFloat = UIApplication.shared.statusBarFrame.size.height
#endif
///状态栏和导航栏的总高度
let navAndstatusBarHeight = statusBarHeight + 44
///判断是否是刘海屏
let iphoneX = ((statusBarHeight != 20) ? true : false)
///获取版本号
let LOCAL_RELEASE_VERSION = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
///获取程序名字
let App_Name = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String
///获取手机型号
let Phone_Model = UIDevice.current.model
///系统版本号
let System_Version = UIDevice.current.systemVersion
///获取当前系统语言
let Language_first_local = NSLocale.preferredLanguages.first!
///底部安全区域
let bottomPadding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
///隐私政策网址
let privacyUrl:URL = .init(string: "https://musicoo.app/privacy")!
///用户协议网址
let serviceUrl:URL = .init(string: "https://musicoo.app/terms")!
//MARK: - 全局变量与方法
///总事件闭包
typealias ActionBlock = () -> Void?
///全局模态弹出类型
var MPModalType:MPPresentModal = .Timer
///转时分值
func setTimesToMinSeconds(_ time:TimeInterval) -> String {
    //设置分钟
    let min = Int(time / 60)
    let second = Int(time.truncatingRemainder(dividingBy: 60))
    return "\(min < 10 ? "0\(min)":"\(min)"):\(second < 10 ? "0\(second)":"\(second)")"
}
///转分值
func setTimesToMins(_ time:TimeInterval) -> String {
    //设置分钟
    let min = Int(time / 60)
    return "\(min < 10 ? "0\(min)":"\(min)")"
}
///获取麦克风权限
func authorize(observe:UIViewController) -> Bool{
    let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
    switch status {
    case .authorized:
        return true
    case .notDetermined:
        // 请求授权
        AVCaptureDevice.requestAccess(for: AVMediaType.audio, completionHandler: {(status) in
            DispatchQueue.main.async(execute: {() -> Void in
                _ = authorize(observe: observe)
            })
        })
    default: ()
        DispatchQueue.main.async(execute: { () -> Void in
        let alertController = UIAlertController(title: "Get Microphone Access",message: "“Musicoo” asks you to turn on your microphone to recognize the decibels around you and turns on white noise for you automatically. Please go to the “Settings” page to turn on the microphone permission",preferredStyle: .alert)
        let cancelAction = UIAlertAction(title:"Cancel", style: .cancel, handler:nil)
        let settingsAction = UIAlertAction(title:"Settings", style: .default, handler: {
                (action) -> Void in
                let url = URL(string: UIApplication.openSettingsURLString)
                if let url = url, UIApplication.shared.canOpenURL(url) {
                    if #available(iOS 10, *) {
                        UIApplication.shared.open(url, options: [:],
                                                  completionHandler: {
                                                    (success) in
                        })
                    } else {
                        UIApplication.shared.openURL(url)
                    }
                }
            })
            alertController.addAction(cancelAction)
            alertController.addAction(settingsAction)
            observe.present(alertController, animated: true)
        })
    }
    return false
}

