"""
Copyright 2021 <NAME>
"""
from pathlib import Path
from typing import Optional

import typer

from converter import video_to_text_converter


def version_callback(value: bool):
    if value:
        print("Joitek-Video2Text Converter    0.0.1")
        raise typer.Exit()


def interactive_callback(value: bool) -> None:
    if value:
        typer.clear()
        typer.echo("=======================================================================")
        typer.echo("Joitek-Video2Text Converter")
        typer.echo("Welcome to interactive mode!")
        typer.echo("=======================================================================")
        typer.echo()
        typer.echo(
            "The program will divide the video into chunks according to the exact number of minutes it has.\n"
            "1 chunk = 1 minute\n\n"
            "The remaining seconds will be ignored. It will be fixed in new versions ;)\n\n"
            "e.g. a video with a duration of 1h: 00m: 18s, those 18 seconds will be ignored.\n"
            "The rest will turn into minutes.\n\n"
            "Start with the basis\n\n"
            "For ignore something, press ENTER\n"
            "For exit, press Ctrl + C or any combination. We make sure you don't have any problems"
        )
        typer.echo("=======================================================================\n\n")
        try:

            v: Path = typer.prompt("Path of the video to convert.\n"
                                   "e.g. C:\\myfolder\\myvideo.mp4\n\n"
                                   "Path of the video to convert", type=Path)
            typer.echo()
            typer.echo()
            while not v.exists() or v.is_dir():
                if v.is_dir():
                    typer.echo(f"[{v}] points to a dir, not a file\n\n")
                else:
                    typer.echo(f"[{v}] not exist\n\n")
                v = typer.prompt("Path of the video to convert", type=Path)
                typer.echo()
                typer.echo()
            v = v.resolve()

            o: Path = typer.prompt("Directory path where the file with the transcript will be saved.\n"
                                   f"If ignored, the current directory will be used.\n\n"
                                   "Output dir path",
                                   type=Path,
                                   default=Path.cwd(),
                                   show_default=False)

            while not o.exists() or o.is_file():
                if o.is_file():
                    typer.echo(f"[{o}] points to a file, not a dir\n\n")
                else:
                    typer.echo(f"[{o}] not exist\n\n")
                o = typer.prompt("Output dir path", type=Path)
                typer.echo()
                typer.echo()

            o = o.resolve()

            typer.echo()
            typer.echo()
            name: str = typer.prompt("Transcription file name (without extension).\n"
                                     "If ignored, the following template will be used:\n"
                                     f"\ttranscription-of-{v.stem}-[date]__[finished-conversion-time]\n\n"
                                     f"Transcription file name",
                                     default=f"transcription-of-{v.stem}-",
                                     show_default=False)

            retries: int = typer.prompt("Attempts to be made in case bad chunk are found.\n"
                                        "If ignored, the following default will be used: 10\n\n"
                                        "Retries",
                                        type=int, default=10,
                                        show_default=False)
            while retries < 0 or retries > 100:
                retries = typer.prompt("Not in range [1-100].\n"
                                       "Try again\n\n"
                                       "Retries",
                                       type=int,
                                       show_default=False)

            typer.echo()
            typer.echo()

            lang: str = typer.prompt("Language spoken in video in 'xx-YY' format.  "
                                     "'YY' can be the same as 'xx'.  "
                                     "'YY' is the regional language.  "
                                     "For example, if you want to convert a video "
                                     "where they speak British English, use en-BR instead of en-US, "
                                     "for greater precision.  "
                                     "See [https://cloud.google.com/speech-to-text/docs/languages] for "
                                     "supported languages\n\n"
                                     "Language")

            skip_bf: bool = typer.confirm("Skip bad chunks.  "
                                          "This will skip the number of attempts to make and "
                                          "proceed to the next one.\n\n"
                                          "Confirm?")

            typer.echo()
            typer.echo()

            abort_on_bf: bool = typer.confirm("Aborts the conversion to the first bad fragment "
                                              "found.  This will save what has been converted.\n\n"
                                              "Confirm?")

            typer.echo()
            typer.echo()
            video_to_text_converter(path_of_video=v,
                                    transcription_file_dir_path=o,
                                    transcription_file_name=name,
                                    abort_on_bad_chunk=abort_on_bf,
                                    skip_bad_chunks=skip_bf,
                                    retries=retries,
                                    language=lang)
            raise typer.Exit()

        # Use Exception due a lot of exception
        # Is bad practice but do the required
        except Exception as e:
            typer.echo()
            typer.echo("Exiting...")
            typer.echo("Thanks!")
            typer.echo(e)
            raise typer.Exit()


