//
//  PQTextView.swift
//  PQSpeed
//
//  Created by SanW on 2020/6/11.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit

open class PQTextView: UITextView {
    private let textValuesQueue = DispatchQueue(label: "com.BytesFlow.textValuesQueue", qos: .default)

   public var textDidChangedHandle: ((_ textView: UITextView) -> Void)?
    // 最多输入的个数
    public  var maxTextLength : Int?
    // 输入个数超过时提示
    public  var maxTextLengthRemind : String?
    /// setNeedsDisplay调用drawRect
    public  var placeHolder: String = "" {
        didSet {
            setNeedsDisplay()
        }
    }

    /// placeHolder是否居中
    public  var isCenter: Bool = false {
        didSet {
            setNeedsDisplay()
        }
    }

    public var placeHolderColor: UIColor = UIColor.gray {
        didSet {
            setNeedsDisplay()
        }
    }

    open override var font: UIFont? {
        didSet {
            setNeedsDisplay()
        }
    }

    open override var text: String! {
        didSet{
           setNeedsLayout()
        }
    }

    open override var attributedText: NSAttributedString! {
        didSet {
            setNeedsDisplay()
        }
    }

    public override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        /// default字号
        PQNotification.addObserver(self, selector: #selector(textDidChanged(noti:)), name: UITextView.textDidChangeNotification, object: self)
    }

    required public init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func textDidChanged(noti _: NSNotification) {
        if maxTextLength != nil && text.count > maxTextLength! {
            text = String(text.prefix(maxTextLength!))
            cShowHUB(superView: nil, msg: (maxTextLengthRemind != nil && (maxTextLengthRemind?.count ?? 0) > 0) ? maxTextLengthRemind : "最多输入\( maxTextLength!)个字符")
        }
        setNeedsDisplay()
        if textDidChangedHandle != nil {
            textDidChangedHandle!(self)
        }
    }

    open override func draw(_ rect: CGRect) {
        if hasText {
            return
        }
        let size = sizeWithText(text: placeHolder, font: font ?? UIFont.systemFont(ofSize: 14), size: rect.size)
        var newRect = CGRect()
        newRect.size.width = size.width
        newRect.size.height = size.height
        if isCenter {
            newRect.origin.x = (rect.width - size.width) / 2
            newRect.origin.y = (rect.height - size.height) / 2
        } else {
            newRect.origin.x = placeHolder.contains("未识别到文字") ? 0 : 5
            newRect.origin.y = placeHolder.contains("未识别到文字") ? 0 : 7
        }
        (placeHolder as NSString).draw(in: newRect, withAttributes: [NSAttributedString.Key.font: font ?? UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: placeHolderColor])
    }

    open override func layoutSubviews() {
        super.layoutSubviews()
        setNeedsDisplay()
    }

    deinit {
        PQNotification.removeObserver(self, name: UITextView.textDidChangeNotification, object: self)
    }
}
