//
//  LeftRightLabelView.swift
//  MVMCoreUI
//
//  Created by Christiano, Kevin on 5/20/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//



@objcMembers open class LeftRightLabelView: View {
    //------------------------------------------------------
    // MARK: - Outlets
    //------------------------------------------------------
    
    let leftTextLabel = Label(fontStyle: .BoldBodySmall)
    let rightTextLabel = Label(fontStyle: .BoldBodySmall)
    
    //------------------------------------------------------
    // MARK: - Constraints
    //------------------------------------------------------
    
    var rightTextLabelLeading: NSLayoutConstraint?
    var leftTextLabelTrailing: NSLayoutConstraint?
    var rightTextLabelTrailing: NSLayoutConstraint?
    
    private func constrainLeftLabelOnly() {
        
        deactivateMiddleConstraint()
        leftTextLabelTrailing?.isActive = false
        leftTextLabelTrailing = layoutMarginsGuide.trailingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor)
        leftTextLabelTrailing?.isActive = true
    }
    
    private func constrainRightLabelOnly() {
        
        deactivateMiddleConstraint()
        rightTextLabelLeading?.isActive = false
        rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor)
        rightTextLabelLeading?.isActive = true
    }
    
    private func deactivateMiddleConstraint() {
        
        leftTextLabelTrailing?.isActive = false
        rightTextLabelLeading?.isActive = false
    }
    
    override open func setupView() {
        super.setupView()
        
        addSubview(leftTextLabel)
        addSubview(rightTextLabel)
        
        leftTextLabel.textAlignment = .left
        rightTextLabel.textAlignment = .right
        
        constrainBothLabels()
    }
    
    //------------------------------------------------------
    // MARK: - View Lifecycle
    //------------------------------------------------------
    
    override open func updateView(_ size: CGFloat) {
        super.updateView(size)
        
        leftTextLabel.updateView(size)
        rightTextLabel.updateView(size)
        
        // Resolves text layout issues found between both dynamically sized labels, number is not exact but performs as required.
        if leftTextLabel.hasText && rightTextLabel.hasText {
            let padding = MFStyler.defaultHorizontalPadding(forSize: size) * 2
            let maximumTextWidth = (size - (padding + Padding.Four)) * 0.4
            // Subtracting 10 resolves issues of SE and iPad
            rightTextLabel.preferredMaxLayoutWidth = round(maximumTextWidth) - 10
        } else {
            rightTextLabel.preferredMaxLayoutWidth = 0
        }
    }
    
    //------------------------------------------------------
    // MARK: - Setup
    //------------------------------------------------------
    
    private func constrainBothLabels() {
        
        leftTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
        leftTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
        
        let leftTextBottom = bottomAnchor.constraint(equalTo: leftTextLabel.bottomAnchor)
        leftTextBottom.priority = UILayoutPriority(249)
        leftTextBottom.isActive = true
        
        bottomAnchor.constraint(greaterThanOrEqualTo: leftTextLabel.bottomAnchor).isActive = true
        
        rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(greaterThanOrEqualTo: leftTextLabel.trailingAnchor, constant: Padding.Four)
        rightTextLabelLeading?.isActive = true
        
        rightTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
        
        rightTextLabelTrailing = layoutMarginsGuide.trailingAnchor.constraint(equalTo: rightTextLabel.trailingAnchor)
        rightTextLabelTrailing?.isActive = true
        
        let rightTextBottom = bottomAnchor.constraint(equalTo: rightTextLabel.bottomAnchor)
        rightTextBottom.priority = UILayoutPriority(rawValue: 249)
        rightTextBottom.isActive = true
        
        bottomAnchor.constraint(greaterThanOrEqualTo: rightTextLabel.bottomAnchor).isActive = true
        
        let leftTextWidth = leftTextLabel.widthAnchor.constraint(equalTo: layoutMarginsGuide.widthAnchor, multiplier: 0.6)
        leftTextWidth.priority = UILayoutPriority(rawValue: 995)
        leftTextWidth.isActive = true
        
        let rightTextWidth = rightTextLabel.widthAnchor.constraint(equalTo: layoutMarginsGuide.widthAnchor, multiplier: 0.4)
        rightTextWidth.priority = UILayoutPriority(rawValue: 995)
        rightTextWidth.isActive = true
        
        leftTextLabel.setContentCompressionResistancePriority(.required, for: .vertical)
        rightTextLabel.setContentCompressionResistancePriority(.required, for: .vertical)
        rightTextLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
    }
    
    override open func reset() {
        super.reset()
        
        deactivateMiddleConstraint()
        constrainBothLabels()
        leftTextLabel.text = ""
        rightTextLabel.text = ""
        leftTextLabel.textAlignment = .left
        rightTextLabel.textAlignment = .right
        backgroundColor = nil
    }
    
    //------------------------------------------------------
    // MARK: - Atomization
    //------------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let leftRightLabelModel = model as? LeftRightLabelModel else { return }
        
        leftTextLabel.set(with: leftRightLabelModel.leftText, delegateObject, additionalData)
        rightTextLabel.setOptional(with: leftRightLabelModel.rightText, delegateObject, additionalData)
        
        // Label defaults to .left if no textAlignment value is provided in model.
        rightTextLabel.textAlignment = leftRightLabelModel.rightText?.textAlignment ?? .right
        
        if !leftTextLabel.hasText {
            constrainRightLabelOnly()
            
        } else if !rightTextLabel.hasText {
            constrainLeftLabelOnly()
        }
    }
}
