from netbox.plugins import PluginTemplateExtension
from extras.models import CustomField
from netbox_tag_id.models import TagidCustomField
from django.contrib.contenttypes.models import ContentType
from django.utils.safestring import mark_safe
import json



class CustomFieldsPage(PluginTemplateExtension):
    # model = "extras.customfield"

    def head(self):
        return '''<script src="/static/netbox_tag_id/script.js"></script>
    <link rel="stylesheet" href="/static/netbox_tag_id/style.css">'''
    
    def right_page(self):
        cf = self.context["object"]  # l'objet CustomField en cours
        if (isinstance(cf, CustomField) and cf.type == "tagid") :
            return self.customField_right_page(cf)
        
        return "" 

    def customField_right_page(self, cf):
        try:
            cfg = TagidCustomField.objects.get(tag_id=cf.pk)
            data = {
                "prefix": cfg.prefix,
                "suffix": cfg.suffix,
                "length": cfg.length,
                "printer_name": cfg.printer_name,
                "cups_host": cfg.cups_host,
                "printer_opt": cfg.printer_opt,
                "printer_count": cfg.printer_count
            }
        except TagidCustomField.DoesNotExist:
            data = None

        return self.render('tagid_panel.html', extra_context={"cfg": data})


template_extensions = [CustomFieldsPage]