# coding: utf-8
import os
from itertools import cycle

def es_local(ip='127.0.0.1'):
    from netaddr import IPAddress
    return IPAddress(ip).is_private()

def digito_verificador(rut):
    reversed_digits = map(int, reversed(str(rut)))
    factors = cycle(range(2, 8))
    s = sum(d * f for d, f in zip(reversed_digits, factors))
    return (-s) % 11 if (-s) % 11 != 10 else 'k'

def send_reset_email(correo):
    from flask import url_for, current_app
    from flask_mail import Message
    from ilab_app import mail

    token = correo.cuenta.get_reset_token()
    msg = Message('Solicitud de recuperación de cuenta', sender='noreply@ilab.cl', recipients=[correo.correo])

    msg.body = '''Para recuperar su cuenta, ingrese a la siguiente URL: {}

En caso que usted *no* haya realizado esta solicitud, simplemente ignore este correo y no se realizará ningún cambio.'''.format(url_for('main.reset_token', token=token, _external=True))
    current_app.logger.debug('Sending Dev({})'.format(msg.body))
    mail.send(msg)

def clean_telefono(mystr):
    import unidecode
    return unidecode.unidecode(mystr).replace(" ", "").replace("\t", "").replace("-", "").replace("'", "").replace(".", "").replace("\"", "").replace("%", "")

def clean_str(mystr):
    import unidecode
    return unidecode.unidecode(mystr).replace(" ", "").replace("\t", "").replace("'", "").replace(".", "").replace("\"", "").replace("%", "")
