__author__ = 'SDE'

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

from django.core.serializers import serialize
from django.db.models.query import QuerySet
# import simplejson
from django.template import Library
from django.utils.html import mark_safe

@register.filter('get_value_from_dict')
def get_value_from_dict(dict_data, key):
    """
    usage example {{ your_dict|get_value_from_dict:your_key }}
    """

    if key in dict_data:
        res = dict_data[key]
        return res

    return False



@register.filter()
def get_rows_count_by_cols_count(data, cols_count):
    rows_count = len(data) // cols_count
    if len(data) % cols_count:
        rows_count += 1
    return rows_count

@register.filter()
def get_numbers_list(from_el, to_el):
    res = range(from_el, to_el+1)
    return res


def val_type(value):
    res = type(value)
    return res.__name__
register.filter('val_type', val_type)

@register.filter()
def get_cols_table_data_for_row_when_cols3(value, row):
    el_count = 3
    from_el = (row-1) * el_count
    to_el = row * el_count
    part = list(value)[from_el:to_el]
    return part
# register.filter('val_type', val_type)


@register.filter
@stringfilter
def correct_for_tables(value):
    if value in ['None', '0.0']:
        return '-'
    return value


@register.filter
@stringfilter
def del_bad_symbols(value):
    from BaseModels.functions import del_bad_symbols
    return del_bad_symbols(value)


@register.filter
@stringfilter
def del_amp_symbols(value):
    from BaseModels.functions import del_nbsp
    return del_nbsp(value)

@register.filter
@stringfilter
def del_lang_from_path(value):
    path_list = value.split('/')
    path = u''
    for i in path_list[1:]:
        path.join(i + '/')
    return path

@register.filter
@stringfilter
def get_color_by_number(value, arg=None):

    color = None
    try:
        val = float(value)

        if not color and arg == u'%':

            color = u'black'
            if val > 50:
                color = u'green'
            elif val <= 50 and val >= 25:
                color = u'#6c8107'
            elif val <= 25 and val >= 10:
                color = u'#a89803'
            elif val <= 10 and val >= 5:
                color = u'#e6a707'
            elif val <= 5 and val >= 0:
                color = u'#e67307'
            elif val <= 0:
                color = u'red'


        # val_range = val_max - val_min
        # # val_percent = (val_range * 100 / val) - 100
        # offset = -(val_min + -(val))
        # if val <0:
        #     val = offset
        # if val > val_max:
        #     val = val_max
        # elif val < 0:
        #     val = 0
        #
        # color_range = 16711680 - 1211136
        # val_1unit = float(color_range) / float(val_range)
        # dec_color = 16711680 - int(val_1unit * val)

        if not color:
            color = u'black'
            if val > 1000:
                color = u'green'
            elif val <= 1000 and val >= 500:
                color = u'#6c8107'
            elif val <= 500 and val >= 250:
                color = u'#a89803'
            elif val <= 250 and val >= 125:
                color = u'#e6a707'
            elif val <= 125 and val >= 50:
                color = u'#e67307'
            elif val <= 50:
                color = u'red'

        # s = u'style="color: #{0}12;"'.format(str(hex(dec_color))[2:6])
        s = u'style="color: {0};"'.format(color)
        return s
    except:
        return u''


# @register.filter
# @stringfilter
# def check_aprox_compare_strings(search_phrase, txt):
#     from ProductApp.search import get_highlight_string
#
#     s = get_highlight_string(search_phrase, txt)
#
#     return s

