//
//  WP_LockVC.swift
//  WallpaperHD_Live
//
//  Created by 忆海16 on 2024/7/22.
//

import UIKit
import SVProgressHUD

class WP_LockVC: WP_RootVC {
    @IBOutlet weak var backBtn: UIButton!
    
    @IBOutlet weak var sure: UIButton!
    
    @IBOutlet weak var paypassView: SPayPassWordView!
    
    var type:Int = 0
    var code:String?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        sure.layer.cornerRadius = 10
        paypassView.delegate = self
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
        if self.type == 0{
            self.backBtn.isHidden = true
        }else{
            self.backBtn.isHidden = false
        }
    }
    
    
    @IBAction func back(_ sender: Any) {
        dismiss(animated: true)
    }
    
    @IBAction func sure(_ sender: Any) {
        let userDefaults = UserDefaults.standard
        if userDefaults.bool(forKey: "islock"){
            let codestr = userDefaults.value(forKey: "lockcode")
            
            if self.code == codestr as? String{
                if self.type == 0 {
                    SVProgressHUD.showInfo(withStatus: "Password is correct")
                    DispatchQueue.global().asyncAfter(deadline: .now() + 1.0) {
                        SVProgressHUD.dismiss()
                    }
                    WP_TabbarCommon.TabBarController()
                }else{
                    userDefaults.set(false, forKey: "islock")
                    SVProgressHUD.showInfo(withStatus: "Close lock successfully")
                    DispatchQueue.global().asyncAfter(deadline: .now() + 1.0) {
                        SVProgressHUD.dismiss()
                    }
                    self.dismiss(animated: true)
                }
       
            }else{
                SVProgressHUD.showInfo(withStatus: "wrong password")
                DispatchQueue.global().asyncAfter(deadline: .now() + 1.0) {
                    SVProgressHUD.dismiss()
                }
            }
        }else{
            userDefaults.set(self.code, forKey: "lockcode")
            userDefaults.set(true, forKey: "islock")
            SVProgressHUD.showInfo(withStatus: "Set lock successfully")
            DispatchQueue.global().asyncAfter(deadline: .now() + 1.0) {
                SVProgressHUD.dismiss()
            }
            self.dismiss(animated: true)
        }
        
    }
}
extension WP_LockVC:SPayPassWordViewDelegate{
    
    func entryComplete(password: String) {
        self.code = password

    }
    
}
