## -*- coding: utf-8 -*-
__author__ = 'SDE'

from django.utils.html import strip_tags
# from uuslug import slugify
import json
import os.path
from PIL import Image
from django.core.files.uploadedfile import InMemoryUploadedFile
from BaseModels.mailSender import techSendMail
from datetime import datetime, timedelta


def get_near_work_day(DT):
    if DT.isoweekday() < 6:
        return DT

    return DT + timedelta(days=8 - DT.isoweekday())


def get_next_DT_for_monthes_delta_great(monthes_delta, fromDT=datetime.now()):
    DT = fromDT
    i = 0

    cur_month = DT.month

    while cur_month == DT.month:
        DT = DT + timedelta(days=1)

    # подбираем ближайший день, существующий в месяце
    fail = True
    i = 0
    while fail:
        try:
            DT = DT.replace(day=fromDT.day - i)
            fail = False
        except:
            i += 1
    # DT = DT - timedelta(days=1)
    # DT = DT.replace(hour=23, minute=59, second=59)

    return DT


def get_prev_DT_for_monthes_delta_less(monthes_delta, fromDT=datetime.now()):
    DT = fromDT
    i = 0

    while i < monthes_delta:
        DT = DT.replace(day=1)
        DT = DT - timedelta(days=1)
        i += 1

    # подбираем ближайший день, существующий в месяце
    fail = True
    i = 0
    while fail:
        try:
            DT = DT.replace(day=fromDT.day - i)
            fail = False
        except:
            i += 1
    # DT = DT - timedelta(days=1)
    # DT = DT.replace(hour=23, minute=59, second=59)

    return DT


def correct_filter_name_for_filter_and_create(filter_kwargs):
    filter_Dict = {}
    create_Dict = {}

    filter_Dict.update(filter_kwargs)
    create_Dict.update(filter_kwargs)

    if 'name' in filter_kwargs:
        filter_Dict['name__iexact'] = filter_kwargs['name']
        del filter_Dict['name']

        if 'id' in filter_kwargs:
            del filter_Dict['id']
            del create_Dict['id']

    return filter_Dict, create_Dict


def date_range_as_Dict(start_date, end_date):
    import datetime
    # for ordinal in range(start_date.toordinal(), end_date.toordinal()):
    #     yield datetime.date.fromordinal(ordinal)

    return [{start_date + datetime.timedelta(n): {}} for n in range(int((end_date - start_date).days) + 1)]


def sortByLength(inputStr):
    return len(inputStr)


def add_domain(request, url, add_lang=False):
    domain = get_domain_by_request(request)
    if add_lang:
        cur_lang = get_cur_lang_by_request(request)
        return '{0}/{1}/{2}'.format(domain, cur_lang, url)
    else:
        return '{0}{1}'.format(domain, url)


def get_domain_by_request(request):
    from project_sets import domain
    if request.query_params and 'domain' in request.query_params:
        return request.query_params['domain']
    return domain


def get_cur_lang_by_request(request):
    from project_sets import lang
    if request.query_params and 'cur_lang' in request.query_params:
        return request.query_params['cur_lang']
    return lang


def get_img_type_by_request(request):
    if request.query_params and 'img_type' in request.query_params:
        return request.query_params['img_type']

    return 'webp'


def image_convert_to_png(photo_file, save_file_path=None):
    from io import BytesIO
    from PIL import Image as Img
    print('image_convert_to_png')

    try:

        fn_list = photo_file.name.split('.')
        if len(fn_list) > 1:
            fp = fn_list[0] + '.png'
        else:
            fp = photo_file.name + '.png'

        image = Img.open(photo_file)

        print('photo was uploaded')

        try:
            image.convert("RGB")
            print('photo was converted to RGB')
        except:
            print('!!! fail convert photo to RGB')

        if save_file_path:
            image.save(save_file_path, format="PNG")

            print('photo was saved')

        fileBytes = BytesIO()
        image.save(fileBytes, format="PNG")
        print('photo was preparing for streaming')

        memoryFile = InMemoryUploadedFile(fileBytes, None, fp, 'image/png', 1, None)

        return memoryFile

    except Exception as e:
        msg = 'image_convert_to_png error={0}'.format(str(e))
        print(msg)
        techSendMail(msg, 'image_convert_to_png error')
        return {'error': msg}


