from __future__ import absolute_import, unicode_literals

import os

import tornado.web

from mopidy import config, ext

__version__ = '0.2.0'


class PartyRequestHandler(tornado.web.RequestHandler):

    def initialize(self, core, data, config):
        self.core = core
        self.data = data
        self.requiredVotes = config["party"]["votes_to_skip"]

    def get(self):
        currentTrack = self.core.playback.get_current_track().get()
        if (currentTrack == None): return
        currentTrackURI = currentTrack.uri

        # If the current track is different to the one stored, clear votes
        if (currentTrackURI != self.data["track"]):
            self.data["track"] = currentTrackURI
            self.data["votes"] = []

        if (self.request.remote_ip in self.data["votes"]): # User has already voted
            self.write("Vous avez déjà voté pour passer cette chanson !")
        else: # Valid vote
            self.data["votes"].append(self.request.remote_ip)
            if (len(self.data["votes"]) == self.requiredVotes):
                self.core.playback.next()
                self.write("Skipping...")
            else:
                self.write("Vous avez voté pour passer cette musique. ("+str(self.requiredVotes-len(self.data["votes"]))+" Votes nécessaires)")



def party_factory(config, core):
    data = {'track':"", 'votes':[]}
    return [
    ('/vote', PartyRequestHandler, {'core': core, 'data':data, 'config':config})
    ]


class Extension(ext.Extension):

    dist_name = 'Mopidy-Party'
    ext_name = 'party'
    version = __version__

    def get_default_config(self):
        conf_file = os.path.join(os.path.dirname(__file__), 'ext.conf')
        return config.read(conf_file)

    def get_config_schema(self):
        schema = super(Extension, self).get_config_schema()
        schema['votes_to_skip'] = config.Integer(minimum=0)
        return schema

    def setup(self, registry):
        registry.add('http:static', {
            'name': self.ext_name,
            'path': os.path.join(os.path.dirname(__file__), 'static'),
        })
        registry.add('http:app', {
            'name': self.ext_name,
            'factory': party_factory,
        })

@@ -1124,16 +1124,29 @@
 te(%22
-Skipping
+Changement de musique
 ...%22
@@ -1202,25 +1202,25 @@
 ous avez vot
-%C3%A9
+e
  pour passer
@@ -1295,17 +1295,17 @@
  Votes n
-%C3%A9
+e
 cessaire

Update __init__.py