class Videos::DispatchCallbackService < ApplicationService
  attr_reader :video, :params

  def initialize(params)
    @video  = Video.find_by(uuid: params[:VideoId])
    @params = params
  end

  def call
    return if video.blank?

    # TODO:: 拆分事件分发
    case params['EventType']
    when 'FileUploadComplete' then # 视频上传完成
      video.file_url = convert_https(params['FileUrl'])
      video.filesize = params['Size']
      video.upload_success
      video.save!
    when 'SnapshotComplete' then # 封面截图完成
      return if video.cover_url.present?
      video.update!(cover_url: params['CoverUrl'])
    when 'StreamTranscodeComplete' then # 转码完成
      #return if video.play_url.present?
      video.update!(play_url: params['FileUrl'], transcoded: true)
      CourseVideoUploadedJob.perform_later(video.id) if video.video_applies.exists?(status: 'agreed') #通过的情况基本上是课堂视频
    when 'DeleteMediaComplete' #完成云端视频删除
      video.update_column(:delete_state, Video::FINISH_DELETE)
    end

  rescue => ex
    Util.logger_error(ex)
  end
end