from django.contrib.gis.db import models
from lingcod.features import register
from lingcod.features.models import Feature
from django.utils.html import escape
from django.conf import settings

class Bookmark(Feature):
    description = models.TextField(default="", null=True, blank=True)
    latitude = models.FloatField()
    longitude = models.FloatField()
    altitude = models.FloatField()
    heading = models.FloatField(default=0)
    tilt = models.FloatField(default=0)
    roll = models.FloatField(default=0)
    altitudeMode = models.FloatField(default=1)
    ip = models.IPAddressField(default="0.0.0.0", null=True, blank=True)
    publicstate = models.TextField(default="{}")
    
    @property
    def kml(self):
        camera = "<Camera>\n"
        camera_params = ["latitude", "longitude", "altitude", "heading", "tilt", "roll", "altitudeMode"]
        for p in camera_params:
            val = self.__dict__[p]
            if val is not None:
                camera += "                <%s>%s</%s>\n" % (p, val, p)
        camera += "            </Camera>\n"
          
        return """
        <Placemark id="%s">
            <visibility>1</visibility>
            <name>%s</name>
            <description>%s</description>
            <styleUrl>#%s-default</styleUrl>
            %s
        </Placemark>
        """ % (self.uid, escape(self.name), escape(self.description), self.model_uid(), 
               camera) 

    @property
    def kml_style(self):
        return """
        <Style id="%s-default"> 
            <!-- invisible -->
            <IconStyle>
                <scale>0.0</scale>
            </IconStyle>
            <LabelStyle>
                <scale>0.0</scale>
            </LabelStyle>
        </Style>
        """ % (self.model_uid())


    class Options:
        manipulators = []
        optional_manipulators = [ ]
        verbose_name = 'Bookmark'
        form = 'lingcod.bookmarks.forms.BookmarkForm'
        icon_url = 'bookmarks/images/bookmark.png'
        form_template = 'bookmarks/form.html'
        show_template = 'bookmarks/show.html'

if settings.BOOKMARK_FEATURE:
    Bookmark = register(Bookmark)

@@ -1211,80 +1211,153 @@
    %3C
-description%3E%25s%3C/description%3E%0A            %3CstyleUrl%3E#%25s-default%3C/styleUrl
+styleUrl%3E#%25s-default%3C/styleUrl%3E%0A            %3CExtendedData%3E%0A                %3CData name=%22descr%22%3E%3Cvalue%3E%25s%3C/value%3E%3C/Data%3E%0A            %3C/ExtendedData
 %3E%0A  
@@ -1433,16 +1433,34 @@
 f.name),
+ self.model_uid(),
  escape(
@@ -1481,42 +1481,8 @@
 on),
- self.model_uid(), %0A              
  cam
@@ -1610,16 +1610,250 @@
 ble --%3E%0A
+            %3CBalloonStyle%3E%0A                %3CbgColor%3Effeeeeee%3C/bgColor%3E%0A                %3Ctext%3E %3C!%5BCDATA%5B %0A                    %3Ch3%3E$%5Bname%5D%3C/h3%3E%0A                    %3Cp%3E$%5Bdescr%5D%3C/p%3E%0A                %5D%5D%3E %3C/text%3E%0A            %3C/BalloonStyle%3E%0A
         

Use balloon style to show description in kml