//
//  Link.swift
//  MVMCoreUI
//
//  Created by Robinson, Blake on 11/26/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit
import VDSColorTokens

@objcMembers open class Link: Button {
    //--------------------------------------------------
    // MARK: - Draw
    //--------------------------------------------------
    
    open override func draw(_ rect: CGRect) {
        
        guard let textRect = titleLabel?.frame,
              let context = UIGraphicsGetCurrentContext()
        else { return }
        
        // Set line to the same color as the text
        if let color = titleLabel?.textColor?.cgColor {
            context.setStrokeColor(color)
        }
        
        // x should be according to the text, not the button
        let x = textRect.origin.x
        
        // Line is 0 point below the text
        let y = textRect.origin.y + textRect.size.height
        
        context.move(to: CGPoint(x: x, y: y))
        context.addLine(to: CGPoint(x: x + textRect.size.width, y: y))
        context.strokePath()
    }
    
    open override var intrinsicContentSize: CGSize {
        guard let size = titleLabel?.intrinsicContentSize else { return super.intrinsicContentSize }
        return CGSize(width: size.width, height: size.height + 1)
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? LinkModel else { return }
        
        setTitle(model.title, for: .normal)
        if let accessibilityText = model.accessibilityText {
            accessibilityLabel = accessibilityText
        }
        setTitleColor((model.inverted ? model.enabledColor_inverted : model.enabledColor).uiColor, for: .normal)
        setTitleColor((model.inverted ? model.disabledColor_inverted : model.disabledColor).uiColor, for: .disabled)
        setTitleColor((model.inverted ? model.activeColor_inverted : model.activeColor).uiColor, for: .highlighted)
        isEnabled = model.enabled
        titleLabel?.font = model.getFont(model.size)
        set(with: model.action, delegateObject: delegateObject, additionalData: additionalData)
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject:  MVMCoreUIDelegateObject?) -> CGFloat? { 31 }
}

// MARK: - MVMCoreViewProtocol
extension Link {
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
    }
    
    open override func setupView() {
        super.setupView()
        backgroundColor = .clear
        contentMode = .redraw
        setTitleColor(VDSColor.elementsPrimaryOnlight, for: .normal)
        setTitleColor(VDSColor.interactiveDisabledOnlight, for: .disabled)
        setTitleColor(VDSColor.interactiveActiveOnlight, for: .highlighted)
        titleLabel?.numberOfLines = 1
        titleLabel?.lineBreakMode = .byTruncatingTail
        titleLabel?.textAlignment = .left
        contentHorizontalAlignment = .left
        contentVerticalAlignment = .top
    }
}

// MARK: - MVMCoreUIViewConstrainingProtocol
extension Link: MVMCoreUIViewConstrainingProtocol {
    
    open func horizontalAlignment() -> UIStackView.Alignment { .leading }
}
