# -*- coding: utf-8 -*-

from autocomplete_light import shortcuts as autocomplete_light

from .models import Skola, Resitel, Problem, Organizator
from taggit.models import Tag


autocomplete_light.register(Tag)


class SkolaAutocomplete(autocomplete_light.AutocompleteModelBase):

    model = Skola

    search_fields = ['nazev', 'mesto', 'ulice']

    split_words = True

    limit_choices = 15

    attrs = {
        # This will set the input placeholder attribute:
        'placeholder': 'Škola',
        # This will set the yourlabs.Autocomplete.minimumCharacters
        # options, the naming conversion is handled by jQuery
        'data-autocomplete-minimum-characters': 1,
    }

    widget_attrs = {
        'data-widget-maximum-values': 15,
        'class': 'modern-style',
    }

autocomplete_light.register(SkolaAutocomplete)


class ResitelAutocomplete(autocomplete_light.AutocompleteModelBase):

    model = Resitel
    
    search_fields = ['jmeno', 'prijmeni']

    split_words = False

    limit_choices = 15

    def choice_label(self, resitel):
        return "%s, %s (%s)" % (resitel.plne_jmeno(), resitel.mesto, resitel.rok_maturity)

    attrs= {
        # This will set the input placeholder attribute:
        'placeholder': 'Řešitel',
        # This will set the yourlabs.Autocomplete.minimumCharacters
        # options, the naming conversion is handled by jQuery
        'data-autocomplete-minimum-characters': 1,
    }

    widget_attrs = {
        'data-widget-maximum-values': 15,
        # Enable modern-style widget !
        'class': 'modern-style',
    }

autocomplete_light.register(ResitelAutocomplete)

class OrganizatorAutocomplete(autocomplete_light.AutocompleteModelBase):

    model = Organizator
    
    search_fields = ['user__first_name', 'user__last_name', 'prezdivka']

    split_words = False

    limit_choices = 15

    def choice_label(self, organizator):
        return "%s '%s' %s" % (organizator.user.first_name,
                                organizator.prezdivka,
                                organizator.user.last_name)

    attrs = {
        # This will set the input placeholder attribute:
        'placeholder': 'Organizátor',
        # This will set the yourlabs.Autocomplete.minimumCharacters
        # options, the naming conversion is handled by jQuery
        'data-autocomplete-minimum-characters': 1,
    }

    widget_attrs = {
        'data-widget-maximum-values': 15,
        # Enable modern-style widget !
        'class': 'modern-style',
    }

autocomplete_light.register(OrganizatorAutocomplete)



class ProblemAutocomplete(autocomplete_light.AutocompleteModelBase):

    model = Problem
    
    search_fields = ['nazev']

    split_words = False

    limit_choices = 10

    def choice_label(self, p):
        if p.stav == Problem.STAV_ZADANY:
            popisek = ""
            try:
                popisek = "%s (%s, %s.%s)".format(p.nazev, p.typ, p.cislo_zadani.rocnik.rocnik, p.kod_v_rocniku())
            except:
                #popisek = "%s (%s, %s.%s)".format(p.nazev, p.typ, p.stav)
                popisek = "CHYBA"
            return popisek
        else:
            return "%s (%s, %s)".format(p.nazev, p.typ, p.stav)

    attrs = {
        # This will set the input placeholder attribute:
        'placeholder': 'Problém',
        # This will set the yourlabs.Autocomplete.minimumCharacters
        # options, the naming conversion is handled by jQuery
        'data-autocomplete-minimum-characters': 1,
    }

    widget_attrs = {
        'data-widget-maximum-values': 10,
        # Enable modern-style widget !
        'class': 'modern-style',
    }

#FIXME Nefunguje, nevime proc
#autocomplete_light.register(ProblemAutocomplete)