def main(pov: Optional[Path] = typer.Argument(..., exists=True,
                                              file_okay=True,
                                              resolve_path=True,
                                              help="Path of the video to convert."),
         lang: Optional[str] = typer.Argument(...,
                                              help="Language spoken in video in 'xx-YY' format.  "
                                                   "'YY' can be the same as 'xx'.  "
                                                   "'YY' is the regional language.  "
                                                   "For example, if you want to convert a video "
                                                   "where they speak British English, use en-BR instead of en-US, "
                                                   "for greater precision.  "
                                                   "See [https://cloud.google.com/speech-to-text/docs/languages] for "
                                                   "supported languages."),

         tfdp: Optional[Path] = typer.Option(Path.cwd(),
                                             "-o",
                                             "--output",
                                             resolve_path=True,
                                             is_flag=True,
                                             show_default=False,
                                             help="Directory path where the file with the "
                                                  "transcript will be saved.  "
                                                  "If it is ignored, the directory where this "
                                                  "program was invoked will be used by default."
                                             ),

         tfn: Optional[str] = typer.Option(
             f"transcription-of-[video-name]-[date]__[finished-conversion-time]",
             "-tfn",
             "--transcription-file-name",
             show_default=False,
             is_flag=True,
             help="Transcription file name only (without file extension).  "
                  "By default: autogenerated"
         ),

         retries: Optional[int] = typer.Option(10, "-R",
                                               "--retries",
                                               min=1,
                                               max=100,
                                               show_default=False,
                                               help="Attempts to be made in case bad chunk are found.  By default:  10"
                                               ),

         interactive: Optional[bool] = typer.Option(False,
                                                    "-i",
                                                    "--interactive",
                                                    show_default=False,
                                                    is_eager=True,
                                                    is_flag=True,
                                                    callback=interactive_callback,
                                                    help="Enter and start interactive mode. Useful for newest."),
         sbc: Optional[bool] = typer.Option(False,
                                            "--skip-bad-fragments",
                                            is_flag=True,
                                            show_default=False,
                                            help="Skip bad fragments.  "
                                                 "This will skip the number of attempts to make and "
                                                 "proceed to the next one."),
         aobc: Optional[bool] = typer.Option(False,
                                             "--abort-on-bad-fragment",
                                             is_flag=True,
                                             show_default=False,
                                             help="Aborts the conversion to the first bad chunk "
                                                  "found.  This will save what has been converted."
                                             ),
         version: Optional[bool] = typer.Option(None,
                                                "-v",
                                                "--version",
                                                is_flag=True,
                                                is_eager=True,
                                                callback=version_callback,
                                                help="Show version and exit.")
         ) -> None:
    if version or interactive:
        typer.echo(None)

    return video_to_text_converter(path_of_video=pov,
                                   language=lang,
                                   transcription_file_dir_path=tfdp,
                                   transcription_file_name=tfn,
                                   abort_on_bad_chunk=aobc,
                                   skip_bad_chunks=sbc,
                                   retries=retries)

"""
Copyright 2021 <NAME>
"""
from pathlib import Path
from typing import Optional

import typer

from converter import video_to_text_converter


def version_callback(value: bool):
    if value:
        print("Joitek-Video2Text Converter    0.0.1")
        raise typer.Exit()


def interactive_callback(value: bool) -> None:
    if value:
        typer.clear()
        typer.echo("=======================================================================")
        typer.echo("Joitek-Video2Text Converter")
        typer.echo("Welcome to interactive mode!")
        typer.echo("=======================================================================")
        typer.echo()
        typer.echo(
            "The program will divide the video into chunks according to the exact number of minutes it has.\n"
            "1 chunk = 1 minute\n\n"
            "The remaining seconds will be ignored. It will be fixed in new versions ;)\n\n"
            "e.g. a video with a duration of 1h: 00m: 18s, those 18 seconds will be ignored.\n"
            "The rest will turn into minutes.\n\n"
            "Start with the basis\n\n"
            "For ignore something, press ENTER\n"
            "For exit, press Ctrl + C or any combination. We make sure you don't have any problems"
        )
        typer.echo("=======================================================================\n\n")
        try:

            v: Path = typer.prompt("Path of the video to convert.\n"
                                   "e.g. C:\\myfolder\\myvideo.mp4\n\n"
                                   "Path of the video to convert", type=Path)
            typer.echo()
            typer.echo()
            while not v.exists() or v.is_dir():
                if v.is_dir():
                    typer.echo(f"[{v}] points to a dir, not a file\n\n")
                else:
                    typer.echo(f"[{v}] not exist\n\n")
                v = typer.prompt("Path of the video to convert", type=Path)
                typer.echo()
                typer.echo()
            v = v.resolve()

            o: Path = typer.prompt("Directory path where the file with the transcript will be saved.\n"
                                   f"If ignored, the current directory will be used.\n\n"
                                   "Output dir path",
                                   type=Path,
                                   default=Path.cwd(),
                                   show_default=False)

            while not o.exists() or o.is_file():
                if o.is_file():
                    typer.echo(f"[{o}] points to a file, not a dir\n\n")
                else:
                    typer.echo(f"[{o}] not exist\n\n")
                o = typer.prompt("Output dir path", type=Path)
                typer.echo()
                typer.echo()

            o = o.resolve()

            typer.echo()
            typer.echo()
            name: str = typer.prompt("Transcription file name (without extension).\n"
                                     "If ignored, the following template will be used:\n"
                                     f"\ttranscription-of-{v.stem}-[date]__[finished-conversion-time]\n\n"
                                     f"Transcription file name",
                                     default=f"transcription-of-{v.stem}-",
                                     show_default=False)

            retries: int = typer.prompt("Attempts to be made in case bad chunk are found.\n"
                                        "If ignored, the following default will be used: 10\n\n"
                                        "Retries",
                                        type=int, default=10,
                                        show_default=False)
            while retries < 0 or retries > 100:
                retries = typer.prompt("Not in range [1-100].\n"
                                       "Try again\n\n"
                                       "Retries",
                                       type=int,
                                       show_default=False)

            typer.echo()
            typer.echo()

            lang: str = typer.prompt("Language spoken in video in 'xx-YY' format.  "
                                     "'YY' can be the same as 'xx'.  "
                                     "'YY' is the regional language.  "
                                     "For example, if you want to convert a video "
                                     "where they speak British English, use en-BR instead of en-US, "
                                     "for greater precision.  "
                                     "See [https://cloud.google.com/speech-to-text/docs/languages] for "
                                     "supported languages\n\n"
                                     "Language")

            skip_bf: bool = typer.confirm("Skip bad chunks.  "
                                          "This will skip the number of attempts to make and "
                                          "proceed to the next one.\n\n"
                                          "Confirm?")

            typer.echo()
            typer.echo()

            abort_on_bf: bool = typer.confirm("Aborts the conversion to the first bad fragment "
                                              "found.  This will save what has been converted.\n\n"
                                              "Confirm?")

            typer.echo()
            typer.echo()
            video_to_text_converter(path_of_video=v,
                                    transcription_file_dir_path=o,
                                    transcription_file_name=name,
                                    abort_on_bad_chunk=abort_on_bf,
                                    skip_bad_chunks=skip_bf,
                                    retries=retries,
                                    language=lang)
            raise typer.Exit()

        # Use Exception due a lot of exception
        # Is bad practice but do the required
        except Exception as e:
            typer.echo()
            typer.echo("Exiting...")
            typer.echo("Thanks!")
            typer.echo(e)
            raise typer.Exit()


