//
//  VisibleBehaviorForVideo.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 3/29/21.
//  Copyright © 2021 Verizon Wireless. All rights reserved.
//

import Foundation

open class VisibleBehaviorForVideo: PageVisibilityBehavior, PageScrolledBehavior {
    var model: PageBehaviorModelProtocol
    
    required public init(model: PageBehaviorModelProtocol, delegateObject: MVMCoreUIDelegateObject?) {
        self.model = model
    }

    public func onPageShown(_ delegateObject: MVMCoreUIDelegateObject?) {
        guard let model = (model as? VisibleBehaviorForVideoModel)?.videoModel,
              let view = model.view,
              view.isVisibleInDelegate() else { return }
        model.halted = false
    }
    
    public func onPageHidden(_ delegateObject: MVMCoreUIDelegateObject?) {
        (model as? VisibleBehaviorForVideoModel)?.videoModel?.halted = true
    }
    
    public func pageScrolled(scrollView: UIScrollView, _ delegateObject: MVMCoreUIDelegateObject?) {
        // If visible to not visible, pause video.
        // If not visible to visible, unpause if needed, add visible behavior
        guard let model = (model as? VisibleBehaviorForVideoModel)?.videoModel,
              let view = model.view else { return }
        model.halted = !view.isVisible(in: scrollView)
    }
}
