# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import yaml

from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _

from horizon import exceptions
from horizon import forms
from horizon import tables
from horizon import tabs
from horizon.utils import memoized

from senlin_dashboard.api import senlin
from senlin_dashboard.cluster.nodes import forms as nodes_forms
from senlin_dashboard.cluster.nodes.tables import NodesTable
from senlin_dashboard.cluster.nodes import tabs as nodes_tabs


class IndexView(tables.DataTableView):
    table_class = NodesTable
    template_name = 'cluster/nodes/index.html'
    page_title = _("Nodes")

    def get_data(self):
        try:
            params = {}
            nodes = senlin.node_list(self.request, params)
        except Exception:
            nodes = []
            exceptions.handle(self.request,
                              _('Unable to retrieve nodes.'))
        return nodes


class CreateView(forms.ModalFormView):
    template_name = 'cluster/nodes/create.html'
    page_title = _("Create Node")
    form_class = nodes_forms.CreateForm
    submit_url = reverse_lazy("horizon:cluster:nodes:create")
    success_url = reverse_lazy("horizon:cluster:nodes:index")


class DetailView(tabs.TabView):
    tab_group_class = nodes_tabs.NodeDetailTabs
    template_name = 'horizon/common/_detail.html'
    page_title = "{{ node.name }}"
    profile_url = 'horizon:cluster:profiles:detail'

    @memoized.memoized_method
    def get_object(self):
        try:
            # Get initial node information
            node_id = self.kwargs["node_id"]
            node = senlin.node_get(self.request, node_id)
        except Exception:
            msg = _("Unable to retrieve node.")
            url = reverse_lazy("horizon:cluster:nodes:index")
            exceptions.handle(self.request, msg, redirect=url)
        return node

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        table = NodesTable(self.request)
        node = self.get_object()
        node.profile_url = reverse_lazy(self.profile_url,
                                        args=[node.profile_id])
        context["actions"] = table.render_row_actions(node)
        context["node"] = node
        context["url"] = reverse_lazy("horizon:cluster:nodes:index")
        return context

    def get_tabs(self, request, *args, **kwargs):
        node = self.get_object()
        return self.tab_group_class(request, node=node, **kwargs)


class UpdateView(forms.ModalFormView):
    template_name = 'cluster/nodes/update.html'
    page_title = _("Update Node")
    form_class = nodes_forms.UpdateNodeForm
    submit_url = reverse_lazy("horizon:cluster:nodes:update")
    success_url = reverse_lazy("horizon:cluster:nodes:index")

    @memoized.memoized_method
    def get_object(self):
        try:
            # Get initial node information
            node_id = self.kwargs["node_id"]
            node = senlin.node_get(self.request, node_id)
            # Metadata in update form should be empty rather than {}
            if not node.metadata:
                metadata = None
            else:
                metadata = yaml.safe_dump(
                    node.metadata,
                    default_flow_style=False)
            node_dict = {"node_id": node_id,
                         "name": node.name,
                         "profile_id": node.profile_id,
                         "role": node.role,
                         "metadata": metadata}

        except Exception:
            msg = _("Unable to retrieve node.")
            url = reverse_lazy("horizon:cluster:nodes:index")
            exceptions.handle(self.request, msg, redirect=url)
        return node_dict

    def get_context_data(self, **kwargs):
        args = (self.kwargs["node_id"],)
        context = super(UpdateView, self).get_context_data(**kwargs)
        context["node"] = self.get_object()
        context["submit_url"] = reverse_lazy(
            "horizon:cluster:nodes:update",
            args=args)
        return context

    def get_initial(self):
        return self.get_object()

@@ -1213,32 +1213,8 @@
 ry:%0A
-            params = %7B%7D%0A
     
@@ -1262,16 +1262,8 @@
 uest
-, params
 )%0A  

Remove unnecessary params