def image_convert_to_webP(photo_file, save_file_path=None):
    from io import BytesIO
    from PIL import Image as Img

    fn_list = photo_file.name.split('.')
    if len(fn_list) > 1:
        webP_fp = fn_list[0] + '.webp'
    else:
        webP_fp = photo_file.name + '.webp'

    image = Img.open(photo_file)

    image.convert("RGB")

    if save_file_path:
        image.save(save_file_path, format="WEBP")

    fileBytes = BytesIO()
    image.save(fileBytes, format="WEBP")
    memoryFile = InMemoryUploadedFile(fileBytes, None, webP_fp, 'image/webp', 1, None)

    return memoryFile


def get_thumb_path(full_filepath, img_type):
    if img_type == 'webp':
        convert_to_webP = True
    else:
        convert_to_webP = False

    icon_path = None

    full_filepath = full_filepath.replace('\\', '/')

    if not os.path.exists(full_filepath):
        return None

    path_list = full_filepath.split('/')
    filename = path_list[-1]
    filepath = '/'.join(path_list[:-1])

    if convert_to_webP:
        fn_list = filename.split('.')
        if len(fn_list) > 1:
            filename = fn_list[0] + '.webp'
        else:
            filename = filename + '.webp'

    icon_path = '{0}/icon-{1}'.format(filepath, filename)

    if not os.path.exists(icon_path):
        size = (300, 300)
        img = Image.open(full_filepath)
        if convert_to_webP:
            img.convert("RGB")
        img.thumbnail(size)
        if convert_to_webP:
            img.save(icon_path, 'WEBP')
        else:
            img.save(icon_path)

    return icon_path


def get_filename_from_path(filepath, wo_ext=False):
    f_list = filepath.split('/')
    if len(f_list) > 1:
        filename = f_list[-1]
    else:
        filename = f_list[0]

    f_list = filename.split('\\')
    if len(f_list) > 1:
        filename = f_list[-1]
    else:
        filename = f_list[0]

    if filename and wo_ext:
        f_list = filename.split('.')
        filename = f_list[0]

    return filename


def get_free_filename(filename, filepath):
    from os import path, access, R_OK  # W_OK for write permission.

    full_path = filepath + filename

    i = 0
    while path.exists(full_path) and path.isfile(full_path) and access(full_path, R_OK):
        i += 1
        full_path = filepath + filename + '-{0}'.format(str(i))

    return full_path


def url_translit(value):
    value = translit(value).lower()
    # value = slugify_text(value).lower()
    # value = value.replace(u',', u'-')
    # value = value.replace(u'.', u'-')
    # value = value.replace(u'_', u'-')
    # value = value.replace(u'"', u'')
    # value = value.replace(u'“', u'')
    # value = value.replace(u'”', u'')
    # value = value.replace(u"'", u'')
    # value = value.replace(u'/', u'-')
    # value = value.replace(u'\\', u'-')
    # value = value.replace(u'(', u'')
    # value = value.replace(u')', u'')
    # value = value.replace(u'&', u'-and-')
    # value = value.replace(u' ', u'-')
    # value = value.replace(u'%', u'')
    # value = value.replace(u'*', u'-')
    # value = value.replace(u'±', u'-')

    allow_symbols = '0123456789abcdefghijklmnopqrstuvwxyz-'
    i = 0
    while i < len(value):
        if not value[i] in allow_symbols:
            value = value.replace(value[i], '-')

        i += 1

    while '--' in value:
        value = value.replace(u'--', u'-')

    if value[len(value) - 1] == '-':
        value = value[:-1]

    return value


