//
//  ThreeLayerTemplate.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 7/23/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit

@objcMembers open class ThreeLayerTemplate<TemplateModel: ThreeLayerPageTemplateModel>: ThreeLayerViewController, TemplateProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var templateModel: TemplateModel?

    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    open override func parsePageJSON() throws {
        try parseTemplate(json: loadObject?.pageJSON)
        try super.parsePageJSON()
    }
    
    open override func handleNewData() {
        topViewOutsideOfScroll = templateModel?.anchorHeader ?? false
        bottomViewOutsideOfScroll = templateModel?.anchorFooter ?? false
        super.handleNewData()
    }

    open override func viewForTop() -> UIView? {
        guard let headerModel = templateModel?.header,
            let molecule = ModelRegistry.createMolecule(headerModel, delegateObject: delegateObjectIVar)
            else { return  nil }
        
        return molecule
    }
    
    open override func viewForMiddle() -> UIView? {
        guard let middleModel = templateModel?.middle,
            let molecule = ModelRegistry.createMolecule(middleModel, delegateObject: delegateObjectIVar)
            else { return  nil }
        
        return molecule
    }
    
    override open func viewForBottom() -> UIView? {
        guard let footerModel = templateModel?.footer,
            let molecule = ModelRegistry.createMolecule(footerModel, delegateObject: delegateObjectIVar)
            else { return  nil }
        
        return molecule
    }
    
    //--------------------------------------------------
    // MARK: - Spacing
    //--------------------------------------------------
    
    open override func spaceBetweenTopAndMiddle() -> CGFloat? { 0 }
    
    open override func spaceBetweenMiddleAndBottom() -> CGFloat? { nil }
}
