//
//  YL_PlayVC.swift
//  playbtest
//
//  Created by 忆海16 on 2024/11/5.
//

import UIKit

class YL_PlayVC: UIViewController,UITextViewDelegate {
    
    
    @IBOutlet weak var textSDKView: UITextView!
    
    @IBOutlet weak var bundleIdLab: UILabel!
    
    @IBOutlet weak var deviceIdLab: UILabel!
    
    @IBOutlet weak var idfaLab: UILabel!
    
    @IBOutlet weak var ipLab: UILabel!
    
    @IBOutlet weak var ad1Lab: UILabel!
    
    @IBOutlet weak var ad2Lab: UILabel!
    
    @IBOutlet weak var ad3Lab: UILabel!
    
    private var observation: NSKeyValueObservation?
    
    private var observationis: NSKeyValueObservation?
    
    var openADTimer:Timer?
    let kOpenADPerSec: CGFloat = 0.1 // 假设的秒数
    let kOpenAdCTimeLength: CGFloat = 30 // 假设的超时时长
    
    static var totalTimeC: CGFloat = 0.0
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        BbbAdManager.shared.loadAd()
        let bundleId = Bundle.main.bundleIdentifier ?? ""
        bundleIdLab.text = "Name:\(bundleId)"
        let deviceId = BbbAdManager.config.adbrush_deviceid ?? ""
        deviceIdLab.text = "DeviceID:\(deviceId)"
        let locIp = BbbAdManager.config.adbrush_localip ?? ""
        let remoteIp = BbbAdManager.config.remoteIp
        ipLab.text = "LocIP:\(locIp),RemoteIp:\(remoteIp)"
        
        if #available(iOS 14, *) {
            IDFA.shared.checkATT { idfa in
                if let idfa = idfa {
                    print("IDFA: \(idfa)")
                    BbbAdManager.config.idfa = idfa
                    self.idfaLab.text =  "IDFA:\(idfa)"
                } else {
                    print("无法获取 IDFA")
                }
            }
        } else {
            IDFA.shared.getIDFAForOlderVersions { idfa in
                if let idfa = idfa {
                    print("IDFA: \(idfa)")
                    BbbAdManager.config.idfa = idfa
                    self.idfaLab.text = "IDFA:\(idfa)"
                } else {
                    print("无法获取 IDFA")
                }
            }
        }
        

        // 确保计时器被正确添加到 RunLoop
        /*
        DispatchQueue.global(qos: .utility).async { [weak self] in
            guard self != nil else { return }
            YL_NetWorkManager.saveIphonelogs()
        }
         */
        
        textSDKView.delegate = self
    
    }
    
    
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        ad1Lab.text = BbbAdManager.config.adids[0]
        ad2Lab.text = BbbAdManager.config.adids[1]
        ad3Lab.text = BbbAdManager.config.adids[2]

        
        self.navigationController?.navigationBar.isHidden = true
        
        NotificationCenter.default.addObserver(self, selector: #selector(addTextToTextView), name: NSNotification.Name("adinfo"), object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(adStatusChange), name: NSNotification.Name("adStatus"), object: nil)
         
        NotificationCenter.default.post(name: NSNotification.Name("adinfo"), object: nil, userInfo: ["text": "adbrush_base_url：\(BbbAdManager.config.adbrush_base_url)，adbrush_deviceid:\(String(describing: BbbAdManager.config.adbrush_deviceid))，adbrush_localip:\(String(describing: BbbAdManager.config.adbrush_localip))，adbrush_local_url:\(BbbAdManager.config.adbrush_local_url)，adbrush_ecpm:\(BbbAdManager.config.adbrush_ecpm)"])
        
        
    }
    
    

    
    
    // 向文本末尾追加 100 行空白行
    func appendExtraLines1() {
        let extraLines = String(repeating: "\n", count: 100)
        let currentText = textSDKView.text ?? ""
        
        // 检查末尾是否已经有额外行，避免重复追加
        if !currentText.hasSuffix(extraLines) {
            textSDKView.text = currentText.trimmingCharacters(in: .whitespacesAndNewlines) + extraLines
        }
    }
    
    
    
    @objc func addTextToTextView(notification: Notification) {
        if let newText = notification.userInfo?["text"] as? String {
            textSDKView.text.append("\(newText)\n\n")
        }
    }
    
    

    @objc func adStatusChange(notification: Notification) {
        if let newText = notification.userInfo?["text"] as? String, let id = notification.userInfo?["id"] as? String {
            var lab  = ad1Lab
            if id == BbbAdManager.config.adids[1] {
                lab = ad2Lab
            } else if id == BbbAdManager.config.adids[2] {
                lab = ad3Lab
            }
            lab?.text = "\(id):\(newText)"
        }
    }
    


    


    
    deinit {
        
        NotificationCenter.default.removeObserver(self)

    }
    
}
