# -*- coding: utf-8 -*-

# Project modules
from ..utils import unicode_or_none
from ..models import Exportable, GenericObject, ExportFormat

from .parse import parse_artboards, parse_slices


############################################################
# DIMENSIONS
############################################################
class Bounds(GenericObject):
  def __init__(self, bounds_str):
    bounds_list = [float(num) for num in bounds_str.split(',')]

    self.x, self.y = bounds_list[0], bounds_list[1]
    self.width, self.height = bounds_list[2], bounds_list[3]

  def __unicode__(self):
    return u'<Bounds (x=%s, y=%s, width=%s, height=%s)>' % (self.x, self.y, self.width, self.height)


class Rect(GenericObject):
  def __init__(self, rect_dict):
    self.x, self.y = float(rect_dict['x']), float(rect_dict['y'])
    self.width, self.height = float(rect_dict['width']), float(rect_dict['height'])

  def __unicode__(self):
    return u'<Rect (x=%s, y=%s, width=%s, height=%s)>' % (self.x, self.y, self.width, self.height)


############################################################
# EXPORTABLE OBJECTS
############################################################
class SketchExportable(Exportable):
  ''' Base class for any exportable Sketch item, i.e. Pages, Artboards, Slices '''
  def __init__(self, filename, exportable_dict):
    self.id = unicode_or_none(exportable_dict, 'id')
    self.name = unicode_or_none(exportable_dict, 'name')
    self.filename = filename  # The file we're exporting from
    super(SketchExportable, self).__init__()

  def __unicode__(self):
    return u'<SketchExportable (id="%s", name="%s")>' % (self.id, self.name)


class Page(SketchExportable):
  def __init__(self, filename, page_dict):
    self.bounds = Bounds(unicode_or_none(page_dict, 'bounds'))
    self.slices = parse_slices(filename, page_dict)
    self.artboards = parse_artboards(filename, page_dict)
    super(Page, self).__init__(filename, page_dict)

  def export(self, export_format=ExportFormat.PNG):
    from .export import export_pages
    return export_pages(self.filename, item_id=self.id,
                        export_format=export_format)[0]

  def __unicode__(self):
    return u'<Page (id="%s", name="%s", bounds=%s, slices=%s, artboards=%s)>' % \
           (self.id, self.name, self.bounds, unicode(self.slices), unicode(self.artboards))


class Slice(SketchExportable):
  def __init__(self, filename, slice_dict):
    self.rect = Rect(slice_dict['rect'])
    super(Slice, self).__init__(filename, slice_dict)

  def export(self, export_format=ExportFormat.PNG):
    from .export import export_slices
    return export_slices(self.filename, item_id=self.id,
                         export_format=export_format)[0]

  def __unicode__(self):
    return u'<Slice (id="%s", name="%s", rect=%s)>' % (self.id, self.name, self.rect)


class Artboard(SketchExportable):
  def __init__(self, filename, artboard_dict):
    self.rect = Rect(artboard_dict['rect'])
    super(Artboard, self).__init__(filename, artboard_dict)

  def export(self, export_format=ExportFormat.PNG):
    from .export import export_artboards
    return export_artboards(self.filename, item_id=self.id,
                            export_format=export_format)[0]

  def __unicode__(self):
    return u'<Artboard (id="%s", name="%s", rect=%s)>' % (self.id, self.name, self.rect)

@@ -2048,30 +2048,31 @@
 t_pages%0A    
-return
+pages =
  export_page
@@ -2108,32 +2108,8 @@
 .id,
-%0A                       
  exp
@@ -2125,35 +2125,71 @@
 t=export_format)
-%5B0%5D
+%0A    return pages%5B0%5D if pages else None
 %0A%0A  def __unicod
@@ -2636,30 +2636,32 @@
 _slices%0A    
-return
+slices =
  export_slic
@@ -2698,33 +2698,8 @@
 .id,
-%0A                        
  exp
@@ -2715,35 +2715,73 @@
 t=export_format)
-%5B0%5D
+%0A    return slices%5B0%5D if slices else None
 %0A%0A  def __unicod
@@ -3162,22 +3162,27 @@
 rds%0A    
-return
+artboards =
  export_
@@ -3226,36 +3226,8 @@
 .id,
-%0A                           
  exp
@@ -3251,19 +3251,63 @@
 _format)
-%5B0%5D
+%0A    return artboards%5B0%5D if artboards else None
 %0A%0A  def 

return none if sketch tool export is blank