# 视频管理
module AliyunVod::Service::VideoManage
  # 修改视频信息
  def update_video_info(video_id, **opts)
    params = {
      Action: 'UpdateVideoInfo',
      VideoId: video_id
    }.merge(base_params)

    params = opts.merge(params)

    result = request(:post, params)

    result
  end

  # 获取视频信息
  def get_video_info(video_id)
    params = {
      Action: 'GetVideoInfo',
      VideoId: video_id
    }.merge(base_params)

    result = request(:post, params)

    result
  end

  # 读取视频编码格式与视频格式
  def get_meta_code_info(video_id)
    params = {
      Action: 'GetMezzanineInfo',
      VideoId: video_id,
      AdditionType: 'video'
    }.merge(base_params)

    result = request(:post, params)
    Rails.logger.info("#######:#{result['Mezzanine']['VideoStreamList'][0]['CodecName']}")
    codecnamne = result['Mezzanine']['VideoStreamList'].first['CodecName']
    file_url = result['Mezzanine']['FileURL']
    format = file_url&.split(".")&.last

    {codecnamne: codecnamne, format: format}
  rescue => e
    Rails.logger.info "读取视频编码信息失败: #{video_id}, #{e.message}"
    {codecnamne: "", format: ""}
  end

  # 删除视频信息
  def delete_video(video_ids)
    params = {
      Action: 'DeleteVideo',
      VideoIds: video_ids.join(',')
    }.merge(base_params)

    result = request(:post, params)

    result
  end

  # 视频播放数据
  def video_data(video_id, start_time, end_time)
    params = {
      Action: 'DescribePlayVideoStatis',
      VideoId: video_id,
      StartTime: start_time,
      EndTime: end_time,
    }.merge(base_params)

    puts params
    result = request(:post, params)

    result
  end
end