//
//  LockupsPlanSMLXL.swift
//  MVMCoreUI
//
//  Created by Subhankar Acharya on 15/07/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

@objcMembers open class LockupsPlanSMLXL: View {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    public let planLabel = Label()
    public let headline = Label(fontStyle: .BoldTitleLarge)
    public let subHeadline = Label(fontStyle: .RegularTitleLarge)
    public let body = Label(fontStyle: .RegularBodySmall)
    public lazy var verticalStack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [(view: headline, model: StackItemModel()),
                                                    (view: subHeadline, model: StackItemModel()),
                                                    (view: body, model: StackItemModel(horizontalAlignment: .fill))],
                                             axis: .vertical, spacing: 0)
      }()
    public lazy var stack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [(view: planLabel, model: StackItemModel(horizontalAlignment: .fill, verticalAlignment: .leading)),
                                                    (view: verticalStack, model: StackItemModel(horizontalAlignment: .fill, verticalAlignment: .leading))],
                                             axis: .horizontal)
      }()
    
    //-------------------------------------------------------
    // MARK: - Lifecycle
    //-------------------------------------------------------
    open override func setupView() {
        super.setupView()
        planLabel.font = MFStyler.getMVA3FontSize(96, bold: true)
        planLabel.standardFontSize = 96
        addSubview(stack)
        planLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        planLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
        planLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
        stack.restack()
        verticalStack.restack()
    }
    
    public override func updateView(_ size: CGFloat) {
        super.updateView(size)
        stack.updateView(size)
        cropPlanLabel()
    }
    
    /// Ensures the label is the size of capital letters.
    public func cropPlanLabel() {
        guard let text = planLabel.text else { return }
        let attributedString = NSMutableAttributedString(attributedString: planLabel.attributedText ?? NSAttributedString(string: text))
        let range = NSRange(location: 0, length: attributedString.length)
        attributedString.removeAttribute(.paragraphStyle, range: range)
        attributedString.removeAttribute(.baselineOffset, range: range)
        
        // Accounts for the overshoot of non flat characters. 5% is typically the highest.
        let overshoot: CGFloat = planLabel.font.pointSize * 0.05
        
        // When messing with the maximum line height, the baseline needs to be adjusted.
        let baseLineOffset = planLabel.font.descender + (overshoot / 2.0)
        
        // We want the top to align with the top of the headline capitals, ignoring ascenders.
        let headlineMatchingOffset = headline.font.ascender - headline.font.capHeight
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.maximumLineHeight = planLabel.font.capHeight + overshoot + headlineMatchingOffset
        attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
        attributedString.addAttribute(.baselineOffset, value: baseLineOffset, range: range)
        planLabel.attributedText = attributedString
    }
    
    //----------------------------------------------------
    // MARK: - Molecule
    //------------------------------------------------------
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? LockupsPlanSMLXLModel else { return }
        planLabel.set(with: model.planLabel, delegateObject, additionalData)
        headline.set(with: model.headline, delegateObject, additionalData)
        subHeadline.set(with: model.subHeadline, delegateObject, additionalData)
        body.set(with: model.body, delegateObject, additionalData)
        updateAccessibilityLabel()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 121
    }
    
    open override func reset() {
        super.reset()
        stack.reset()
        planLabel.font = MFStyler.getMVA3FontSize(96, bold: true)
        planLabel.standardFontSize = 96
        headline.setFontStyle(.BoldTitleLarge)
        subHeadline.setFontStyle(.RegularTitleLarge)
        body.setFontStyle(.RegularBodySmall)
    }
        
    //--------------------------------------------------
    // MARK: - Accessibility
    //--------------------------------------------------
    func updateAccessibilityLabel() {
        isAccessibilityElement = true
        var message = ""
        
        if let planLabel = planLabel.text, !planLabel.isEmpty {
            message += planLabel + ", "
        }
        if let headline = headline.text, !headline.isEmpty {
            message += headline + ", "
        }
        if let subHeadline = subHeadline.text, !subHeadline.isEmpty {
            message += subHeadline + ", "
        }
        if let body = body.text, !body.isEmpty {
            message += body
        }
        accessibilityLabel = message
    }
}



