import logging

from hashlib import sha1

from app import app
from util.config.validators import BaseValidator, ConfigValidationException
from util.registry.torrent import jwt_from_infohash

logger = logging.getLogger(__name__)

class BittorrentValidator(BaseValidator):
  name = "bittorrent"

  @classmethod
  def validate(cls, config, user, user_password):
    """ Validates the configuration for using BitTorrent for downloads. """
    announce_url = config.get('BITTORRENT_ANNOUNCE_URL')
    if not announce_url:
      raise ConfigValidationException('Missing announce URL')

    # Ensure that the tracker is reachable and accepts requests signed with a registry key.
    client = app.config['HTTPCLIENT']

    params = {
      'info_hash': sha1('test').digest(),
      'peer_id': '-QUAY00-6wfG2wk6wWLc',
      'uploaded': 0,
      'downloaded': 0,
      'left': 0,
      'numwant': 0,
      'port': 80,
    }

    encoded_jwt = jwt_from_infohash(params['info_hash'])
    params['jwt'] = encoded_jwt

    resp = client.get(announce_url, timeout=5, params=params)
    logger.debug('Got tracker response: %s: %s', resp.status_code, resp.text)

    if resp.status_code == 404:
      raise ConfigValidationException('Announce path not found; did you forget `/announce`?')

    if resp.status_code == 500:
      raise ConfigValidationException('Did not get expected response from Tracker; ' +
                                      'please check your settings')

    if resp.status_code == 200:
      if 'invalid jwt' in resp.text:
        raise ConfigValidationException('Could not authorize to Tracker; is your Tracker ' +
                                        'properly configured?')

      if 'failure reason' in resp.text:
        raise ConfigValidationException('Could not validate signed announce request: ' + resp.text)