#pragma once

#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <string>

namespace smartcut::media {

struct AdScanRegion {
  double left{0.10};
  double top{0.10};
  double right{0.10};
  double bottom{0.10};
};

struct AdDetectiveParameters {
  double scene_threshold{0.30};
  double average_black_level{0.075};
  double peak_black_level{0.20};
  double contrast_threshold{0.15};
  std::size_t minimum_frame_spacing{5};
  AdScanRegion region;
  bool i_and_p_frames_only{false};
  bool detect_black_transitions{true};
};

struct AdFrameStatistics {
  std::size_t frame{};
  std::int64_t pts{};
  double average{};
  double peak{};
  double contrast{};
  double scene_score{};
  bool black{};
  char picture_type{'?'};
};

using AdProgress = std::function<void(const AdFrameStatistics&)>;

std::string detect_ad_transitions(const std::filesystem::path& path, std::size_t start_frame,
                                  const AdDetectiveParameters& parameters,
                                  const AdProgress& progress = {});

std::string calibrate_ad_thresholds(const std::filesystem::path& path, std::size_t first_frame,
                                    std::size_t end_frame, const AdScanRegion& region,
                                    bool i_and_p_frames_only = false);

std::string detect_ad_transitions(
    const std::filesystem::path& path, std::size_t start_frame = 0,
    double scene_threshold = 0.30, double black_level = 0.075,
    std::size_t minimum_frame_spacing = 5,
    const std::function<void(std::size_t)>& progress = {});

}  // namespace smartcut::media
