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

import UIKit
import SVProgressHUD


class WA_LoginVC: WA_RootVC {
    @IBOutlet weak var yesBtn: UIButton!
    
    @IBOutlet weak var btnLogin: UIButton!
    
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var sureTwoV: UIView!
    
    @IBOutlet weak var registerBtn: UIButton!
    
    @IBOutlet weak var accountTextF: UITextField!
    
    @IBOutlet weak var codeTextF: UITextField!
    
    
    @IBOutlet weak var sureCodeTextF: UITextField!
    
    var type:Int = 1
    
    var surexx:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        btnLogin.layer.cornerRadius = 10
        
        
        self.sureTwoV.isHidden = true
        
    }
    
    @IBAction func backBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
        
    }
    
    
    @IBAction func xieyiBtn(_ sender: Any) {
        let  vc = WA_PrivacyVC()
        vc.type = 1
        self.present(vc, animated: true)
        
    }
    
    @IBAction func goRegisterBtn(_ sender: Any) {
        if self.type == 1 {
            self.sureTwoV.isHidden = false
            self.btnLogin.setTitle("register", for: .normal)
            self.titleLabel.text = "Welcome to register"
            self.registerBtn.setTitle("log on", for: .normal)
            self.type = 2
        }else{
            self.sureTwoV.isHidden = true
            self.btnLogin.setTitle("log on", for: .normal)
            self.titleLabel.text = "Welcome to login"
            self.registerBtn.setTitle("register", for: .normal)
            self.type = 1
        }
    }
    
    @IBAction func loginBtn(_ sender: UIButton) {
        if self.surexx == true{
            if self.type == 1{
                if AccountManager.login(username: self.accountTextF.text!, password: self.codeTextF.text!) {
                    setLoggedIn(true)
                    SVProgressHUD.showInfo(withStatus: "Login successful")
                    DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
                    SVProgressHUD.dismiss()
                    }
                    self.navigationController?.popViewController(animated: true)
                } else {
                    SVProgressHUD.showInfo(withStatus: "The registered account cannot be found or the password entered is incorrect.")
                    DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
                    SVProgressHUD.dismiss()
                    }
                }
                
            }else{
                if let acc = self.accountTextF.text, acc.count > 0 && acc.count <= 11{
                    if let code = self.codeTextF.text , code.count > 0 && code.count < 10{
                        if self.codeTextF.text == self.sureCodeTextF.text{
                            let newAccount = Account(username: self.accountTextF.text!, password: self.codeTextF.text!)
                            AccountManager.saveAccount(account: newAccount)
                            SVProgressHUD.showInfo(withStatus: "registration success")
                            DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
                            SVProgressHUD.dismiss()
                            }
                            self.sureTwoV.isHidden = true
                            self.btnLogin.setTitle("log on", for: .normal)
                            self.titleLabel.text = "Welcome to login"
                            self.registerBtn.setTitle("register", for: .normal)
                            self.type = 1
                            self.accountTextF.text = ""
                            self.codeTextF.text = ""
                            self.sureCodeTextF.text = ""
                        }else{
                            SVProgressHUD.showInfo(withStatus: "Please enter to confirm the correct password")
                            DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
                                SVProgressHUD.dismiss()
                            }
                        }
                    }else{
                        SVProgressHUD.showInfo(withStatus: "Please enter a password within 11 digits")
                        DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
                            SVProgressHUD.dismiss()
                        }
                    }
                }else{
                    SVProgressHUD.showInfo(withStatus: "Please enter an account within 11 digits")
                    DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
                        SVProgressHUD.dismiss()
                    }
                }
                
            }
            
        }else{
            SVProgressHUD.showInfo(withStatus: "Please check the agreement")
            DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
                SVProgressHUD.dismiss()
            }
        }

   
        
        
    }
    
    @IBAction func btnxySure(_ sender: Any) {
        
        if self.surexx == false {
            self.yesBtn.isSelected = true
            self.surexx = true
        }else{
            self.yesBtn.isSelected = false
            self.surexx = false
        }
    }

    
}
