//
//  UITextViewExt.swift
//  ExtensionKit
//
//  Created by FFIB on 2017/9/13.
//  Copyright © 2017年 FFIB. All rights reserved.
//

import UIKit

//
extension UITextView {
    
    public var isEmpty: Bool {
        return text?.isEmpty ?? false
    }
    
    public func clear() {
        text = ""
        attributedText = NSAttributedString(string: "")
    }
    
}

private var isLimit = false
//placeholder
extension UITextView: UITextViewDelegate {
    
    /// Resize the placeholder when the UITextView bounds change
    
    override open var bounds: CGRect {
        didSet {
            resizePlaceholder()
        }
    }
    
    open override var backgroundColor: UIColor? {
        didSet {
            if let placeholderLabel = viewWithTag(1000001) as? UILabel {
                 placeholderLabel.backgroundColor = backgroundColor
            }
        }
    }
    
    public var placeholder: String? {
        get {
            var placeholderText: String?
            
            if let placeholderLabel = viewWithTag(1000001) as? UILabel {
                placeholderText = placeholderLabel.text
            }
            
            return placeholderText
        }
        set {
            if let placeholderLabel = viewWithTag(1000001) as! UILabel? {
                placeholderLabel.text = newValue
                placeholderLabel.sizeToFit()
            } else {
                self.addPlaceholder(newValue!)
            }
            delegate = self
        }
    }
    
    /// When the UITextView did change, show or hide the label based on if the UITextView is empty or not
    ///
    /// - Parameter textView: The UITextView that got updated
    public func textViewDidChange(_ textView: UITextView) {
        if let placeholderLabel = viewWithTag(1000001) as? UILabel {
            placeholderLabel.isHidden = text.count > 0
        }
        
        //adaptive height
//        if isAdaptiveHeight {
            var heightConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: height)
            for constraint in constraints {
                if constraint.firstAttribute == .height {
                    heightConstraint = constraint
                }
            }
            let value = max(contentSize.height, heightConstraint.constant)
            heightConstraint.constant = value
            if !heightConstraint.isActive {
                heightConstraint.isActive = true
            }
//        }
    }
    
    /// Resize the placeholder UILabel to make sure it's in the same position as the UITextView text
    private func resizePlaceholder() {
        if let placeholderLabel = viewWithTag(1000001) as! UILabel? {
            let labelX = textContainer.lineFragmentPadding + textContainerInset.left
            let labelY = textContainerInset.top - 2
            let labelWidth = frame.width - (labelX * 2)
            let labelHeight = placeholderLabel.frame.height
            
            placeholderLabel.frame = CGRect(x: labelX, y: labelY, width: labelWidth, height: labelHeight)
        }
    }
    
    /// Add a placeholder UILabel to this UITextView
    private func addPlaceholder(_ placeholderText: String) {
        let placeholderLabel = UILabel()
        placeholderLabel.text = placeholderText
        placeholderLabel.sizeToFit()
        placeholderLabel.font = font
        placeholderLabel.textColor = UIColor.lightGray
        placeholderLabel.tag = 1000001
        placeholderLabel.isHidden = text.count > 0
        
        addSubview(placeholderLabel)
        resizePlaceholder()
        delegate = self
    }
}

// scroll
extension UITextView {
    
    public func scrollToBottom() {
        let range = NSMakeRange(text.count - 1, 1)
        scrollRangeToVisible(range)
    }
    
    public func scrollToTop() {
        let range = NSMakeRange(0, 1)
        scrollRangeToVisible(range)
    }
}