//
//  MVMCoreUITopAlertExpandableView+Extension.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 9/15/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//


extension MVMCoreUITopAlertExpandableView: MoleculeViewProtocol {
    
    public func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        defaultSetup()
        guard let model = model as? CollapsableNotificationModel else { return }
        backgroundColor = model.backgroundColor?.uiColor ?? .mvmGreen
        collapseTime = model.collapseTime
        onlyShowTopMessageWhenCollapsed = !model.alwaysShowTopLabel

        var actionMap = model.button?.action.toJSON()
        if let title = model.button?.title {
            actionMap?.updateValue(title, forKey: KeyTitle)
        }
        buttonView?.setupCloseButton(model.closeButton != nil, animationDelegate: MVMCoreUITopAlertView.sharedGlobal()?.animationDelegate)
        setTopMessage(model.topLabel.text, message: model.headline.text, subMessage: model.body?.text, contentColor: model.headline.textColor?.uiColor ?? .white, actionMap: actionMap, additionalData: nil)
        expand(false)
        
        if let button = shortView?.button,
            let topActionMap = model.topAction?.toJSON() {
            MVMCoreUITopAlertBaseView.addAction(to: button, actionMap: topActionMap, additionalData: nil)
            shortView?.label?.accessibilityTraits = .button
        }
        
        if let accessibilityIdentifier = model.accessibilityIdentifier {
            self.accessibilityIdentifier = accessibilityIdentifier
        }
    }
}

extension MVMCoreUITopAlertExpandableView: StatusBarUI {
    func getStatusBarUI() -> (color: UIColor, style: UIStatusBarStyle) {
        if shortView?.label?.text?.count ?? 0 > 0 {
            let color = backgroundColor ?? UIColor.mvmGreen
            var greyScale: CGFloat = 0
            if shortView?.label?.textColor.getWhite(&greyScale, alpha: nil) ?? false {
                return (color, greyScale > 0.5 ? .lightContent : .default)
            } else {
                return (color, .default)
            }
        } else {
            return (.white, .default)
        }
    }
}
