//
//  TextView.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 4/1/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


@objc open class TextView: UITextView, MVMCoreViewProtocol, MoleculeViewProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open var model: MoleculeModelProtocol?
    
    private var initialSetupPerformed = false
    
    /// If true then text textView is currently displaying the stored placeholder text as there is not content to display.
    public var isShowingPlaceholder: Bool = false
    
    /// Set to true to hide the blinking textField cursor.
    public var hideBlinkingCaret = false
    
    public var placeholder = ""
    public var fontStyle: Styler.Font = Styler.Font.RegularBodyLarge
    public var placeholderFontStyle: Styler.Font = Styler.Font.RegularMicro
    public var placeholderTextColor: UIColor = .mvmCoolGray3
    
    public var isEnabled: Bool = true {
        didSet { isUserInteractionEnabled = isEnabled }
    }
    
    //--------------------------------------------------
    // MARK: - Delegate
    //--------------------------------------------------
    
    /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well.
    public weak var didDeleteDelegate: TextInputDidDeleteProtocol?
    
    //--------------------------------------------------
    // MARK: - Initialization
    //--------------------------------------------------
    
    public override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: .zero, textContainer: nil)
        initialSetup()
    }
    
    public convenience init() {
        self.init(frame: .zero, textContainer: nil)
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialSetup()
    }
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    public func initialSetup() {
        
        if !initialSetupPerformed {
            tintColor = .mvmBlack
            initialSetupPerformed = true
            setupView()
        }
    }
    
    open func updateView(_ size: CGFloat) {
        font = (isShowingPlaceholder ? placeholderFontStyle : fontStyle).getFont()
    }
    
    /// Will be called only once.
    open func setupView() {
        
        translatesAutoresizingMaskIntoConstraints = false
        defaultConfiguration()
    }
    
    public func defaultConfiguration() {
        
        text = ""
        placeholder = ""
        textAlignment = .left
        insetsLayoutMarginsFromSafeArea = false
        showsVerticalScrollIndicator = false
        showsHorizontalScrollIndicator = false
        isSecureTextEntry = false
        backgroundColor = .mvmWhite
        clipsToBounds = true
        smartQuotesType = .no
        smartDashesType = .no
        smartInsertDeleteType = .no
        inputAccessoryView = nil
        isAccessibilityElement = true
        accessibilityTraits = .staticText
        font = fontStyle.getFont()
        keyboardType = .default
        isEditable = true
    }
    
    open func reset() {
        
        fontStyle = .RegularBodyLarge
        placeholderFontStyle = .RegularMicro
        placeholderTextColor = .mvmCoolGray3
        textColor = .mvmBlack
        isEnabled = true
        text = ""
        isShowingPlaceholder = false
        inputAccessoryView?.removeFromSuperview()
        defaultConfiguration()
    }
    
    //--------------------------------------------------
    // MARK: - TextInputDidDeleteProtocol
    //--------------------------------------------------
    
    /// Alters the blinking caret line as per design standards.
    open override func caretRect(for position: UITextPosition) -> CGRect {
        
        if hideBlinkingCaret {
            return .zero
        }
        
        let caretRect = super.caretRect(for: position)
        return CGRect(origin: caretRect.origin, size: CGSize(width: 1, height: caretRect.height))
    }
    
    open override func deleteBackward() {
        super.deleteBackward()
        didDeleteDelegate?.textInputDidDelete()
    }
    
    //--------------------------------------------------
    // MARK: - Text / Placeholder
    //--------------------------------------------------
    
    open override func becomeFirstResponder() -> Bool {
        
        if isShowingPlaceholder {
            setTextContentTraits()
        }
        return super.becomeFirstResponder()
    }
    
    open override func resignFirstResponder() -> Bool {
        
        setPlaceholderIfAvailable()
        return super.resignFirstResponder()
    }
    
    public func setPlaceholderIfAvailable() {
        
        if !placeholder.isEmpty && text.isEmpty {
            setPlaceholderContentTraits()
        }
    }
    
    open func setTextContentTraits() {
        
        isShowingPlaceholder = false
        text = ""
        font = fontStyle.getFont()
        textColor = .mvmBlack
    }
    
    open func setPlaceholderContentTraits() {
        
        isShowingPlaceholder = true
        textColor = placeholderTextColor
        font = placeholderFontStyle.getFont()
        text = placeholder
    }
    
    @objc open func dismissFieldInput(_ sender: TextView) {
        
        _ = resignFirstResponder()
    }
}
