//
//  KMVerficationCodeWindowController.swift
//  PDF Reader Pro
//
//  Created by lizhe on 2023/2/24.
//

import Cocoa

var verficationCodeController: KMVerficationCodeWindowController?
var verficationCodeMainWindow: NSWindow?

typealias KMVerficationCodeWindowControllerDidCancellation = () -> ()
class KMVerficationCodeWindowController: NSWindowController {

    @IBOutlet weak var verificationCodeView: KMVerificationCodeView!
    @IBOutlet weak var networkView: KMLightNoNetworkView!
    
    var didCancellation: KMVerficationCodeWindowControllerDidCancellation?
    var inputType: DataNavigationViewButtonActionType?
    
    deinit {
        KMPrint("KMVerficationCodeWindowController 释放")
    }
    
    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
        self.setup()
    }
    
    //MARK: 打开文件
    static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMVerficationCodeWindowController {
        let controller: KMVerficationCodeWindowController = KMVerficationCodeWindowController.init(windowNibName: "KMVerficationCodeWindowController")
        controller.inputType = type
        window.beginSheet(controller.window!)
        controller.window?.center()
        controller.verificationCodeView.inputType = .accountInfo
        controller.verificationCodeView.model.email = KMLightMemberManager.manager.info.email
        controller.sendVerifyCode(sender: nil)
        verficationCodeController = controller
        verficationCodeMainWindow = window
        return controller
    }
    
    func setup() {
        self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
        
        self.verificationCodeView.closeAction = { view in
            KMPrint("关闭")
            verficationCodeMainWindow?.endSheet(view.window!)
            view.window?.close()
            verficationCodeController = nil
            verficationCodeMainWindow = nil
        }
        
        self.verificationCodeView.cancelAction = { view in
            KMPrint("登出")
            verficationCodeMainWindow?.endSheet(view.window!)
            view.window?.close()
            verficationCodeController = nil
            verficationCodeMainWindow = nil
        }
        
        self.verificationCodeView.doneAction = { [weak self] (view, data , sender) in
            KMPrint("注销")
            KMRequestServerManager.manager.logOff(verifyCode: data.verifyCode, complete: { [weak self] success, result in
                if self?.verificationCodeView != nil {
                    if success {
                        verficationCodeMainWindow?.endSheet(view.window!)
                        view.window?.close()
                        verficationCodeController = nil
                        verficationCodeMainWindow = nil
                        self?.didCancellation?()
                    } else {
                        self?.verificationCodeView.showAlert(result: result)
                    }
                    self?.verificationCodeView.changeDoneButtonState(enable: true)
                }
            })
        }
        
        self.verificationCodeView.reSendAction = {[weak self]  view, sender in
            self?.sendVerifyCode(sender: sender)
        }
        
        self.verificationCodeView.verificationCodeAction = { [weak self] (view, data, codeString) in
            KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: codeString, verifyCodeType: view.verifyCodeType) { success, result in
                if self?.verificationCodeView != nil {
                    if success {
                        self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: true)
                    } else {
                        self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: false)
                        self?.verificationCodeView.showAlert(result: result)
                    }
                }
            }
        }
        
        KMRequestServer.requestServer.reachabilityStatusChange { [weak self] status in
            if status == .notReachable {
                KMPrint("无网络")
                self?.networkView.isHidden = false
            } else {
                KMPrint("有网络")
                self?.networkView.isHidden = true
            }
        }
    }
    
    func sendVerifyCode(sender: NSTextView?) {
        KMRequestServerManager.manager.getVerifyCode(verifyCodeType: self.verificationCodeView.verifyCodeType, email: self.verificationCodeView.model.email) { [weak self] success, result in
            if self?.verificationCodeView != nil {
                if success {
                    self?.verificationCodeView.resetTimer()
                } else {
                    self?.verificationCodeView.showAlert(result: result)
                    self?.verificationCodeView.time = (self?.verificationCodeView!.startTime)!
                    self?.verificationCodeView.updateLanguage()
                }
                
                if sender != nil {
                    sender!.isSelectable = true
                }
            }
        }
    }
    
}