def translit(locallangstring):
    conversion = {
        u'\u0410': 'A', u'\u0430': 'a',
        u'\u0411': 'B', u'\u0431': 'b',
        u'\u0412': 'V', u'\u0432': 'v',
        u'\u0413': 'G', u'\u0433': 'g',
        u'\u0414': 'D', u'\u0434': 'd',
        u'\u0415': 'E', u'\u0435': 'e',
        u'\u0401': 'Yo', u'\u0451': 'yo',
        u'\u0416': 'Zh', u'\u0436': 'zh',
        u'\u0417': 'Z', u'\u0437': 'z',
        u'\u0418': 'I', u'\u0438': 'i',
        u'\u0419': 'Y', u'\u0439': 'y',
        u'\u041a': 'K', u'\u043a': 'k',
        u'\u041b': 'L', u'\u043b': 'l',
        u'\u041c': 'M', u'\u043c': 'm',
        u'\u041d': 'N', u'\u043d': 'n',
        u'\u041e': 'O', u'\u043e': 'o',
        u'\u041f': 'P', u'\u043f': 'p',
        u'\u0420': 'R', u'\u0440': 'r',
        u'\u0421': 'S', u'\u0441': 's',
        u'\u0422': 'T', u'\u0442': 't',
        u'\u0423': 'U', u'\u0443': 'u',
        u'\u0424': 'F', u'\u0444': 'f',
        u'\u0425': 'H', u'\u0445': 'h',
        u'\u0426': 'Ts', u'\u0446': 'ts',
        u'\u0427': 'Ch', u'\u0447': 'ch',
        u'\u0428': 'Sh', u'\u0448': 'sh',
        u'\u0429': 'Sch', u'\u0449': 'sch',
        u'\u042a': '', u'\u044a': '',
        u'\u042b': 'Y', u'\u044b': 'y',
        u'\u042c': '', u'\u044c': '',
        u'\u042d': 'E', u'\u044d': 'e',
        u'\u042e': 'Yu', u'\u044e': 'yu',
        u'\u042f': 'Ya', u'\u044f': 'ya',
        u'№': 'no',
    }
    translitstring = []
    for c in locallangstring:
        translitstring.append(conversion.setdefault(c, c))
    return ''.join(translitstring)


def slugify_text(str_text):
    utf8_code = False
    try:
        str_text = str_text.encode('utf-8').decode('utf-8')
        utf8_code = True
    except:
        pass

    if utf8_code == False:
        try:
            str_text = str_text.decode('utf-8')
        except:
            pass

    str_text = del_bad_symbols(str_text)

    str_text = str_text.replace(u'"', u'')
    str_text = str_text.replace(u"'", u'')
    str_text = str_text.replace(u".", u'')
    str_text = str_text.replace(u",", u'')
    str_text = str_text.replace(u" -", u'-')
    str_text = str_text.replace(u"- ", u'-')
    str_text = str_text.replace(u"„", u'')
    str_text = str_text.replace(u"(", u'')
    str_text = str_text.replace(u")", u'')
    str_text = str_text.replace(u"{", u'')
    str_text = str_text.replace(u"}", u'')
    str_text = str_text.replace(u"<", u'')
    str_text = str_text.replace(u">", u'')

    str = translit(str_text)
    str = translit(str)
    if len(str) < 2 or len(str) + 3 < len(str_text):
        str = translit(str_text)
        str = translit(str)

    str = str.replace(u"'", u'')
    str = str.replace(u'"', u'')

    if len(str) < 2:
        str = u''
    return str


def get_price_from_string_w_del_tails(string):
    string = del_bad_symbols(string)

    while string.find(' ') > -1:
        string = string.replace(' ', '')
    string = string.replace(u'$', '')
    string = string.replace(u'USD', '')
    string = string.replace(u'Br', '')
    string = string.replace(u'руб.', '')
    string = string.replace(u',', '.')

    return string


