# coding: utf-8
# Module: commands
# Created on: 28.07.2015
# Author: Roman Miroshnychenko aka Roman V.M. (romanvm@yandex.ua)
# Licence: GPL v.3: http://www.gnu.org/copyleft/gpl.html
"""
Context menu commands
"""

import sys
import xbmc
import xbmcgui
import json_requests as jsonrq
from simpleplugin import Addon

addon = Addon('plugin.video.yatp')
_ = addon.initialize_gettext()


def show_torrent_info(info_hash):
    """
    Display current torrent info

    :param info_hash:
    :return:
    """
    torr_info = jsonrq.get_torrent_info(info_hash)
    info_dialog = xbmcgui.DialogProgress()
    info_dialog.create(torr_info['name'])
    while not info_dialog.iscanceled():
        info_dialog.update(torr_info['progress'],
                           _('state: {0}; seeds: {1}; peers: {2}').format(
                               torr_info['state'],
                               torr_info['num_seeds'],
                               torr_info['num_peers']
                           ),
                           _('size: {0}MB; DL speed: {1}KB/s; UL speed: {2}KB/s').format(
                               torr_info['size'],
                               torr_info['dl_speed'],
                               torr_info['ul_speed']
                           ),
                           _('total DL: {0}MB; total UL: {1}MB').format(
                               torr_info['total_download'],
                               torr_info['total_upload'])
                           )
        xbmc.sleep(1000)
        torr_info = jsonrq.get_torrent_info(info_hash)


if __name__ == '__main__':
    if sys.argv[1] == 'pause':
        jsonrq.pause_torrent(sys.argv[2])
    elif sys.argv[1] == 'resume':
        jsonrq.resume_torrent(sys.argv[2])
    elif sys.argv[1] == 'delete' and xbmcgui.Dialog().yesno(
            _('Confirm delete'),
            _('Do you really want to delete the torrent?')):
        jsonrq.remove_torrent(sys.argv[2], False)
    elif sys.argv[1] == 'delete_with_files'and xbmcgui.Dialog().yesno(
            _('Confirm delete'),
            _('Do you really want to delete the torrent with files?'),
            _('Warning: The files will be deleted permanently!')):
        jsonrq.remove_torrent(sys.argv[2], True)
    elif sys.argv[1] == 'pause_all':
        jsonrq.pause_all()
    elif sys.argv[1] == 'resume_all':
        jsonrq.resume_all()
    elif sys.argv[1] == 'show_info':
        show_torrent_info(sys.argv[2])
    elif sys.argv[1] == 'restore_finished':
        jsonrq.restore_finished(sys.argv[2])
    else:
        addon.log_debug('Command cancelled or invalid command: {0}'.format(sys.argv[1]))
    xbmc.executebuiltin('Container.Refresh')

[((323, 349), 'simpleplugin.Addon', 'Addon', (['"""plugin.video.yatp"""'], {}), "('plugin.video.yatp')\n", (328, 349), False, 'from simpleplugin import Addon\n'), ((518, 552), 'json_requests.get_torrent_info', 'jsonrq.get_torrent_info', (['info_hash'], {}), '(info_hash)\n', (541, 552), True, 'import json_requests as jsonrq\n'), ((571, 595), 'xbmcgui.DialogProgress', 'xbmcgui.DialogProgress', ([], {}), '()\n', (593, 595), False, 'import xbmcgui\n'), ((2643, 2683), 'xbmc.executebuiltin', 'xbmc.executebuiltin', (['"""Container.Refresh"""'], {}), "('Container.Refresh')\n", (2662, 2683), False, 'import xbmc\n'), ((1498, 1514), 'xbmc.sleep', 'xbmc.sleep', (['(1000)'], {}), '(1000)\n', (1508, 1514), False, 'import xbmc\n'), ((1535, 1569), 'json_requests.get_torrent_info', 'jsonrq.get_torrent_info', (['info_hash'], {}), '(info_hash)\n', (1558, 1569), True, 'import json_requests as jsonrq\n'), ((1638, 1671), 'json_requests.pause_torrent', 'jsonrq.pause_torrent', (['sys.argv[2]'], {}), '(sys.argv[2])\n', (1658, 1671), True, 'import json_requests as jsonrq\n'), ((1714, 1748), 'json_requests.resume_torrent', 'jsonrq.resume_torrent', (['sys.argv[2]'], {}), '(sys.argv[2])\n', (1735, 1748), True, 'import json_requests as jsonrq\n'), ((1912, 1953), 'json_requests.remove_torrent', 'jsonrq.remove_torrent', (['sys.argv[2]', '(False)'], {}), '(sys.argv[2], False)\n', (1933, 1953), True, 'import json_requests as jsonrq\n'), ((2204, 2244), 'json_requests.remove_torrent', 'jsonrq.remove_torrent', (['sys.argv[2]', '(True)'], {}), '(sys.argv[2], True)\n', (2225, 2244), True, 'import json_requests as jsonrq\n'), ((1786, 1802), 'xbmcgui.Dialog', 'xbmcgui.Dialog', ([], {}), '()\n', (1800, 1802), False, 'import xbmcgui\n'), ((2290, 2308), 'json_requests.pause_all', 'jsonrq.pause_all', ([], {}), '()\n', (2306, 2308), True, 'import json_requests as jsonrq\n'), ((2001, 2017), 'xbmcgui.Dialog', 'xbmcgui.Dialog', ([], {}), '()\n', (2015, 2017), False, 'import xbmcgui\n'), ((2355, 2374), 'json_requests.resume_all', 'jsonrq.resume_all', ([], {}), '()\n', (2372, 2374), True, 'import json_requests as jsonrq\n'), ((2503, 2539), 'json_requests.restore_finished', 'jsonrq.restore_finished', (['sys.argv[2]'], {}), '(sys.argv[2])\n', (2526, 2539), True, 'import json_requests as jsonrq\n')]