#!python
# coding=utf-8

"""
LICENSE:
Copyright 2015,2016 Hermann Krumrey

This file is part of toktokkie.

    toktokkie is a program that allows convenient managing of various
    local media collections, mostly focused on video.

    toktokkie is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    toktokkie is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with toktokkie.  If not, see <http://www.gnu.org/licenses/>.
LICENSE
"""

import os
import sys
import argparse
from toktokkie.utils.renaming.TVSeriesRenamer import TVSeriesRenamer
from toktokkie.utils.renaming.schemes.SchemeManager import SchemeManager


def parse_args():

    parser = argparse.ArgumentParser()
    parser.add_argument("directory", help="The directory whose contents to rename")
    parser.add_argument("-n", "--noconfirm", action="store_true", help="Renames without confirming with the user first")
    parser.add_argument("-r", "--recursive", action="store_true", help="Renames recursively")
    return parser.parse_args()

if __name__ == '__main__':

    args = parse_args()

    directory = args.directory
    if not os.path.isdir(directory):
        print("Not a valid directory")
        sys.exit(1)

    renamer = TVSeriesRenamer(args.directory, SchemeManager.get_scheme_from_scheme_name("Plex (TVDB)"), args.recursive)

    if not args.noconfirm:
        confirmation = renamer.request_confirmation()

        print("Renaming:")
        for episode in confirmation:
            print(episode.episode.get_old_name().ljust(50) + " -> ".ljust(5).rjust(5) + episode.episode.get_new_name())

        confirmed = input("Is this OK? (y/n)")

        if confirmed.lower() == "y":
            print("Starting Renaming...")
            renamer.start_rename(True)
            print("Done")

    else:
        print("Starting Renaming...")
        renamer.start_rename(True)
        print("Done")

    print("Bye")
