// 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/fullscreen/ui_bundled/test/fullscreen_model_test_util.h"

#import "ios/chrome/browser/fullscreen/ui_bundled/fullscreen_model.h"
#import "ios/chrome/browser/toolbar/legacy/ui_bundled/fullscreen/toolbars_size.h"
#import "testing/gtest/include/gtest/gtest.h"

void SetUpFullscreenModelForTesting(FullscreenModel* model,
                                    CGFloat toolbar_height) {
  EXPECT_GE(toolbar_height, 0.0);
  ToolbarsSize* toolbars_size =
      [[ToolbarsSize alloc] initWithCollapsedTopToolbarHeight:0.0
                                     expandedTopToolbarHeight:toolbar_height
                                  expandedBottomToolbarHeight:0.0
                                 collapsedBottomToolbarHeight:0.0];
  model->SetToolbarsSize(toolbars_size);
  model->SetScrollViewHeight(2 * toolbar_height);
  model->SetContentHeight(2 * model->GetScrollViewHeight());
  model->ResetForNavigation();
  model->SetYContentOffset(0.0);
}

void SimulateFullscreenUserScrollWithDelta(FullscreenModel* model,
                                           CGFloat offset_delta) {
  model->SetScrollViewIsDragging(true);
  model->SetScrollViewIsScrolling(true);
  model->SetYContentOffset(model->GetYContentOffset() + offset_delta);
  model->SetScrollViewIsDragging(false);
  model->SetScrollViewIsScrolling(false);
}

void SimulateFullscreenUserScrollWithDeltaWithoutEnding(FullscreenModel* model,
                                                        CGFloat offset_delta) {
  model->SetScrollViewIsDragging(true);
  model->SetScrollViewIsScrolling(true);
  model->SetYContentOffset(model->GetYContentOffset() + offset_delta);
}

void SimulateFullscreenUserScrollForProgress(FullscreenModel* model,
                                             CGFloat progress) {
  ASSERT_GE(progress, 0.0);
  ASSERT_LE(progress, 1.0);
  SimulateFullscreenUserScrollWithDelta(
      model, GetFullscreenOffsetDeltaForProgress(model, progress));
}

// Simulates a user scroll to the bottom of the scroll view.
void SimulateScrollToBottom(FullscreenModel* model) {
  model->SetScrollViewIsDragging(true);
  model->SetScrollViewIsScrolling(true);
  CGFloat max_offset = model->GetContentHeight() - model->GetScrollViewHeight();
  model->SetYContentOffset(max_offset);
  model->SetScrollViewIsDragging(false);
  model->SetScrollViewIsScrolling(false);
}

CGFloat GetFullscreenOffsetDeltaForProgress(FullscreenModel* model,
                                            CGFloat progress) {
  CGFloat height_delta = model->get_toolbar_height_delta();
  CGFloat base_offset =
      GetFullscreenBaseOffsetForProgress(model, model->progress());
  CGFloat final_y_content_offset =
      base_offset + (1.0 - progress) * height_delta;
  return final_y_content_offset - model->GetYContentOffset();
}

CGFloat GetFullscreenBaseOffsetForProgress(FullscreenModel* model,
                                           CGFloat progress) {
  return model->GetYContentOffset() -
         (1.0 - progress) * model->get_toolbar_height_delta();
}
