"""_____________________________________________________________________

:PROJECT: LARAsuite

*lara_django_processes views *

:details: lara_django_processes views module.
         - add app specific urls here
         - 
:authors: mark doerr <mark.doerr@uni-greifswald.de>

.. note:: -
.. todo:: -
________________________________________________________________________
"""


from django.shortcuts import get_object_or_404, render
from dataclasses import dataclass, field
from typing import List

from django.shortcuts import get_object_or_404, render, redirect
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse

from django.views.generic import (
    DetailView,
    ListView,
    CreateView,
    UpdateView,
    DeleteView,
)

from django_tables2 import SingleTableView

from .models import (
    Method,
    Procedure,
    Process,
    MethodInstance,
    ProcedureInstance,
    ProcessInstance,
)
from .forms import (
    MethodCreateForm,
    MethodUpdateForm,
    MethodInstanceCreateForm,
    MethodInstanceUpdateForm,
    ProcedureCreateForm,
    ProcedureUpdateForm,
    ProcedureInstanceCreateForm,
    ProcedureInstanceUpdateForm,
    ProcessCreateForm,
    ProcessUpdateForm,
    ProcessInstanceCreateForm,
    ProcessInstanceUpdateForm,
)
from .tables import (
    MethodClassTable,
    MethodTable,
    MethodInstanceTable,
    ProcedureTable,
    ProcedureInstanceTable,
    ProcessTable,
    ProcessInstanceTable,
)

# Create your  lara_django_processes views here.


@dataclass
class ProcessesMenu:
    menu_items: List[dict] = field(
        default_factory=lambda: [
            {
                "name": "Procdure Instances",
                "path": "lara_django_processes:procedure-instance-list",
            },
            {"name": "Procedure", "path": "lara_django_processes:procedure-list"},
            {
                "name": "Process Instances",
                "path": "lara_django_processes:process-instance-list",
            },
            {"name": "Processes", "path": "lara_django_processes:process-list"},
            {
                "name": "Method Instances",
                "path": "lara_django_processes:method-instance-list",
            },
            {"name": "Method", "path": "lara_django_processes:method-list"},
        ]
    )


class MethodSingleTableView(SingleTableView):
    model = Method
    table_class = MethodTable

    # fields = ('name', 'name_full', 'URL', 'pid', 'iri', 'manufacturer', 'model_no', 'product_type', 'type_barcode', 'product_no', 'weight', 'specifications', 'spec_json', 'spec_doc', 'brochure', 'quickstart', 'manual', 'service_manual', 'icon', 'image', 'description', 'method_id', 'method_class', 'shape')

    template_name = "lara_django_processes/list.html"
    success_url = "/processes/method/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method - List"
        context["create_link"] = "lara_django_processes:method-create"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class MethodDetailView(DetailView):
    model = Method

    template_name = "lara_django_processes/method_detail.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method - Details"
        context["update_link"] = "lara_django_processes:method-update"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class MethodCreateView(CreateView):
    model = Method

    template_name = "lara_django_processes/create_form.html"
    form_class = MethodCreateForm
    success_url = "/processes/method/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method - Create"
        return context


class MethodUpdateView(UpdateView):
    model = Method

    template_name = "lara_django_processes/update_form.html"
    form_class = MethodUpdateForm
    success_url = "/processes/method/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method - Update"
        context["delete_link"] = "lara_django_processes:method-delete"
        return context


class MethodDeleteView(DeleteView):
    model = Method

    template_name = "lara_django_processes/delete_form.html"
    success_url = "/processes/method/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method - Delete"
        context["delete_link"] = "lara_django_processes:method-delete"
        return context


# ==== Method Instance ====


class MethodInstanceSingleTableView(SingleTableView):
    model = MethodInstance
    table_class = MethodInstanceTable

    # fields = ('name', 'name_full', 'URL', 'pid', 'iri', 'manufacturer', 'model_no', 'product_type', 'type_barcode', 'product_no', 'weight', 'specifications', 'spec_json', 'spec_doc', 'brochure', 'quickstart', 'manual', 'service_manual', 'icon', 'image', 'description', 'method-instance_id', 'method-instance_class', 'shape')

    template_name = "lara_django_processes/list.html"
    success_url = "/processes/method-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method Instance - List"
        context["create_link"] = "lara_django_processes:method-instance-create"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class MethodInstanceDetailView(DetailView):
    model = MethodInstance

    template_name = "lara_django_processes/method-instance_detail.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method Instance - Details"
        context["update_link"] = "lara_django_processes:method-instance-update"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class MethodInstanceCreateView(CreateView):
    model = MethodInstance

    template_name = "lara_django_processes/create_form.html"
    form_class = MethodInstanceCreateForm
    success_url = "/processes/method-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method Instance - Create"
        return context


