//
//  CaretLink.swift
//  MVMCoreUI
//
//  Created by Kolli, Praneeth on 1/5/18.
//  Created by Christiano, Kevin on 3/18/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


open class CaretLink: Button, MVMCoreUIViewConstrainingProtocol {
    //------------------------------------------------------
    // MARK: - Constants
    //------------------------------------------------------
    
    private let CARET_VIEW_HEIGHT: Float = 10.5
    private let CARET_VIEW_WIDTH: Float = 6.5
    
    //------------------------------------------------------
    // MARK: - Properties
    //------------------------------------------------------
    
    @objc public var rightView: UIView?
    @objc public var rightViewHeight: NSNumber?
    @objc public var rightViewWidth: NSNumber?
    
    @objc public var enabledColor: UIColor = .mvmBlack {
        didSet { changeCaretColor() }
    }
    
    @objc public var disabledColor: UIColor = .mvmCoolGray3 {
        didSet { changeCaretColor() }
    }
    
    private var caretSpacingConstraint: NSLayoutConstraint?
    
    //------------------------------------------------------
    // MARK: - Inits
    //------------------------------------------------------
    
    // Default values for view.
    public required init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.init(frame: .zero)
        backgroundColor = .clear
        translatesAutoresizingMaskIntoConstraints = false
        setTitleColor(enabledColor, for: .normal)
        setTitleColor(disabledColor, for: .disabled)
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    //------------------------------------------------------
    // MARK: - Lifecycle
    //------------------------------------------------------
    
    override public var isEnabled: Bool {
        didSet { changeCaretColor() }
    }
    
    public override func updateView(_ size: CGFloat) {
        titleLabel?.font = MFStyler.fontBoldBodySmall()
    }
    
    //------------------------------------------------------
    // MARK: - Methods
    //------------------------------------------------------
    
    private func changeCaretColor() {
        
        setTitleColor(enabledColor, for: .normal)
        setTitleColor(disabledColor, for: .disabled)
        
        if let rightCaretView = rightView as? CaretView {
            rightCaretView.enabledColor = enabledColor
            rightCaretView.disabledColor = disabledColor
            rightCaretView.isEnabled = isEnabled
        }
    }
    
    private func createCaretView() -> CaretView {
        let caret = CaretView()
        caret.lineWidth = 1.5
        return caret
    }
    
    private func addCaretImageView() {
        
        rightView?.removeFromSuperview()
        
        let width = CGFloat(rightViewWidth?.floatValue ?? CARET_VIEW_WIDTH)
        let height = CGFloat(rightViewHeight?.floatValue ?? CARET_VIEW_HEIGHT)
        
        contentEdgeInsets.right = 4 + width
        
        let caretView: UIView = rightView ?? createCaretView()
        rightView = caretView
        rightView?.translatesAutoresizingMaskIntoConstraints = false
        addSubview(caretView)
        
        caretView.widthAnchor.constraint(equalToConstant: width).isActive = true
        caretView.heightAnchor.constraint(equalToConstant: height).isActive = true
        
        let caretLabelSpacing = NSLayoutConstraint(item: caretView, attribute: .left, relatedBy: .equal, toItem: titleLabel, attribute: .right, multiplier: 1.0, constant: 7)
        caretLabelSpacing.isActive = true
        caretSpacingConstraint = caretLabelSpacing
        
        caretView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        trailingAnchor.constraint(greaterThanOrEqualTo: caretView.trailingAnchor).isActive = true
        caretView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor).isActive = true
        bottomAnchor.constraint(greaterThanOrEqualTo: caretView.bottomAnchor).isActive = true
        contentHorizontalAlignment = .left
        
        // Set correct color after layout
        changeCaretColor()
    }
    
    public func updateCaretSpacing(_ spacing: CGFloat) {
        caretSpacingConstraint?.constant = spacing
    }
    
    //------------------------------------------------------
    // MARK: - Atomization
    //------------------------------------------------------
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        addCaretImageView()
        guard let model = model as? CaretLinkModel else { return }
        
        if let color = model.backgroundColor {
            backgroundColor = color.uiColor
        }
        
        enabledColor = (model.inverted ? model.enabledColor_inverted : model.enabledColor).uiColor
        disabledColor = (model.inverted ? model.disabledColor_inverted : model.disabledColor).uiColor
        
        isEnabled = model.enabled
        set(with: model.action, delegateObject: delegateObject, additionalData: additionalData)
        setTitle(model.title, for: .normal)
    }
    
    open override func reset() {
        super.reset()
        rightView?.removeFromSuperview()
    }

    public func needsToBeConstrained() -> Bool { true }
    
    open func horizontalAlignment() -> UIStackView.Alignment { .leading }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 10.5 }
}
