#!/usr/bin/env python
# -*- coding: utf-8 -*-


__author__ = 'quentingerome'


import requests
from cherrypy.lib.auth2 import require, member_of
import logging
import htpc
import cherrypy
from HTMLParser import HTMLParser
from htpc.helpers import striphttp

logger = logging.getLogger('modules.utorrent')


class AuthTokenParser(HTMLParser):
    token = None

    def handle_data(self, data):
        self._token = data

    def token(self, html):
        self._token = None
        self.feed(html)
        return self._token


fields = {
    'name': 2,
    'id': 0,
    'status': 1,
    'size': 3,
    'percentage_done': 4,
    'dl': 5,
    'up': 6,
    'dl_speed': 9,
    'up_speed': 8,
    'eta': 10,
    'ratio': 7,
}

status = {
    1: 'Started',
    2: 'Checking',
    4: 'Started&Checked',
    8: 'Checked',
    16: 'Error',
    32: 'Paused',
    64: 'Queued',
    128: 'Loaded'
}


def _get_torrent_state(state_sum):
    """
    Returns a list of all states of the torrent
    :param value: int
    :return: str
    """

    states = []
    for ps in sorted(status.keys(), reverse=True):
        if not state_sum:
            break
        if ps <= state_sum:
            states.append(ps)
            state_sum -= ps
    return states


def TorrentResult(values):
    """

    :param values:
    :type values: list
    :return:
    :rtype: dict
    """