//
//  YHCodeSueecssViewController.swift
//  galaxy
//
//  Created by EDY on 2024/2/5.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHCodeSueecssViewController: YHBaseViewController {

    var time = 60
    
    var bgImageView: UIImageView!
    var closeButton: UIButton!
    var loginTitleLabel: UILabel!
    var loginSubTitleLabel: UILabel!
    var getCodeLabel: UILabel!
    var getCodeButton: UIButton!
    var smscodeView: YHSmsCodeInputView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        gk_navBackgroundColor = .clear
        gk_navShadowColor = .clear
        gk_navigationBar.isHidden = true
        setView()
        // Do any additional setup after loading the view.
        startClicked()
    }
    
    func setView() {
        view.backgroundColor = .white
        bgImageView = {
            let imageView = UIImageView()
            imageView.image = UIImage(named: "login_phone_bg_image")
            imageView.isUserInteractionEnabled = true
            return imageView
        }()
        view.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.left.right.top.equalTo(view)
            make.height.equalTo(300)
        }
        
        closeButton = {
            let button = UIButton(type: .custom)
            button.setImage(UIImage(named: "login_back"), for: .normal)
            button.addTarget(self, action: #selector(close), for: .touchUpInside)
            return button
        }()
        view.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.left.equalTo(13)
            make.top.equalTo(51)
            make.width.height.equalTo(30)
        }
        
        loginTitleLabel = {
            let label = UILabel()
            label.text = "请输入验证码"
            label.font = kBoldFont(size: 28)
            label.textColor = UIColor(hex:0x222222)
            return label
        }()
        view.addSubview(loginTitleLabel)
        loginTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.top.equalTo(203)
            make.height.equalTo(25)
        }
        
        loginSubTitleLabel = {
            let label = UILabel()
            label.text = "已发送至 +86 188****2882"
            label.font = kFont(size: 13)
            label.textColor =  UIColor(hex:0xc0c0c0)
            return label
        }()
        view.addSubview(loginSubTitleLabel)
        loginSubTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.top.equalTo(233)
            make.height.equalTo(20)
        }
        
        smscodeView = {
            let view = YHSmsCodeInputView(config: YHSmsCodeViewConfig())
            view.complete = { [weak self] code in
                self?.dismiss(animated: true)
            }
            return view
        }()
        view.addSubview(smscodeView)
        smscodeView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(288)
            make.height.equalTo(55)
        }
        
        getCodeLabel = {
            let button = UILabel()
            button.font = kBoldFont(size: 12)
            button.text = "重新发送（58s）"
            button.textColor = UIColor(hex:0xc0c0c0)
            button.textAlignment = .center
            button.isHidden = false
            button.layer.cornerRadius = 4
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor(hex: 0xebeef4).cgColor
            return button
        }()
        view.addSubview(getCodeLabel)
        getCodeLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(382)
            make.height.equalTo(28)
            make.width.equalTo(91)
        }
        
        getCodeButton = {
            let button = UIButton(type: .custom)
            button.titleLabel?.font = kBoldFont(size: 12)
            button.contentHorizontalAlignment = .center
            button.setTitle("获取验证码", for: .normal)
            button.setTitleColor( UIColor(hex:0x222222), for: .normal)
            button.addTarget(self, action: #selector(startClicked), for: .touchUpInside)
            button.isHidden = true
            button.layer.cornerRadius = 4
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor(hex: 0xebeef4).cgColor
            return button
        }()
        view.addSubview(getCodeButton)
        getCodeButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(382)
            make.height.equalTo(28)
            make.width.equalTo(91)
        }
        
        //测试
        
    }
    
    @objc func startClicked() {
        getCodeLabel.isHidden = false
        getCodeButton.isHidden = true
        let sourceTimer = DispatchSource.makeTimerSource()
        sourceTimer.schedule(deadline: DispatchTime.now(), repeating: DispatchTimeInterval.milliseconds(1000), leeway: DispatchTimeInterval.milliseconds(0))
        sourceTimer.setEventHandler {
            if self.time != 0 {
                self.time -= 1
                DispatchQueue.main.async {
                    self.getCodeLabel?.text = String.localizedStringWithFormat("重新获取（%ds）",self.time)
                }
            } else if self.time <= 0 {
                sourceTimer.cancel()
                DispatchQueue.main.async {
//                    self.getCodeLabel?.text = "获取验证码"
                    self.time = 60
                    self.getCodeLabel.isHidden = true
                    self.getCodeButton.isHidden = false
                }
            }
        }
        // 启动定时器
        sourceTimer.resume()
    }
    
    @objc func close() {
        self.navigationController?.popViewController()
    }
}
