#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import shlex

from fastflix.encoders.common.helpers import Command, generate_all, generate_color_details
from fastflix.models.encode import VAAPIH264Settings
from fastflix.models.fastflix import FastFlix

logger = logging.getLogger("fastflix")


def build(fastflix: FastFlix):
    settings: VAAPIH264Settings = fastflix.current_video.video_settings.video_encoder_settings
    start_extra = [
        "-init_hw_device",
        f"vaapi=hwdev:{settings.vaapi_device}",
        "-hwaccel",
        "vaapi",
        "-hwaccel_device",
        "hwdev",
        "-hwaccel_output_format",
        "vaapi",
    ]
    beginning, ending, output_fps = generate_all(
        fastflix,
        "h264_vaapi",
        start_extra=start_extra,
        enable_opencl=False,
        vaapi=True,
    )

    beginning.extend(
        [
            "-rc_mode",
            str(settings.rc_mode),
            "-async_depth",
            str(settings.async_depth),
            "-b_depth",
            str(settings.b_depth),
            "-idr_interval",
            str(settings.idr_interval),
        ]
    )
    beginning.extend(generate_color_details(fastflix))
    beginning.extend(["-filter_hw_device", "hwdev"])

    if settings.aud:
        beginning.extend(["-aud", "1"])

    if settings.low_power:
        beginning.extend(["-low-power", "1"])

    if settings.level:
        beginning.extend(["-level", str(settings.level)])

    # ffmpeg -init_hw_device vaapi=foo:/dev/dri/renderD128  -hwaccel_device foo -i input.mp4 -filter_hw_device foo -vf 'format=nv12|vaapi,hwupload'

    # if not fastflix.current_video.video_settings.remove_hdr:

    # Currently unsupported https://github.com/xiph/rav1e/issues/2554
    #         rav1e_options = []
    # if side_data.master_display:
    #     rav1e_options.append(
    #         "mastering-display="
    #         f"G{side_data.master_display.green}"
    #         f"B{side_data.master_display.blue}"
    #         f"R{side_data.master_display.red}"
    #         f"WP{side_data.master_display.white}"
    #         f"L{side_data.master_display.luminance}"
    #     )
    #
    # if side_data.cll:
    #     rav1e_options.append(f"content-light={side_data.cll}")
    # if rav1e_options:
    #     opts = ":".join(rav1e_options)
    #     beginning += f'-rav1e-params "{opts}"'
    #
    # if not settings.single_pass:
    #     pass_log_file = fastflix.current_video.work_path / f"pass_log_file_{secrets.token_hex(10)}"
    #     beginning += f'-passlogfile "{pass_log_file}" '

    pass_type = "bitrate" if settings.bitrate else "QP"

    if not settings.bitrate:
        command_1 = (
            beginning + ["-qp", str(settings.qp)] + (shlex.split(settings.extra) if settings.extra else []) + ending
        )
        return [Command(command=command_1, name=f"{pass_type}", exe="ffmpeg")]

    # if settings.single_pass:
    command_1 = (
        beginning + ["-b:v", settings.bitrate] + (shlex.split(settings.extra) if settings.extra else []) + ending
    )
    return [Command(command=command_1, name=f"{pass_type}", exe="ffmpeg")]
    # else:
    #     command_1 = f"{beginning} -b:v {settings.bitrate} -pass 1 {settings.extra if settings.extra_both_passes else ''} -an -f matroska {null}"
    #     command_2 = f"{beginning} -b:v {settings.bitrate} -pass 2 {settings.extra} {ending}"
    #     return [
    #         Command(command=command_1, name=f"First pass {pass_type}", exe="ffmpeg"),
    #         Command(command=command_2, name=f"Second pass {pass_type} ", exe="ffmpeg"),
    #     ]
