from __future__ import unicode_literals, division, absolute_import
from builtins import *  # noqa pylint: disable=unused-import, redefined-builtin

import logging
import hashlib

from flexget import plugin
from flexget.event import event
from flexget.plugin import PluginWarning
from flexget.utils.requests import Session as RequestSession, TimedLimiter
from requests.exceptions import RequestException

__name__ = 'sms_ru'
log = logging.getLogger(__name__)

SMS_SEND_URL = 'http://sms.ru/sms/send'
SMS_TOKEN_URL = 'http://sms.ru/auth/get_token'

requests = RequestSession(max_retries=3)
requests.add_domain_limiter(TimedLimiter('sms.ru', '5 seconds'))


class SMSRuNotifier(object):
    """
    Sends SMS notification through sms.ru http api sms/send.
    Phone number is a login assigned to sms.ru account.

    Example:

      sms_ru:
        phone_number: <PHONE_NUMBER> (accepted format example: '79997776655')
        password: <PASSWORD>
        [message: <MESSAGE_TEXT>]

    """
    schema = {
        'type': 'object',
        'properties': {
            'phone_number': {'type': 'string'},
            'password': {'type': 'string'},
            'message': {'type': 'string'},
            'file_template': {'type': 'string'},
        },
        'additionalProperties': False,
        'required': ['phone_number', 'password']
    }

    def notify(self, phone_number, password, message, **kwargs):
        """
        Send an SMS RU notification

        :param str phone_number: Phone number to send to
        :param str password: Password
        :param str message: Message to send
        """
        try:
            token_response = requests.get(SMS_TOKEN_URL)
        except RequestException as e:
            raise PluginWarning('Could not get auth token: %s' % repr(e))

        sha512 = hashlib.sha512(password + token_response.text).hexdigest()

        # Build request params
        notification = {'login': phone_number,
                       'sha512': sha512,
                       'token': token_response.text,
                       'to': phone_number,
                       'text': message}

        try:
            response = requests.get(SMS_SEND_URL, params=notification)
        except RequestException as e:
            raise PluginWarning(e.args[0])
        else:
            if not response.text.find('100') == 0:
                raise PluginWarning(response.text)

    # Run last to make sure other outputs are successful before sending notification
    @plugin.priority(0)
    def on_task_output(self, task, config):
        # Send default values for backwards compatibility
        notify_config = {
            'to': [{__name__: config}],
            'scope': 'entries',
            'what': 'accepted'
        }
        plugin.get_plugin_by_name('notify').instance.send_notification(task, notify_config)


@event('plugin.register')
def register_plugin():
    plugin.register(SMSRuNotifier, __name__, api_ver=2, groups=['notifiers'])

@@ -941,42 +941,8 @@
 ORD%3E
-%0A        %5Bmessage: %3CMESSAGE_TEXT%3E%5D
 %0A%0A  
@@ -1106,101 +1106,8 @@
 ng'%7D
-,%0A            'message': %7B'type': 'string'%7D,%0A            'file_template': %7B'type': 'string'%7D,
 %0A   
@@ -1234,30 +1234,13 @@
 lf, 
-phone_number, password
+title
 , me
@@ -1250,16 +1250,14 @@
 ge, 
-**kwargs
+config
 ):%0A 
@@ -1306,148 +1306,8 @@
 tion
-%0A%0A        :param str phone_number: Phone number to send to%0A        :param str password: Password%0A        :param str message: Message to send
 %0A   
@@ -1530,24 +1530,32 @@
 .sha512(
+config%5B'
 password
  + token
@@ -1546,16 +1546,18 @@
 password
+'%5D
  + token
@@ -1649,16 +1649,24 @@
 login': 
+config%5B'
 phone_nu
@@ -1661,32 +1661,34 @@
 ig%5B'phone_number
+'%5D
 ,%0A              
@@ -1796,16 +1796,24 @@
   'to': 
+config%5B'
 phone_nu
@@ -1816,16 +1816,18 @@
 e_number
+'%5D
 ,%0A      

Update sms_ru plugin for new notification api