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

import UIKit


open class ColorViewWithLabel: View {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var label = Label(fontStyle: .RegularBodySmall, true)
    public var colorView = View()
    private var sizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: 8)!
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    public var heightConstraint: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        addSubview(colorView)
        addSubview(label)
        
        heightConstraint = colorView.heightAnchor.constraint(equalToConstant: sizeObject.getValueBasedOnApplicationWidth())
        heightConstraint?.isActive = true
        colorView.widthAnchor.constraint(equalTo: colorView.heightAnchor).isActive = true
        colorView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        colorView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        
        label.leadingAnchor.constraint(equalTo: colorView.trailingAnchor, constant: PaddingOne).isActive = true
        label.topAnchor.constraint(equalTo: topAnchor).isActive = true
        trailingAnchor.constraint(equalTo: label.trailingAnchor).isActive = true
        bottomAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        label.updateView(size)
        heightConstraint?.constant = sizeObject.getValueBased(onSize: size)
        setNeedsDisplay()
    }
    
    open override func reset() {
        super.reset()
        label.reset()
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let chartItemModel = model as? DoughnutChartItemModel else { return }
        
        label.set(with: chartItemModel.label, delegateObject, additionalData)
        colorView.backgroundColor = chartItemModel.color.uiColor
    }
}
