//
//  HeadersH2PricingTwoRows.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 16/06/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

@objcMembers open class HeadersH2PricingTwoRows: HeaderView {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    public let headline = Label(fontStyle: .RegularTitleLarge)
    public let body = Label(fontStyle: .RegularMicro)
    public let subBody = Label(fontStyle: .RegularMicro)
    public let body2 = Label(fontStyle: .RegularMicro)
    public let subBody2 = Label(fontStyle: .RegularMicro)
    public let body3 = Label(fontStyle: .RegularMicro)
    public let subBody3 = Label(fontStyle: .RegularMicro)
    public let verticalLine1 = Line()
    public let verticalLine2 = Line()
    public lazy var verticalStack1: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [body, subBody], spacing: 0)
      }()
    public lazy var verticalStack2: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [body2, subBody2], spacing: 0)
      }()
    public lazy var verticalStack3: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [body3, subBody3], spacing: 0)
      }()
    public lazy var horizontalStack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [(view: verticalStack1, model: StackItemModel(percent: 29, verticalAlignment: .top)),
                                                    (view: verticalLine1, model: StackItemModel(verticalAlignment: .top)),
                                                    (view: verticalStack2, model: StackItemModel(horizontalAlignment: .leading, verticalAlignment: .top)),
                                                    (view: verticalLine2, model: StackItemModel(verticalAlignment: .top)),
                                                    (view: verticalStack3, model: StackItemModel(percent: 32, verticalAlignment: .top))],
                                             axis: .horizontal)
      }()
    public lazy var stack: Stack<StackModel> = {
        return Stack<StackModel>.createStack(with: [headline, horizontalStack], spacing: 8)
      }()
    
    //-------------------------------------------------------
    // MARK: - Lifecycle
    //-------------------------------------------------------
    open override func setupView() {
        super.setupView()
        setDefaults()
        body.lineBreakMode = .byTruncatingTail
        body2.lineBreakMode = .byTruncatingTail
        body3.lineBreakMode = .byTruncatingTail
        
        verticalLine1.widthConstraint?.isActive = true
        verticalLine1.backgroundColor = .mvmBlack
        verticalLine2.widthConstraint?.isActive = true
        verticalLine2.backgroundColor = .mvmBlack
        
        addMolecule(stack)
        stack.restack()
        verticalStack1.restack()
        verticalStack2.restack()
        verticalStack3.restack()
        horizontalStack.restack()
        setLineHeight()
    }
    
    open func setDefaults() {
        headline.setFontStyle(.BoldTitleLarge)
        body.setFontStyle(.RegularMicro)
        subBody.setFontStyle(.RegularMicro)
        body2.setFontStyle(.RegularMicro)
        subBody2.setFontStyle(.RegularMicro)
        body3.setFontStyle(.RegularMicro)
        subBody3.setFontStyle(.RegularMicro)
        subBody.textColor = .mvmCoolGray6
        subBody2.textColor = .mvmCoolGray6
        subBody3.textColor = .mvmCoolGray6
    }
    
    open func setLineHeight() {
        verticalLine1.heightConstraint?.isActive = false
        verticalLine1.heightConstraint = verticalLine1.heightAnchor.constraint(equalTo: body2.heightAnchor, multiplier: 1)
        verticalLine1.heightConstraint?.isActive = true
        
        verticalLine2.heightConstraint?.isActive = false
        verticalLine2.heightConstraint = verticalLine2.heightAnchor.constraint(equalTo: body3.heightAnchor, multiplier: 1)
        verticalLine2.heightConstraint?.isActive = true
    }
    
    //----------------------------------------------------
    // 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? HeadersH2PricingTwoRowsModel else { return }
        headline.set(with: model.headline, delegateObject, additionalData)
        verticalStack1.updateContainedMolecules(with: [model.body, model.subBody], delegateObject, additionalData)
        verticalStack2.updateContainedMolecules(with: [model.body2, model.subBody2], delegateObject, additionalData)
        verticalStack3.updateContainedMolecules(with: [model.body3, model.subBody3], delegateObject, additionalData)
        setLineHeight()
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 121
    }
    
    open override func reset() {
        super.reset()
        setDefaults()
    }
}
