// yt - A fully featured command line YouTube client
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use serde_json::{json, Value};

use crate::{constants, storage::video_database::YtDlpOptions};

// {
//     "ratelimit": conf.ratelimit if conf.ratelimit > 0 else None,
//     "retries": conf.retries,
//     "merge_output_format": conf.merge_output_format,
//     "restrictfilenames": conf.restrict_filenames,
//     "ignoreerrors": False,
//     "postprocessors": [{"key": "FFmpegMetadata"}],
//     "logger": _ytdl_logger
// }

pub fn download_opts(additional_opts: YtDlpOptions) -> serde_json::Map<String, serde_json::Value> {
    match json!({
      "extract_flat": false,
      "extractor_args": {
        "youtube": {
          "comment_sort": [
            "top"
          ],
          "max_comments": [
            "150",
            "all",
            "100"
          ]
        }
      },
      "ffmpeg_location": env!("FFMPEG_LOCATION"),
      "format": "bestvideo[height<=?1080]+bestaudio/best",
      "fragment_retries": 10,
      "getcomments": true,
      "ignoreerrors": false,
      "retries": 10,

      "writeinfojson": true,
      "writeannotations": true,
      "writesubtitles": true,
      "writeautomaticsub": true,

      "outtmpl": {
        "default": constants::download_dir().join("%(channel)s/%(title)s.%(ext)s"),
        "chapter": "%(title)s - %(section_number)03d %(section_title)s [%(id)s].%(ext)s"
      },
      "compat_opts": {},
      "forceprint": {},
      "print_to_file": {},
      "windowsfilenames": false,
      "restrictfilenames": false,
      "trim_file_names": false,
      "postprocessors": [
        {
          "api": "https://sponsor.ajay.app",
          "categories": [
            "interaction",
            "intro",
            "music_offtopic",
            "sponsor",
            "outro",
            "poi_highlight",
            "preview",
            "selfpromo",
            "filler",
            "chapter"
          ],
          "key": "SponsorBlock",
          "when": "after_filter"
        },
        {
          "force_keyframes": false,
          "key": "ModifyChapters",
          "remove_chapters_patterns": [],
          "remove_ranges": [],
          "remove_sponsor_segments": [
            "sponsor"
          ],
          "sponsorblock_chapter_title": "[SponsorBlock]: %(category_names)l"
        },
        {
          "add_chapters": true,
          "add_infojson": null,
          "add_metadata": false,
          "key": "FFmpegMetadata"
        },
        {
          "key": "FFmpegConcat",
          "only_multi_video": true,
          "when": "playlist"
        }
      ]
    }) {
        serde_json::Value::Object(mut obj) => {
            obj.insert(
                "subtitleslangs".to_owned(),
                serde_json::Value::Array(
                    additional_opts
                        .subtitle_langs
                        .split(',')
                        .map(|val| Value::String(val.to_owned()))
                        .collect::<Vec<_>>(),
                ),
            );
            obj
        }
        _ => unreachable!("This is an object"),
    }
}