//
//  MP_BaseViewController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/4/18.
//

import UIKit
class MP_BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //背景颜色黑色
        view.backgroundColor = .init(hex: "#151718")
        navigationController?.navigationBar.isTranslucent = true
        tabBarController?.tabBar.isTranslucent = true
//        MP_NetWorkManager.shared.requestStatusToYouTube()
        // 注册应用从后台唤醒到前台的通知
//        NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForegroundAction), name:UIApplication.willEnterForegroundNotification, object: nil)
       
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //隐藏导航栏
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
    //前台通知
    @objc private func appWillEnterForegroundAction() {
        //检测当前状态是否拥有网络权限
        guard MP_NetWorkManager.shared.netWorkStatu != .reachable else {
            //当前网络正常状态
            return
        }
        //当前网络不可用,检查是否手动触发过
        guard UserDefaults.standard.bool(forKey: "isNetWorkUsage") != true else {
            return
        }
        //保存手动触发状态
        UserDefaults.standard.set(true, forKey: "isNetWorkUsage")

    }

//    private func triggerLocalNetworkPrivacyAlert() {
//        let sock4 = socket(AF_INET, SOCK_DGRAM, 0)
//        guard sock4 >= 0 else { return }
//        defer { close(sock4) }
//        let sock6 = socket(AF_INET6, SOCK_DGRAM, 0)
//        guard sock6 >= 0 else { return }
//        defer { close(sock6) }
//        
//        let addresses = addressesOfDiscardServiceOnBroadcastCapableInterfaces()
//        var message = [UInt8]("!".utf8)
//        for address in addresses {
//            address.withUnsafeBytes { buf in
//                let sa = buf.baseAddress!.assumingMemoryBound(to: sockaddr.self)
//                let saLen = socklen_t(buf.count)
//                let sock = sa.pointee.sa_family == AF_INET ? sock4 : sock6
//                _ = sendto(sock, &message, message.count, MSG_DONTWAIT, sa, saLen)
//            }
//        }
//    }
//    private func addressesOfDiscardServiceOnBroadcastCapableInterfaces() -> [Data] {
//        var addrList: UnsafeMutablePointer<ifaddrs>? = nil
//        let err = getifaddrs(&addrList)
//        guard err == 0, let start = addrList else { return [] }
//        defer { freeifaddrs(start) }
//        return sequence(first: start, next: { $0.pointee.ifa_next })
//            .compactMap { i -> Data? in
//            guard(i.pointee.ifa_flags & UInt32(bitPattern: IFF_BROADCAST)) != 0,
//                let sa = i.pointee.ifa_addr
//            else { return nil }
//            var result = Data(UnsafeRawBufferPointer(start: sa, count: Int(sa.pointee.sa_len)))
//            switch CInt(sa.pointee.sa_family) {
//            case AF_INET:
//                result.withUnsafeMutableBytes { buf in
//                    let sin = buf.baseAddress!.assumingMemoryBound(to: sockaddr_in.self)
//                    sin.pointee.sin_port = UInt16(9).bigEndian
//                }
//            case AF_INET6:
//                result.withUnsafeMutableBytes { buf in
//                    let sin6 = buf.baseAddress!.assumingMemoryBound(to: sockaddr_in6.self)
//                    sin6.pointee.sin6_port = UInt16(9).bigEndian
//                }
//            default:
//                return nil
//            }
//            return result
//        }
//    }

}
