//
//  MultiProgress.swift
//  MVMCoreUI
//
//  Created by Ryan on 9/9/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit


@objcMembers open class MultiProgress: View {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    private let stack = Stack<StackModel>()
    
    var multiProgressModel: MultiProgressBarModel? {
        get { model as? MultiProgressBarModel }
    }
    
    var roundedCorners: Bool = false {
        didSet {
            if roundedCorners {
                layer.cornerRadius = (thicknessConstraint?.constant ?? defaultHeight) / 2
            } else {
                layer.cornerRadius = 0
            }
        }
    }
    
    var thicknessConstraint: NSLayoutConstraint?
    let defaultHeight: CGFloat = 8
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public required init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.init(frame: .zero)
        set(with: model, delegateObject, additionalData)
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    //--------------------------------------------------
    // MARK: - Lifecycles
    //--------------------------------------------------
    
    override open func setupView() {
        super.setupView()
       
        backgroundColor = .mvmCoolGray3
        clipsToBounds = true
        
        addSubview(stack)
        NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
        stack.backgroundColor = backgroundColor
        stack.contentView.backgroundColor = .clear
        stack.model = StackModel(molecules: [], axis: .horizontal, spacing: 2)
        stack.stackModel?.horizontalAlignment = .leading

        if thicknessConstraint == nil {
            thicknessConstraint = heightAnchor.constraint(equalToConstant: defaultHeight)
            thicknessConstraint?.isActive = true
        }
    }
    
    open override func reset() {
        super.reset()
        backgroundColor = .mvmCoolGray3
        stack.reset()
        set(with: [], nil, nil)
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    /// Creates the bars
    open func set(with progressList: [SingleProgressBarModel], _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        stack.removeAllItemViews()
        
        guard let stackModel = stack.stackModel else { return }
        
        var views: [StackItem] = []
        var models: [StackItemModel] = []
        for progressObject in progressList where progressObject.percent > 0.0 {
            let model = StackItemModel(percent: Int(progressObject.percent), horizontalAlignment: .fill, verticalAlignment: .fill)
            model.backgroundColor = progressObject.color
            models.append(model)
            views.append(StackItem())
        }
        stack.stackItems = views
        stackModel.molecules = models
        stack.set(with: stackModel, delegateObject, additionalData)
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let multiProgressModel = multiProgressModel else { return }
        
        roundedCorners = multiProgressModel.roundedCorners ?? false
        thicknessConstraint?.constant = multiProgressModel.thickness ?? defaultHeight
        set(with: multiProgressModel.progressList, delegateObject, additionalData)
    }
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        (model as? MultiProgressBarModel)?.thickness ?? 8
    }
}
