//
//  StringAndMoleculeView.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 1/17/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


open class StringAndMoleculeView: View {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    var label = Label(fontStyle: .RegularBodySmall)
    var molecule: MoleculeViewProtocol

    var leftWidthConstraint: NSLayoutConstraint?
    @Percent var percentage: CGFloat = 5
    
    var constraintBetweenViews: NSLayoutConstraint?
    var spaceBetweenViews: CGFloat = 0 {
        didSet {
            if spaceBetweenViews != oldValue {
                constraintBetweenViews?.constant = spaceBetweenViews
                setNeedsDisplay()
            }
        }
    }
    
    //--------------------------------------------------
    // MARK: - Inits
    //--------------------------------------------------
    
    public init(string: String, molecule: MoleculeViewProtocol) {
        self.label.text = string
        self.molecule = molecule
        super.init(frame: .zero)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public required init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
        let moleculeModel = model as? StringAndMoleculeModel
        label.text = moleculeModel?.string
        self.molecule = moleculeModel?.molecule as! MoleculeViewProtocol
        super.init(model: model, delegateObject, additionalData)
    }
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    override public func setupView() {
        super.setupView()
        
        guard subviews.count == 0 else { return }
    
        addSubview(label)
        addSubview(molecule)
        
        NSLayoutConstraint.constraintPinSubview(label, pinTop: true, pinBottom: false, pinLeft: true, pinRight: false)
        bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor).isActive = true
        
        let lowBottomConstraint = bottomAnchor.constraint(equalTo: label.bottomAnchor)
        lowBottomConstraint.priority = UILayoutPriority(rawValue: 200)
        lowBottomConstraint.isActive = true
                
        NSLayoutConstraint.constraintPinSubview(molecule, pinTop: true, pinBottom: true, pinLeft: false, pinRight: true)
        constraintBetweenViews = molecule.leftAnchor.constraint(equalTo: label.rightAnchor, constant: spaceBetweenViews)
        constraintBetweenViews?.priority = .required
        constraintBetweenViews?.isActive = true
        
        setContentHuggingPriority(.defaultHigh, for: .vertical)
        setContentHuggingPriority(.defaultHigh, for: .horizontal)
        updateLeftViewWidthConstraint(percentage)
    }
    
    override open func updateView(_ size: CGFloat) {
        super.updateView(size)
        (molecule as? MVMCoreViewProtocol)?.updateView(size)
        label.updateView(size)
    }
    
    override open func reset() {
        super.reset()
        label.reset()
        molecule.reset()
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? StringAndMoleculeModel else { return }
        label.text = model.string
        label.textColor = model.stringColor.uiColor
        molecule.set(with: model.molecule, delegateObject, additionalData)
    }
    
    func updateLeftViewWidthConstraint(_ percent: CGFloat) {
        percentage = percent
        leftWidthConstraint?.isActive = false
        leftWidthConstraint = label.widthAnchor.constraint(equalTo: widthAnchor, multiplier: CGFloat(percent / 100), constant: 0)
        leftWidthConstraint?.isActive = true
    }
}
