from django.shortcuts import render
from django.views.generic import TemplateView
from django.utils.text import slugify

from .models import TournamentPage

# Create your views here.


def func(id, page):
    t = TournamentPage.objects.get(
        id=id, published=True)

    remap = {
        'wyniki': 'schedule_and_results',
        'rejestracja': 'registration',
        'regulamin': 'rules',
        'nagrody': 'fee_and_prizes',
        'noclegi': 'accomodation',
        'kontakt': 'contact',
        f'{slugify(t.schedule_and_results_rename)}': 'schedule_and_results',
        f'{slugify(t.registration_rename)}': 'registration',
        f'{slugify(t.rules_rename)}': 'rules',
        f'{slugify(t.fee_and_prizes_rename)}': 'fee_and_prizes',
        f'{slugify(t.accomodation_rename)}': 'accomodation',
        f'{slugify(t.contact_rename)}': 'contact'
    }

    rename = {
        'schedule_and_results': (t.schedule_and_results_rename, slugify(t.schedule_and_results_rename)) if t.schedule_and_results_enabled else '',
        'registration': (t.registration_rename, slugify(t.registration_rename)) if t.registration_enabled else '',
        'rules': (t.rules_rename, slugify(t.rules_rename)) if t.rules_enabled else '',
        'fee_and_prizes': (t.fee_and_prizes_rename, slugify(t.fee_and_prizes_rename)) if t.fee_and_prizes_enabled else '',
        'accomodation': (t.accomodation_rename, slugify(t.accomodation_rename)) if t.accomodation_enabled else '',
        'contact': (t.contact_rename, slugify(t.contact_rename)) if t.contact_enabled else '',
    }

    page = remap[page] if page in remap else page

    return {'t': t, 'rename': rename, 'content': t.__dict__[page], 'content_buttons': t.__dict__[f'{page}_buttons'], 'page': page}

class HomeView(TemplateView):
    template_name = "tournament.html"

    def get_context_data(self, id, **kwargs):
        return func(id, 'homepage')


class PageView(TemplateView):
    template_name = "tournament.html"

    def get_context_data(self, id, page, **kwargs):
        t = TournamentPage.objects.get(
            id=id, published=True)

        remap = {
            'wyniki': 'schedule_and_results',
            'rejestracja': 'registration',
            'regulamin': 'rules',
            'nagrody': 'fee_and_prizes',
            'noclegi': 'accomodation',
            'kontakt': 'contact',
            f'{slugify(t.schedule_and_results_rename)}': 'schedule_and_results',
            f'{slugify(t.registration_rename)}': 'registration',
            f'{slugify(t.rules_rename)}': 'rules',
            f'{slugify(t.fee_and_prizes_rename)}': 'fee_and_prizes',
            f'{slugify(t.accomodation_rename)}': 'accomodation',
            f'{slugify(t.contact_rename)}': 'contact'
        }

        rename = {
            'schedule_and_results': (t.schedule_and_results_rename, slugify(t.schedule_and_results_rename)) if t.schedule_and_results_enabled else '',
            'registration': (t.registration_rename, slugify(t.registration_rename)) if t.registration_enabled else '',
            'rules': (t.rules_rename, slugify(t.rules_rename)) if t.rules_enabled else '',
            'fee_and_prizes': (t.fee_and_prizes_rename, slugify(t.fee_and_prizes_rename)) if t.fee_and_prizes_enabled else '',
            'accomodation': (t.accomodation_rename, slugify(t.accomodation_rename)) if t.accomodation_enabled else '',
            'contact': (t.contact_rename, slugify(t.contact_rename)) if t.contact_enabled else '',
        }

        page = remap[page] if page in remap else page

        return {'t': t, 'rename': rename, 'content': t.__dict__[page], 'content_buttons': t.__dict__[f'{page}_buttons'], 'page': page}


class ScheduleAndResultsView(TemplateView):
    template_name = "tournament.html"

    def get_context_data(self, id, **kwargs):
        t = TournamentPage.objects.get(
            id=id, published=True, schedule_and_results_enabled=True)
        return {'t': t, 'content': t.schedule_and_results, 'content_buttons': t.schedule_and_results_buttons, 'schedule_and_results': True}


class RegistrationView(TemplateView):
    template_name = "tournament.html"

    def get_context_data(self, id, **kwargs):
        t = TournamentPage.objects.get(
            id=id, published=True, registration_enabled=True)
        return {'t': t, 'content': t.registration, 'content_buttons': t.registration_buttons, 'registration': True}


class RulesView(TemplateView):
    template_name = "tournament.html"

    def get_context_data(self, id, **kwargs):
        t = TournamentPage.objects.get(
            id=id, published=True, rules_enabled=True)
        return {'t': t, 'content': t.rules, 'content_buttons': t.rules_buttons, 'rules': True}


class FeeAndPrizesView(TemplateView):
    template_name = "tournament.html"

    def get_context_data(self, id, **kwargs):
        t = TournamentPage.objects.get(
            id=id, published=True, fee_and_prizes_enabled=True)
        return {'t': t, 'content': t.fee_and_prizes, 'content_buttons': t.fee_and_prizes_buttons, 'fee_and_prizes': True}


class AccomodationView(TemplateView):
    template_name = "tournament.html"

    def get_context_data(self, id, **kwargs):
        t = TournamentPage.objects.get(
            id=id, published=True, accomodation_enabled=True)
        return {'t': t, 'content': t.accomodation, 'content_buttons': t.accomodation_buttons, 'accomodation': True}


class ContactView(TemplateView):
    template_name = "tournament.html"

    def get_context_data(self, id, **kwargs):
        t = TournamentPage.objects.get(
            id=id, published=True, contact_enabled=True)
        return {'t': t, 'content': t.contact, 'content_buttons': t.contact_buttons, 'contact': True}
