//
//  ExternalLink.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 2/20/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


open class ExternalLink: Link {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var exportImageView: UIImageView?
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? ExternalLinkModel else { return }
        
        exportImageView?.tintColor = titleColor(for: model.enabled ? .normal : .disabled)
    }
    
    //--------------------------------------------------
    // MARK: - MVMCoreViewProtocol
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        let image = MVMCoreUIUtility.imageNamed("externalLink")
        exportImageView = UIImageView(image: image?.withRenderingMode(.alwaysTemplate))
        
        guard let exportIcon = exportImageView else { return }
        
        exportIcon.contentMode = .scaleAspectFit
        exportIcon.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(exportIcon)
        trailingAnchor.constraint(greaterThanOrEqualTo: exportIcon.trailingAnchor).isActive = true
        
        if let titleLabel = titleLabel {
            let dimension =  titleLabel.font.pointSize
            exportIcon.heightAnchor.constraint(equalToConstant: dimension).isActive = true
            exportIcon.widthAnchor.constraint(equalToConstant: dimension).isActive = true
            exportIcon.leadingAnchor.constraint(equalTo: titleLabel.trailingAnchor, constant: 4).isActive = true
            exportIcon.bottomAnchor.constraint(equalTo: titleLabel.lastBaselineAnchor, constant: 3).isActive = true
        }
    }
}
