#!/usr/bin/env ruby -W
#
# detect-crop
#
# Copyright (c) 2013-2020 Don Melton
#

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'video_transcoding/cli'

module VideoTranscoding
  class Command
    include CLI

    def about
      <<HERE
detect-crop #{VERSION}
#{COPYRIGHT}
HERE
    end

    def usage
      <<HERE
Detect crop values for video file or disc image directory.

Usage: #{$PROGRAM_NAME} [OPTION]... [FILE|DIRECTORY]...

    --scan          list title(s) and tracks in video media and exit
    --title NUMBER  select numbered title in video media
                      (default: main feature or first listed)
    --constrain     constrain crop to optimal shape
    --values-only   output only unambiguous crop values, not commands

-v, --verbose       increase diagnostic information
-q, --quiet         decrease     "           "

-h, --help          display this help and exit
    --version       output version information and exit

Requires `HandBrakeCLI` and `ffmpeg`.
HERE
    end

    def initialize
      super
      @scan         = false
      @title        = nil
      @constrain    = false
      @values_only  = false
    end

    def define_options(opts)
      opts.on('--scan')               { @scan = true }
      opts.on('--title ARG', Integer) { |arg| @title = arg }
      opts.on('--constrain')          { @constrain = true }
      opts.on('--values-only')        { @values_only = true }
    end

    def configure
      HandBrake.setup
      FFmpeg.setup
    end

    def process_input(arg)
      Console.info "Processing: #{arg}..."

      if @scan
        media = Media.new(path: arg, title: @title)
        Console.debug media.info.inspect
        puts media.summary
        return
      end

      media = Media.new(path: arg, title: @title, autocrop: true, extended: false)
      Console.debug media.info.inspect
      width, height = media.info[:width], media.info[:height]
      directory = media.info[:directory]
      hb_crop = media.info[:autocrop]
      hb_crop = Crop.constrain(hb_crop, width, height) if @constrain
      shell_path = arg.shellescape

      print_transcode = ->(crop) do
        puts
        print 'transcode-video '
        print "--title #{media.info[:title]} " if directory
        puts "--crop #{Crop.handbrake_string(crop)} #{shell_path}"
        puts
      end

      print_all = ->(crop) do
        puts
        puts "mpv --no-audio --vf=\"lavfi=[drawbox=#{Crop.drawbox_string(crop, width, height)}:invert:1]\" #{shell_path}"
        puts "mpv --no-audio --vf=crop=#{Crop.player_string(crop, width, height)} #{shell_path}"
        print_transcode.call crop
      end

      if directory
        if @values_only
          puts Crop.handbrake_string(hb_crop)
        else
          print_transcode.call hb_crop
        end
      else
        ff_crop = Crop.detect(arg, media.info[:duration], width, height)
        ff_crop = Crop.constrain(ff_crop, width, height) if @constrain

        if hb_crop == ff_crop
          if @values_only
            puts Crop.handbrake_string(hb_crop)
          else
            Console.info 'Results from HandBrakeCLI and ffmpeg are identical...'
            print_all.call hb_crop
          end
        else
          fail "results from HandBrakeCLI and ffmpeg differ: #{arg}" if @values_only
          Console.warn 'Results differ...'
          puts
          puts '# From HandBrakeCLI:'
          print_all.call hb_crop
          puts '# From ffmpeg:'
          print_all.call ff_crop
        end
      end
    end
  end
end

VideoTranscoding::Command.new.run
