//
//  YNSmsCodeView.swift
//  galaxy
//
//  Created by edy on 2023/12/29.
//

import UIKit

class YNSmsCodeView: UIView {

    // 是否显示光标
    var showCursor : Bool = false {
        didSet {
            if (showCursor) {
                UIView.animate(withDuration: 0.5, animations:  {
                    [weak self] in
                    self?.cursor.isHidden = false
                    
                }, completion:  {
                    
                    [weak self] finishd in
                    UIView.animate(withDuration: 0.5, animations:  {
                        self?.cursor.isHidden = true
                    })
                })
                
            } else {
                cursor.layer.removeAllAnimations()
                cursor.isHidden = true
            }
        }
    }
    var text : String? {
        didSet {
            label.text = text
        }
    }
    lazy var label : UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 20)
        return label
    }()
    
    var cursor : UIView = {
        let cursor = UIView()
        cursor.backgroundColor = .black
        cursor.alpha = 0
        return cursor
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.config()
    }
    
    func config() {
        self.isUserInteractionEnabled = false
        self.layer.cornerRadius = 6;
        self.clipsToBounds = true;
        self.backgroundColor = UIColor(white: 1, alpha: 0.3)

        self.addSubview(label)
        self.addSubview(cursor)
    }
    

    override func layoutSubviews() {
        
//        let x = (self.frame.size.width - self.label.frame.size.width) / 2.0;
//        let y = (self.frame.size.height - self.label.frame.size.height) / 2.0;
        self.label.frame = self.bounds
        
        self.cursor.bounds = CGRectMake(0, 0, 1.5, self.frame.size.height - 20);
        self.cursor.center = CGPoint(x:CGRectGetMidX(self.bounds) , y: CGRectGetMidY(self.bounds))
    }
}