class MethodInstanceUpdateView(UpdateView):
    model = MethodInstance

    template_name = "lara_django_processes/update_form.html"
    form_class = MethodInstanceUpdateForm
    success_url = "/processes/method-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method Instance - Update"
        context["delete_link"] = "lara_django_processes:method-instance-delete"
        return context


class MethodInstanceDeleteView(DeleteView):
    model = MethodInstance

    template_name = "lara_django_processes/delete_form.html"
    success_url = "/processes/method-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Method Instance - Delete"
        context["delete_link"] = "lara_django_processes:method-instance-delete"
        return context


# ==== Procdure ====


class ProcedureSingleTableView(SingleTableView):
    model = Procedure
    table_class = ProcedureTable

    # fields = ('name', 'name_full', 'URL', 'pid', 'iri', 'manufacturer', 'model_no', 'product_type', 'type_barcode', 'product_no', 'weight', 'specifications', 'spec_json', 'spec_doc', 'brochure', 'quickstart', 'manual', 'service_manual', 'icon', 'image', 'description', 'procedure_id', 'procedure_class', 'shape')

    template_name = "lara_django_processes/list.html"
    success_url = "/processes/procedure/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Procedure - List"
        context["create_link"] = "lara_django_processes:procedure-create"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class ProcedureDetailView(DetailView):
    model = Procedure

    template_name = "lara_django_processes/procedure_detail.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Procedure - Details"
        context["update_link"] = "lara_django_processes:procedure-update"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class ProcedureCreateView(CreateView):
    model = Procedure

    template_name = "lara_django_processes/create_form.html"
    form_class = ProcedureCreateForm
    success_url = "/processes/procedure/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Procedure - Create"
        return context


class ProcedureUpdateView(UpdateView):
    model = Procedure

    template_name = "lara_django_processes/update_form.html"
    form_class = ProcedureUpdateForm
    success_url = "/processes/procedure/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Procedure - Update"
        context["delete_link"] = "lara_django_processes:procedure-delete"
        return context


class ProcedureDeleteView(DeleteView):
    model = Procedure

    template_name = "lara_django_processes/delete_form.html"
    success_url = "/processes/procedure/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Procedure - Delete"
        context["delete_link"] = "lara_django_processes:procedure-delete"
        return context


class ProcedureInstanceSingleTableView(SingleTableView):
    model = ProcedureInstance
    table_class = ProcedureInstanceTable

    # fields = ('name', 'name_full', 'URL', 'pid', 'iri', 'manufacturer', 'model_no', 'product_type', 'type_barcode', 'product_no', 'weight', 'specifications', 'spec_json', 'spec_doc', 'brochure', 'quickstart', 'manual', 'service_manual', 'icon', 'image', 'description', 'procedure-instance_id', 'procedure-instance_class', 'shape')

    template_name = "lara_django_processes/list.html"
    success_url = "/processes/procedure-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcedureInstance - List"
        context["create_link"] = "lara_django_processes:procedure-instance-create"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class ProcedureInstanceDetailView(DetailView):
    model = ProcedureInstance

    template_name = "lara_django_processes/procedure-instance_detail.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcedureInstance - Details"
        context["update_link"] = "lara_django_processes:procedure-instance-update"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class ProcedureInstanceCreateView(CreateView):
    model = ProcedureInstance

    template_name = "lara_django_processes/create_form.html"
    form_class = ProcedureInstanceCreateForm
    success_url = "/processes/procedure-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcedureInstance - Create"
        return context


class ProcedureInstanceUpdateView(UpdateView):
    model = ProcedureInstance

    template_name = "lara_django_processes/update_form.html"
    form_class = ProcedureInstanceUpdateForm
    success_url = "/processes/procedure-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcedureInstance - Update"
        context["delete_link"] = "lara_django_processes:procedure-instance-delete"
        return context


class ProcedureInstanceDeleteView(DeleteView):
    model = ProcedureInstance

    template_name = "lara_django_processes/delete_form.html"
    success_url = "/processes/procedure-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcedureInstance - Delete"
        context["delete_link"] = "lara_django_processes:procedure-instance-delete"
        return context


# ==== Process ====


