import defcon
from fontTools.misc.py23 import basestring
from fontParts.base import BaseFont
from fontParts.fontshell.base import RBaseObject
from fontParts.fontshell.info import RInfo
from fontParts.fontshell.groups import RGroups
from fontParts.fontshell.kerning import RKerning
from fontParts.fontshell.features import RFeatures
from fontParts.fontshell.lib import RLib
from fontParts.fontshell.layer import RLayer
from fontParts.fontshell.guideline import RGuideline


class RFont(RBaseObject, BaseFont):

    wrapClass = defcon.Font
    infoClass = RInfo
    groupsClass = RGroups
    kerningClass = RKerning
    featuresClass = RFeatures
    libClass = RLib
    layerClass = RLayer
    guidelineClass = RGuideline

    # ---------------
    # File Operations
    # ---------------

    # Initialize

    def _init(self, pathOrObject=None, showInterface=True, **kwargs):
        if isinstance(pathOrObject, basestring):
            font = self.wrapClass(pathOrObject)
        elif pathOrObject is None:
            font = self.wrapClass()
        else:
            font = pathOrObject
        self._wrapped = font
        self._wrappedInfo = None
        self._wrappedGroups = None
        self._wrappedKerning = None
        self._wrappedFeatures = None
        self._wrappedLib = None

    # path

    def _get_path(self, **kwargs):
        return self.naked().path

    # save

    def _save(self, path=None, showProgress=False,
              formatVersion=None, **kwargs):
        self.naked().save(path=path, formatVersion=formatVersion)

    # close

    def _close(self, **kwargs):
        del self._wrapped

    # -----------
    # Sub-Objects
    # -----------

    # info

    def _get_info(self):
        if self._wrappedInfo is None:
            self._wrappedInfo = self.infoClass(wrap=self.naked().info)
        return self._wrappedInfo

    # groups

    def _get_groups(self):
        if self._wrappedGroups is None:
            self._wrappedGroups = self.groupsClass(wrap=self.naked().groups)
        return self._wrappedGroups

    # kerning

    def _get_kerning(self):
        if self._wrappedKerning is None:
            self._wrappedKerning = self.kerningClass(wrap=self.naked().kerning)
        return self._wrappedKerning

    # features

    def _get_features(self):
        if self._wrappedFeatures is None:
            self._wrappedFeatures = self.featuresClass(
                                        wrap=self.naked().features
                                    )
        return self._wrappedFeatures

    # lib

    def _get_lib(self):
        if self._wrappedLib is None:
            self._wrappedLib = self.libClass(wrap=self.naked().lib)
        return self._wrappedLib

    # ------
    # Layers
    # ------

    def _get_layers(self, **kwargs):
        return [self.layerClass(wrap=layer) for layer in self.naked().layers]

    # order

    def _get_layerOrder(self, **kwargs):
        return self.naked().layers.layerOrder

    def _set_layerOrder(self, value, **kwargs):
        self.naked().layers.layerOrder = value

    # default layer

    def _get_defaultLayerName(self):
        return self.naked().layers.defaultLayer.name

    def _set_defaultLayerName(self, value, **kwargs):
        for layer in self.layers:
            if layer.name == value:
                break
        layer = layer.naked()
        self.naked().layers.defaultLayer = layer

    # new

    def _newLayer(self, name, color, **kwargs):
        layers = self.naked().layers
        layer = layers.newLayer(name)
        layer.color = color
        return self.layerClass(wrap=layer)

    # remove

    def _removeLayer(self, name, **kwargs):
        layers = self.naked().layers
        del layers[name]

    # ------
    # Glyphs
    # ------

    def _get_glyphOrder(self):
        return self.naked().glyphOrder

    def _set_glyphOrder(self, value):
        self.naked().glyphOrder = value

    # ----------
    # Guidelines
    # ----------

    def _lenGuidelines(self, **kwargs):
        return len(self.naked().guidelines)

    def _getGuideline(self, index, **kwargs):
        guideline = self.naked().guidelines[index]
        return self.guidelineClass(guideline)

    def _appendGuideline(self, position, angle,
                         name=None, color=None, **kwargs):
        guideline = self.guidelineClass().naked()
        guideline.x = position[0]
        guideline.y = position[1]
        guideline.name = name
        guideline.color = color
        self.naked().appendGuideline(guideline)
        return self.guidelineClass(guideline)

    def _removeGuideline(self, index, **kwargs):
        guideline = self.naked().guidelines[index]
        self.naked().removeGuideline(guideline)

@@ -1115,181 +1115,8 @@
 font
-%0A        self._wrappedInfo = None%0A        self._wrappedGroups = None%0A        self._wrappedKerning = None%0A        self._wrappedFeatures = None%0A        self._wrappedLib = None
 %0A%0A  
@@ -1545,69 +1545,14 @@
     
-if self._wrappedInfo is None:%0A            self._wrappedInfo =
+return
  sel
@@ -1590,41 +1590,8 @@
 nfo)
-%0A        return self._wrappedInfo
 %0A%0A  
@@ -1641,73 +1641,14 @@
     
-if self._wrappedGroups is None:%0A            self._wrappedGroups =
+return
  sel
@@ -1690,43 +1690,8 @@
 ups)
-%0A        return self._wrappedGroups
 %0A%0A  
@@ -1743,75 +1743,14 @@
     
-if self._wrappedKerning is None:%0A            self._wrappedKerning =
+return
  sel
@@ -1794,44 +1794,8 @@
 ing)
-%0A        return self._wrappedKerning
 %0A%0A  
@@ -1849,343 +1849,112 @@
     
-if self._wrappedFeatures is None:%0A            self._wrappedFeatures = self.featuresClass(%0A                                        wrap=self.naked().features%0A                                    )%0A        return self._wrappedFeatures%0A%0A    # lib%0A%0A    def _get_lib(self):%0A        if self._wrappedLib is None:%0A            self._wrappedLib =
+return self.featuresClass(wrap=self.naked().features)%0A%0A    # lib%0A%0A    def _get_lib(self):%0A        return
  sel
@@ -1990,40 +1990,8 @@
 lib)
-%0A        return self._wrappedLib
 %0A%0A  

Remove object caching.