#! /usr/bin/env python3

#    Copyright 2016, 2020 Denis Salem
#
#    This file is part of VenC.
#
#    VenC 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.
#
#    VenC 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 VenC.  If not, see <http://www.gnu.org/licenses/>.

import sys
import codecs

# Force stdout encoding to utf-8
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

import importlib

from venc2.helpers import notify
from venc2.l10n import messages; 
from venc2.l10n import locale_err;


#import cProfile, pstats, io
#pr = cProfile.Profile()
#pr.enable()
## ... do something ...

if locale_err:
    notify(messages.cannot_get_current_locale, color="YELLOW")

command_index = {"-v":                  [".print", "print_version", 0],
                "-nb":                  [".new", "new_blog", -1],
                "-ne":                  [".new", "new_entry", 2],
                "-xb":                  [".export", "export_blog", 1],
                "-ex":                  [".export", "edit_and_export", 1],
                "-xftp":                [".export", "export_and_remote_copy", 1],
                "-rc":                  [".remote", "remote_copy", 0],
                "-it":                  [".install", "install_theme", 1],
                "-h":                   [".print", "print_help", 0],
                "-s":                   [".serv", "serv_blog", 0],
                "--serv":               [".serv", "serv_blog", 0],
                "--help":               [".print", "print_help", 0],
                "--install-theme":      [".install", "install_theme", 1],
                "--remote-copy":        [".remote", "remote_copy", 0],
                "--export-via-ftp":     [".export", "export_and_remote_copy", 1],
                "--edit-and-export":    [".export", "edit_and_export", 1],
                "--export-blog":        [".export", "export_blog", 1],
                "--new-entry":          [".new", "new_entry", 2],
                "--new-blog":           [".new", "new_blog", -1],
                "--version":            [".print", "print_version", 0]}

def argv_handler(argv=None):
    argv = argv if argv is not None else sys.argv[1:]
    if argv != []: 
       if argv[0] in command_index:
           if command_index[argv[0]][2] != -1: 
               arguments = argv[1:command_index[argv[0]][2]+1]
           
           else:
               arguments = list()
               for argument in argv[1:]:
                   if argument not in command_index.keys():
                       arguments.append(argument)

                   else:
                       break

           module = importlib.import_module(command_index[argv[0]][0], "venc2.commands")
           command = getattr(module, command_index[argv[0]][1])
           command(arguments)
           argv_handler(argv[len(arguments)+1:])

       else:
           notify(messages.unknown_command.format(argv[0]),"RED")
           argv_handler(argv[1:])

if sys.argv[1:] != []: 
    argv_handler()

else:
    notify(messages.nothing_to_do,"YELLOW")

#pr.disable()
#s = io.StringIO()
#sortby = 'cumulative'
#ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
#ps.print_stats()
#print(s.getvalue())
