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

import UIKit


@objcMembers open class ActionDetailWithImage: View {
    //------------------------------------------------------
    // MARK: - Outlets
    //------------------------------------------------------
    
    let headlineBodyButton = HeadlineBodyButton(frame: .zero)
    let imageLoader = LoadImageView(pinnedEdges: .all)
    
    //------------------------------------------------------
    // MARK: - Properties
    //------------------------------------------------------
    
    var buttonHeaderPadding: CGFloat = PaddingTwo
    
    //------------------------------------------------------
    // MARK: - Constraints
    //------------------------------------------------------
    
    var imageLeadingConstraint: NSLayoutConstraint?
    
    //------------------------------------------------------
    // MARK: - View Lifecycle
    //------------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        guard subviews.isEmpty else { return }
        
        setDefaultState()
        
        addSubview(imageLoader)
        addSubview(headlineBodyButton)
        
        headlineBodyButton.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
        headlineBodyButton.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
        
        layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: headlineBodyButton.bottomAnchor).isActive = true
        let leftContainerBottom = headlineBodyButton.bottomAnchor.constraint(equalTo: bottomAnchor)
        leftContainerBottom.priority = UILayoutPriority(249)
        leftContainerBottom.isActive = true
        
        imageLoader.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        layoutMarginsGuide.trailingAnchor.constraint(equalTo: imageLoader.trailingAnchor).isActive = true
        imageLeadingConstraint = imageLoader.leadingAnchor.constraint(greaterThanOrEqualTo: headlineBodyButton.trailingAnchor, constant: 16)
        imageLeadingConstraint?.isActive = true
        imageLoader.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor).isActive = true
        
        layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: imageLoader.bottomAnchor).isActive = true
        let imageloaderBottom = imageLoader.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
        imageloaderBottom.priority = UILayoutPriority(249)
        imageloaderBottom.isActive = true
    }
    
    override open func updateView(_ size: CGFloat) {
        super.updateView(size)
    
        headlineBodyButton.updateView(size)
        imageLoader.updateView(size)
    }
    
    //------------------------------------------------------
    // MARK: - Methods
    //------------------------------------------------------
    
    private func setDefaultState() {
        
        headlineBodyButton.headlineBody.headlineLabel.font = MFStyler.fontBoldTitleMedium()
        headlineBodyButton.headlineBody.messageLabel.font = MFStyler.fontRegularMicro()
        imageLoader.imageView.contentMode = .scaleAspectFit
        imageLoader.addSizeConstraintsForAspectRatio = true
        buttonHeaderPadding = PaddingTwo
    }
    
    override open func reset() {
        super.reset()
        
        headlineBodyButton.reset()
        imageLeadingConstraint?.constant = 16
        imageLoader.reset()
        setDefaultState()
    }
    
    // MARK:- MoleculeViewProtocol
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 197
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let model = model as? ActionDetailWithImageModel else { return }
        headlineBodyButton.set(with: model.headlineBodyButton, delegateObject, additionalData)
        imageLoader.set(with: model.image, delegateObject, additionalData)
    }
}
