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

import UIKit


@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 1 point below the text
        let y = textRect.origin.y + textRect.size.height + 1
        
        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 + 2)
    }
    
    //--------------------------------------------------
    // 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)
        isEnabled = model.enabled
        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)
        
        var width = size
        if MVMCoreGetterUtility.fequal(a: Float.leastNormalMagnitude, b: Float(size)) {
            width = MVMCoreUIUtility.getWidth()
        }
        
        titleLabel?.font = MFStyler.fontB2(forWidth: width)
    }
    
    open override func setupView() {
        super.setupView()
        backgroundColor = .clear
        contentMode = .redraw
        setTitleColor(.mvmBlack, for: .normal)
        setTitleColor(.mvmCoolGray6, for: .disabled)
        titleLabel?.numberOfLines = 1
        titleLabel?.lineBreakMode = .byTruncatingTail
        titleLabel?.textAlignment = .left
        contentHorizontalAlignment = .left
        contentVerticalAlignment = .top
    }
}

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