def kill_pretexts(txt):
    pretexts = [
        'в', 'без', 'до', 'из', 'к', 'на', 'по', 'о', 'от', 'перед', 'при', 'через', 'с', 'у', 'за', 'над',
        'об', 'под', 'про', 'для'
    ]

    words = txt.split(' ')
    words = [item for item in words if not item in pretexts]

    return ' '.join(words)


def stay_only_text_and_numbers(txt):
    bad_symbols = '"~`{}[]|!@#$%^&*()_+№;:?=   '
    nums = '0123456789'

    for symbol in bad_symbols:
        txt = txt.replace(symbol, ' ')

    symbols_for_check = ',.'
    i = 0
    while i < len(txt):
        if txt[i] in ['.', ',']:
            if i < 1 or not txt[i - 1] in nums or i == len(txt) - 1 or not txt[i + 1] in nums:
                txt_list = list(txt)
                txt_list[i] = ' '
                txt = ''.join(txt_list)
        # if txt[i] in ['"']:
        #     if i < 1 or not txt[i - 1] in nums:
        #         txt_list = list(txt)
        #         txt_list[i] = ' '
        #         txt = ''.join(txt_list)

        i += 1

    txt = txt.strip()
    while '  ' in txt:
        txt = txt.replace('  ', ' ')

    return txt


def del_bad_symbols_and_enters_and_tags(string):
    # from string import maketrans

    try:

        string = strip_tags(string)
        string = string.replace('\r\n', '')
        del_bad_symbols(string)
    except:
        pass

    return string


def del_bad_symbols(string):
    # from string import maketrans

    try:

        # string = strip_tags(string)
        # string = string.replace('\r\n','')
        string = string.strip()

        while string.find('&nbsp;') > -1:
            string = string.replace('&nbsp;', ' ')
        # table = maketrans('&nbsp;', '     ')
        # string = string.translate(table)

        while string.find('  ') > -1:
            string = string.replace('  ', ' ')
    except:
        pass

    return string


# def get_offers_from_cookie(request):
#     if 'oknaplast_right_offers' in request.COOKIES:
#         order_list = json.loads(request.COOKIES['oknaplast_right_offers'], encoding='utf8')
#         return WindowOfferModel.objects.filter(id__in=order_list)
#     else:
#         return []


def del_nbsp(string):
    mapping = [
        ("&quot;", u'"'),
        ('&amp;', u'&'),
        ('&lt;', u'<'),
        ('&gt;', u'>'),
        ('&nbsp;', u' '),
        ('&iexcl;', u'¡'),
        ('&cent;', u'¢'),
        ('&pound;', u'£'),
        ('&curren;', u'¤'),
        ('&yen;', u'¥'),
        ('&brvbar;', u'¦'),
        ('&sect;', u'§'),
        ('&uml;', u'¨'),
        ('&copy;', u'©'),
        ('&ordf;', u'ª'),
        ('&laquo;', u'«'),
        ('&not;', u'¬'),
        ('&reg;', u'®'),
        ('&macr;', u'¯'),
        ('&deg;', u'°'),
        ('&plusmn;', u'±'),
        ('&sup2;', u'²'),
        ('&sup3;', u'³'),
        ('&acute;', u'´'),
        ('&micro;', u'µ'),
        ('&para;', u'¶'),
        ('&middot;', u'•'),
        ('&cedil;', u'¸'),
        ('&sup1;', u'¹'),
        ('&ordm;', u'º'),
        ('&raquo;', u'»'),
        ('&frac14;', u'¼'),
        ('&frac12;', u'½'),
        ('&frac34;', u'¾'),
        ('&euro;', u'€'),
        ('\n', ''),
        ('\r', ''),
        ('\t', ' '),
        ('&mdash;', '-'),
    ]
    for pair in mapping:
        string = string.replace(pair[0], pair[1])
    return string
