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

import UIKit
import MVMCoreUI

open class Step: View {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var headline = Label(fontStyle: .BoldMicro)
    public var bodyTop = Label(fontStyle: .RegularMicro)
    public var bodyBottom = Label(fontStyle: .RegularMicro)
    public var stateImage = ImageView()
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------
    
    public var heightConstraint: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Computed Properties
    //--------------------------------------------------
    
    public var stepModel: StepModel? {
        return model as? StepModel
    }
    
    public var state: StepModel.State? {
        return stepModel?.state
    }
    
    //--------------------------------------------------
    // MARK: - Life Cycle
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        addSubview(stateImage)
        addSubview(headline)
        addSubview(bodyTop)
        addSubview(bodyBottom)
        
        stateImage.contentMode = .scaleAspectFit
        stateImage.layer.backgroundColor = UIColor.white.cgColor
        stateImage.topAnchor.constraint(equalTo: topAnchor).isActive = true
        stateImage.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        stateImage.heightAnchor.constraint(equalToConstant: 18).isActive = true
        stateImage.widthAnchor.constraint(equalToConstant: 18).isActive = true
        
        headline.topAnchor.constraint(equalTo: topAnchor, constant: 2).isActive = true
        headline.leadingAnchor.constraint(equalTo: stateImage.trailingAnchor, constant: Padding.Four).isActive = true
        trailingAnchor.constraint(equalTo: headline.trailingAnchor).isActive = true
        
        bodyTop.topAnchor.constraint(equalTo: headline.bottomAnchor).isActive = true
        bodyTop.leadingAnchor.constraint(equalTo: stateImage.trailingAnchor, constant: Padding.Four).isActive = true
        trailingAnchor.constraint(equalTo: bodyTop.trailingAnchor).isActive = true
        
        bodyBottom.topAnchor.constraint(equalTo: bodyTop.bottomAnchor).isActive = true
        bodyBottom.leadingAnchor.constraint(equalTo: stateImage.trailingAnchor, constant: Padding.Four).isActive = true
        trailingAnchor.constraint(equalTo: bodyBottom.trailingAnchor).isActive = true
        let bodyBottomConstraint = bottomAnchor.constraint(greaterThanOrEqualTo: bodyBottom.bottomAnchor)
        bodyBottomConstraint.priority = .defaultHigh
        bodyBottomConstraint.isActive = true
        
        heightConstraint = heightAnchor.constraint(equalToConstant: 58)
        heightConstraint?.priority = .defaultLow
        heightConstraint?.isActive = true
    }
    
    public override func reset() {
        super.reset()
        
        headline.reset()
        bodyTop.reset()
        bodyBottom.reset()
        stateImage.reset()
        heightConstraint?.isActive = true
    }
    
    //------------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //------------------------------------------------------
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 64
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? StepModel else { return }
        
        headline.set(with: model.headline, delegateObject, additionalData)
        
        if let bodyTopModel = model.bodyTop {
            bodyTop.set(with: bodyTopModel, delegateObject, additionalData)
        }
        
        if let bodyBottomModel = model.bodyBottom {
            bodyBottom.set(with: bodyBottomModel, delegateObject, additionalData)
        }
        
        stateImage.image = model.state?.image()
        
        if let iconColor = model.iconColor?.uiColor {
            stateImage.tintColor = iconColor
        } else {
            stateImage.tintColor = model.state?.color()
        }
    }
}
