# -*- coding: utf-8 -*-
import os
import re

import irc3
import requests
from docopt import Dict
from irc3.plugins.command import command
from irc3.utils import IrcString

from . import Plugin
from ..utils import date_from_iso


@irc3.plugin
class YouTube(Plugin):
    requires = ['irc3.plugins.command']

    URL = 'https://www.googleapis.com/youtube/v3'
    API = '{}/videos?part=snippet,statistics,contentDetails'.format(URL)
    SEARCH = '{}/search?part=id'.format(URL)

    def get_video_data(self, video_id: str):
        """Requests the infos for a video id and formats them."""
        data = self._api(self.API, id=video_id)

        if not data.get('items'):
            return

        item = data['items'][0]
        date = date_from_iso(item['snippet']['publishedAt'])
        length = re.findall('(\d+[DHMS])', item['contentDetails']['duration'])

        views = int(item['statistics'].get('viewCount', 0))
        likes = int(item['statistics'].get('likeCount', 0))
        dislikes = int(item['statistics'].get('dislikeCount', 0))

        try:
            score = 100 * float(likes) / (likes + dislikes)
        except ZeroDivisionError:
            score = 0

        return '\x02[YouTube]\x02 ' \
               '{title} - length {length} -\x033 {likes:,}\x03 /' \
               '\x034 {dislikes:,}\x03 ({score:,.1f}%) - {views:,} ' \
               'views - {channel} on {date}' \
            .format(title=item['snippet']['title'],
                    channel=item['snippet']['channelTitle'],
                    length=' '.join([l.lower() for l in length]),
                    likes=likes,
                    dislikes=dislikes,
                    score=score,
                    views=views,
                    date=date.strftime('%Y.%m.%d'))

    @irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :.*(?:youtube.*?(?:v=|/v/)'
                r'|youtu\.be/)(?P<video_id>[-_a-zA-Z0-9]+).*')
    def youtube_parser(self, target: str, video_id: str):
        data = self.get_video_data(video_id)
        if data:
            self.bot.privmsg(target, data)

    @command
    def yt(self, mask: IrcString, target: IrcString, args: Dict):
        """Searches for query on YouTube and returns first result.

        %%yt <query>...
        """
        data = self._api(self.SEARCH, q=' '.join(args['<query>']))

        if 'error' in data:
            return '\x02[YouTube]\x02 Error performing search'
        elif data['pageInfo']['totalResults'] is 0:
            return '\x02[YouTube]\x02 No results found'
        else:
            video_id = data['items'][0]['id']['videoId']
            data = self.get_video_data(video_id)
            return '{} - https://youtu.be/{}'.format(data, video_id)

    # noinspection PyMethodMayBeStatic
    def _api(self, url: str, **kwargs):
        """Wrapper around requests.get which adds the Google API key."""
        kwargs['key'] = os.environ['GOOGLE_API_KEY']
        return requests.get(url, params=kwargs).json()