def main(pov: Optional[Path] = typer.Argument(..., exists=True,
                                              file_okay=True,
                                              resolve_path=True,
                                              help="Path of the video to convert."),
         lang: Optional[str] = typer.Argument(...,
                                              help="Language spoken in video in 'xx-YY' format.  "
                                                   "'YY' can be the same as 'xx'.  "
                                                   "'YY' is the regional language.  "
                                                   "For example, if you want to convert a video "
                                                   "where they speak British English, use en-BR instead of en-US, "
                                                   "for greater precision.  "
                                                   "See [https://cloud.google.com/speech-to-text/docs/languages] for "
                                                   "supported languages."),

         tfdp: Optional[Path] = typer.Option(Path.cwd(),
                                             "-o",
                                             "--output",
                                             resolve_path=True,
                                             is_flag=True,
                                             show_default=False,
                                             help="Directory path where the file with the "
                                                  "transcript will be saved.  "
                                                  "If it is ignored, the directory where this "
                                                  "program was invoked will be used by default."
                                             ),

         tfn: Optional[str] = typer.Option(
             f"transcription-of-[video-name]-[date]__[finished-conversion-time]",
             "-tfn",
             "--transcription-file-name",
             show_default=False,
             is_flag=True,
             help="Transcription file name only (without file extension).  "
                  "By default: autogenerated"
         ),

         retries: Optional[int] = typer.Option(10, "-R",
                                               "--retries",
                                               min=1,
                                               max=100,
                                               show_default=False,
                                               help="Attempts to be made in case bad chunk are found.  By default:  10"
                                               ),

         interactive: Optional[bool] = typer.Option(False,
                                                    "-i",
                                                    "--interactive",
                                                    show_default=False,
                                                    is_eager=True,
                                                    is_flag=True,
                                                    callback=interactive_callback,
                                                    help="Enter and start interactive mode. Useful for newest."),
         sbc: Optional[bool] = typer.Option(False,
                                            "--skip-bad-fragments",
                                            is_flag=True,
                                            show_default=False,
                                            help="Skip bad fragments.  "
                                                 "This will skip the number of attempts to make and "
                                                 "proceed to the next one."),
         aobc: Optional[bool] = typer.Option(False,
                                             "--abort-on-bad-fragment",
                                             is_flag=True,
                                             show_default=False,
                                             help="Aborts the conversion to the first bad chunk "
                                                  "found.  This will save what has been converted."
                                             ),
         version: Optional[bool] = typer.Option(None,
                                                "-v",
                                                "--version",
                                                is_flag=True,
                                                is_eager=True,
                                                callback=version_callback,
                                                help="Show version and exit.")
         ) -> None:
    if version or interactive:
        typer.echo(None)

    return video_to_text_converter(path_of_video=pov,
                                   language=lang,
                                   transcription_file_dir_path=tfdp,
                                   transcription_file_name=tfn,
                                   abort_on_bad_chunk=aobc,
                                   skip_bad_chunks=sbc,
                                   retries=retries)

Copyright 2021 <NAME> # Use Exception due a lot of exception # Is bad practice but do the required