// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/chrome/browser/main_content/ui_bundled/main_content_ui_broadcasting_util.h"

#import "ios/chrome/browser/broadcaster/ui_bundled/chrome_broadcaster.h"
#import "ios/chrome/browser/main_content/ui_bundled/main_content_ui.h"
#import "ios/chrome/browser/main_content/ui_bundled/main_content_ui_state.h"
#import "ios/web/common/features.h"

void StartBroadcastingMainContentUI(id<MainContentUI> main_content,
                                    ChromeBroadcaster* broadcaster) {
  if (web::features::ShouldUseBroadcasterForSmoothScrolling()) {
    [broadcaster broadcastValue:@"contentSize"
                       ofObject:main_content.mainContentUIState
                       selector:@selector(broadcastScrollViewContentSize:)];
    [broadcaster broadcastValue:@"scrollViewSize"
                       ofObject:main_content.mainContentUIState
                       selector:@selector(broadcastScrollViewSize:)];
    [broadcaster broadcastValue:@"scrolling"
                       ofObject:main_content.mainContentUIState
                       selector:@selector(broadcastScrollViewIsScrolling:)];
    [broadcaster broadcastValue:@"dragging"
                       ofObject:main_content.mainContentUIState
                       selector:@selector(broadcastScrollViewIsDragging:)];
    [broadcaster broadcastValue:@"zooming"
                       ofObject:main_content.mainContentUIState
                       selector:@selector(broadcastScrollViewIsZooming:)];
    [broadcaster broadcastValue:@"contentInset"
                       ofObject:main_content.mainContentUIState
                       selector:@selector(broadcastScrollViewContentInset:)];
    [broadcaster broadcastValue:@"yContentOffset"
                       ofObject:main_content.mainContentUIState
                       selector:@selector(broadcastContentScrollOffset:)];
  }
}

void StopBroadcastingMainContentUI(ChromeBroadcaster* broadcaster) {
  if (web::features::ShouldUseBroadcasterForSmoothScrolling()) {
    [broadcaster
        stopBroadcastingForSelector:@selector(broadcastScrollViewContentSize:)];
    [broadcaster
        stopBroadcastingForSelector:@selector(broadcastScrollViewSize:)];
    [broadcaster
        stopBroadcastingForSelector:@selector(broadcastScrollViewIsScrolling:)];
    [broadcaster
        stopBroadcastingForSelector:@selector(broadcastScrollViewIsDragging:)];
    [broadcaster
        stopBroadcastingForSelector:@selector(broadcastScrollViewIsZooming:)];
    [broadcaster stopBroadcastingForSelector:@selector
                 (broadcastScrollViewContentInset:)];
    [broadcaster
        stopBroadcastingForSelector:@selector(broadcastContentScrollOffset:)];
  }
}