class ProcessSingleTableView(SingleTableView):
    model = Process
    table_class = ProcessTable

    # fields = ('name', 'name_full', 'URL', 'pid', 'iri', 'manufacturer', 'model_no', 'product_type', 'type_barcode', 'product_no', 'weight', 'specifications', 'spec_json', 'spec_doc', 'brochure', 'quickstart', 'manual', 'service_manual', 'icon', 'image', 'description', 'process_id', 'process_class', 'shape')

    template_name = "lara_django_processes/list.html"
    success_url = "/processes/process/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Process - List"
        context["create_link"] = "lara_django_processes:process-create"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class ProcessDetailView(DetailView):
    model = Process

    template_name = "lara_django_processes/process_detail.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Process - Details"
        context["update_link"] = "lara_django_processes:process-update"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class ProcessCreateView(CreateView):
    model = Process

    template_name = "lara_django_processes/create_form.html"
    form_class = ProcessCreateForm
    success_url = "/processes/process/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Process - Create"
        return context


class ProcessUpdateView(UpdateView):
    model = Process

    template_name = "lara_django_processes/update_form.html"
    form_class = ProcessUpdateForm
    success_url = "/processes/process/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Process - Update"
        context["delete_link"] = "lara_django_processes:process-delete"
        return context


class ProcessDeleteView(DeleteView):
    model = Process

    template_name = "lara_django_processes/delete_form.html"
    success_url = "/processes/process/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "Process - Delete"
        context["delete_link"] = "lara_django_processes:process-delete"
        return context


class ProcessInstanceSingleTableView(SingleTableView):
    model = ProcessInstance
    table_class = ProcessInstanceTable

    # fields = ('name', 'name_full', 'URL', 'pid', 'iri', 'manufacturer', 'model_no', 'product_type', 'type_barcode', 'product_no', 'weight', 'specifications', 'spec_json', 'spec_doc', 'brochure', 'quickstart', 'manual', 'service_manual', 'icon', 'image', 'description', 'process-instance_id', 'process-instance_class', 'shape')

    template_name = "lara_django_processes/list.html"
    success_url = "/processes/process-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcessInstance - List"
        context["create_link"] = "lara_django_processes:process-instance-create"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class ProcessInstanceDetailView(DetailView):
    model = ProcessInstance

    template_name = "lara_django_processes/process-instance_detail.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcessInstance - Details"
        context["update_link"] = "lara_django_processes:process-instance-update"
        context["menu_items"] = ProcessesMenu().menu_items
        return context


class ProcessInstanceCreateView(CreateView):
    model = ProcessInstance

    template_name = "lara_django_processes/create_form.html"
    form_class = ProcessInstanceCreateForm
    success_url = "/processes/process-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcessInstance - Create"
        return context


class ProcessInstanceUpdateView(UpdateView):
    model = ProcessInstance

    template_name = "lara_django_processes/update_form.html"
    form_class = ProcessInstanceUpdateForm
    success_url = "/processes/process-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcessInstance - Update"
        context["delete_link"] = "lara_django_processes:process-instance-delete"
        return context


class ProcessInstanceDeleteView(DeleteView):
    model = ProcessInstance

    template_name = "lara_django_processes/delete_form.html"
    success_url = "/processes/process-instance/list"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["section_title"] = "ProcessInstance - Delete"
        context["delete_link"] = "lara_django_processes:process-instance-delete"
        return context


### - ---- old


def index(request):
    process_list = Process.objects.all()
    context = {"process_list": process_list}
    return render(request, "lara_django_processes/index.html", context)


def processList(request):
    process_list = Process.objects.all()
    table_caption = "List of all LARA data"
    context = {"table_caption": table_caption, "process_list": process_list}
    return render(request, "lara_django_processes/process_list.html", context)


def searchForm(request):
    return render(request, "lara_django_processes/search.html", context={})


def search(request):
    search_string = request.POST["search"]

    try:
        process_list = []
        curr_process = Process.objects.get(device__name=search_string)
        process_list.append(curr_process)

    except ValueError as err:
        sys.stderr.write("{}".format(err))
        # ~ return render(request, 'polls/detail.html', {
        # ~ 'question': question,
        # ~ 'error_message': "You didn't select a choice.",
        # ~ })
    else:
        context = {"process_list": process_list}
        return render(request, "lara_django_processes/results.html", context)
        # return HttpResponseRedirect(reverse('lara_django_processes:results', args=(lara_device_list,)))


def results(request, process_list):
    # question = get_object_or_404(Question, pk=question_id)
    context = {"process_list": process_list}
    return render(request, "lara_django_processes/results.html", context)


def processDetails(request, process_id):
    curr_process = get_object_or_404(Device, pk=process_id)
    context = {"process": curr_process}
    return render(request, "lara_django_processes/process.html", context)
