//
//  WA_SettingSVC.swift
//  wallpaper_project
//
//  Created by 忆海16 on 2024/3/11.
//

import UIKit
import SVProgressHUD

class WA_SettingSVC: WA_RootVC {
    
    @IBOutlet weak var btnstar: UIButton!
    
    @IBOutlet weak var bgView: UIView!
    var overlayWindow: UIWindow?
    var tapCount = 0
    var timer: Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        bgView.layer.cornerRadius = 10
        bgView.clipsToBounds = true
        
        
        let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "star")
        
        if !hasLaunchedBefore {
            // 设置 UserDefaults 中的键值对，标记应用程序已经启动过了
            self.btnstar.isSelected = false
            UserDefaults.standard.set(false, forKey: "star")
        }else{
            self.btnstar.isSelected = true
            UserDefaults.standard.set(true, forKey: "star")
        }
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
        if let currentAccount = AccountManager.currentAccount {
            print("当前登录的账户是：\(currentAccount.username)")
        } else {
            print("没有登录的账户")
        }
    }
    
    @IBAction func starBtn(_ sender: UIButton) {
        let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "star")
        if hasLaunchedBefore == true{
            self.btnstar.isSelected = false
            UserDefaults.standard.set(false, forKey: "star")
        }else{
            self.btnstar.isSelected = true
            UserDefaults.standard.set(true, forKey: "star")
        }
    }
    
    @IBAction func back(_ sender: Any) {
       
        
        self.navigationController?.popViewController(animated: true)
    }
    
    
    
    @IBAction func privacyBtn(_ sender: Any) {
        
        let vc = WA_PrivacyVC()
        vc.type = 0
        self.present(vc, animated: true)
        
    }
    
    
    @IBAction func scBtn(_ sender: Any) {
        if isLoggedIn(){
            let vc = WA_SCVC()
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else{
            let vc = WA_LoginVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        
    }
    
    
    @IBAction func btnLoginCancel(_ sender: UIButton) {
        if isLoggedIn(){
            // 创建UIAlertController
            let alertController = UIAlertController(title: "prompt", message: "Are you sure you want to Delete  your account?", preferredStyle: .alert)
            
            
            let confirmAction = UIAlertAction(title: "sure", style: .default) { (_) in
                let currentAccount = AccountManager.currentAccount
                let loggedInUsername = currentAccount!.username
                AccountManager.logoutSpecificAccount(username: loggedInUsername)
                setLoggedIn(false)
            }
            
            confirmAction.setValue(UIColor.red, forKey: "titleTextColor")
            
            alertController.addAction(confirmAction)
            
            let secondAction = UIAlertAction(title: "cancle", style: .default) { (_) in
                
            }
            
            alertController.addAction(secondAction)
            
            self.present(alertController, animated: true, completion: nil)
            
        }else{
            
            let vc = WA_LoginVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }
    
    
    
    
    @IBAction func browseBtn(_ sender: Any) {
        let vc = WA_HistoryVC()
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
    
    @IBAction func touchv(_ sender: Any) {
        // 增加点击计数
        tapCount += 1
        
        // 如果计数器为空，启动一个定时器
        if timer == nil {
            timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(resetTapCount), userInfo: nil, repeats: false)
        }
        
        // 检查是否点击了三次
        if tapCount == 6 {
            // 执行你想要的操作
            print("连点6下")
//            SVProgressHUD.showInfo(withStatus: "连点6下")
//            DispatchQueue.main.asyncAfter(wallDeadline: .now() + 1.0) {
//                SVProgressHUD.dismiss()
//            }
//            AppDelegate.shared.showOverlayWindow()
//            UserDefaults.standard.set(true, forKey: "iswindows")
//            
            
            if UserDefaults.standard.object(forKey: "iswindows") != nil {
                if UserDefaults.standard.bool(forKey: "iswindows") {
                    AppDelegate.shared.removeOverlayWindow()
                    UserDefaults.standard.set(false, forKey: "iswindows")
                } else {
                    AppDelegate.shared.showOverlayWindow()
                    UserDefaults.standard.set(true, forKey: "iswindows")
                }
            } else {
                AppDelegate.shared.showOverlayWindow()
                UserDefaults.standard.set(true, forKey: "iswindows")
            }
            
            resetTapCount()
        }
    }
    
    @objc func resetTapCount() {
        // 重置计数器和定时器
        tapCount = 0
        timer?.invalidate()
        timer = nil
    }
    
    
   
}
