//
//  CCSMSView.swift
//  SwiftProject
//
//  Created by soldoros on 2024/1/10.
//

import UIKit
import Foundation

let kPasswordLength:NSInteger = 6

let TIMER:NSInteger = 60

/// 确认手机号或者邮箱
typealias SMSConfirmPEBlock = () -> Bool

/// 确认可以发送验证码 进入倒计时
typealias SMSConfirmSendBlock = () -> ()


class CCSMSView: UIView {

    /// 确认手机号或者邮箱的代码块
    var _peBlock:SMSConfirmPEBlock?
    
    /// 确认可以发送验证码的代码块
    var _sendBlock:SMSConfirmSendBlock?
    
    //  是否可以发送
    var _peConfirm:Bool?

    //计时
    var _timer:Timer?
    var _time:NSInteger?
    
    //倒计时按钮
    var _mGetPassBtn:UIButton?

    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// 初始化验证码发射器
    ///
    /// - Parameters:
    ///   - frame: 倒计时区域的frame
    ///   - type: 验证码发送方式（手机号，邮箱）
    ///   - style: 验证码功能
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        
        _time = TIMER
        
        self.clipsToBounds = true
        self.layer.cornerRadius = 4
        self.layer.borderColor = UIColor.hexStringToColor(hexString: "#333333").cgColor
        self.layer.borderWidth = 1
        self.isUserInteractionEnabled = true
        
        
        _mGetPassBtn = UIButton.init(type: UIButton.ButtonType.custom)
        _mGetPassBtn?.frame = self.bounds
        _mGetPassBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        _mGetPassBtn?.setTitleColor(UIColor.hexStringToColor(hexString: "#333333"), for: UIControl.State.normal)
        _mGetPassBtn?.setTitle("获取验证码", for: UIControl.State.normal)
        self.addSubview(_mGetPassBtn!)
        _mGetPassBtn?.backgroundColor = UIColor.clear
        _mGetPassBtn?.addTarget(self, action: #selector(buttonPressed(sender:)), for: UIControl.Event.touchUpInside)
        
    }
    
    
    /// 验证邮箱和手机号 发送验证码
    ///
    /// - Parameters:
    ///   - peBlock: 返回邮箱或者手机号
    ///   - codeBlock: 获取验证码后的回调
    func confirmPEBlock(peBlock:@escaping SMSConfirmPEBlock,sendBlock:@escaping SMSConfirmSendBlock){
        
        _peBlock = peBlock
        _sendBlock = sendBlock
    }
    
    
    @objc func buttonPressed(sender:UIButton){
        
        _peConfirm = _peBlock!()
        print(_peConfirm as Any)
        
        if(_peConfirm == true){
            self.smsBeginTimer()
            _sendBlock!()
        }
    }
    
    func smsBeginTimer(){
        self.layer.borderColor = UIColor.lightGray.cgColor
        _mGetPassBtn?.isEnabled = false
        _mGetPassBtn?.setTitleColor(UIColor.lightGray, for: UIControl.State.normal)
        self .startTimer()
    }
    
    func startTimer(){
        
        let message:NSString = NSString(format: "重发%lds", _time!)
        _mGetPassBtn?.setTitle(message as String, for: UIControl.State.normal)
        self.perform(#selector(addTimer), with: nil, afterDelay: 1)
        
        if(_timer == nil){
            _timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(addTimer), userInfo: nil, repeats: true)
        }
        
    }
    
    @objc func addTimer(){
        
        if(_time! > 0){
            let message:NSString = NSString(format: "重发%lds", _time!)
            _mGetPassBtn?.setTitle(message as String, for: UIControl.State.normal)
            _time = _time! - 1
        }
        else{
            self.chushihua()
        }
    }
    
    func chushihua(){
        
        _time = TIMER
        _timer?.invalidate()
        if(_timer != nil){
            _timer = nil
        }
        self.layer.borderColor = UIColor.hexStringToColor(hexString: "#333333").cgColor
        _mGetPassBtn?.isEnabled = true
        _mGetPassBtn?.setTitleColor(UIColor.hexStringToColor(hexString: "#333333"), for: UIControl.State.normal)
        _mGetPassBtn?.setTitle("获取验证码", for: UIControl.State.normal)
    }
}

