import os
from urllib import parse

from waterbutler.core import metadata

from waterbutler.providers.googledrive import utils


class BaseGoogleDriveMetadata(metadata.BaseMetadata):

    def __init__(self, raw, path):
        super().__init__(raw)
        self._path = path

    @property
    def provider(self):
        return 'googledrive'

    @property
    def path(self):
        return os.path.join(str(self._path), parse.quote(self.raw['title'], safe=''))

    @property
    def full_path(self):
        return os.path.join(parse.unquote(str(self._path)), self.raw['title'])

    @property
    def extra(self):
        return {'revisionId': self.raw['version']}


class GoogleDriveFolderMetadata(BaseGoogleDriveMetadata, metadata.BaseFolderMetadata):

    @property
    def id(self):
        return self.raw['id']

    @property
    def name(self):
        return self.raw['title']

    @property
    def path(self):
        return super().path + '/'

    @property
    def full_path(self):
        return super().path + '/'


class GoogleDriveFileMetadata(BaseGoogleDriveMetadata, metadata.BaseFileMetadata):

    @property
    def id(self):
        return self.raw['id']

    @property
    def name(self):
        title = self.raw['title']
        name, ext = os.path.splitext(title)
        if utils.is_docs_file(self.raw) and not ext:
            ext = utils.get_extension(self.raw['exportLinks'])
            title += ext
        return title

    @property
    def size(self):
        # Google docs(Docs,sheets, slides, etc)  don't have file size before they are exported
        return self.raw.get('fileSize')

    @property
    def modified(self):
        return self.raw['modifiedDate']

    @property
    def content_type(self):
        return self.raw['mimeType']

    @property
    def extra(self):
        ret = super().extra
        if utils.is_docs_file(self.raw):
            ret['downloadExt'] = utils.get_download_extension(self.raw['exportLinks'])
        return ret


class GoogleDriveRevision(metadata.BaseFileRevisionMetadata):

    @property
    def version_identifier(self):
        return 'revision'

    @property
    def version(self):
        return self.raw['id']

    @property
    def modified(self):
        return self.raw['modifiedDate']

@@ -363,32 +363,59 @@
 def path(self):%0A
+        #TODO Address this%0A
         return o
@@ -426,16 +426,28 @@
 th.join(
+parse.quote(
 str(self
@@ -453,16 +453,17 @@
 f._path)
+)
 , parse.
@@ -512,36 +512,44 @@
 roperty%0A    def 
-full
+materialized
 _path(self):%0A   
@@ -577,22 +577,8 @@
 oin(
-parse.unquote(
 str(
@@ -588,17 +588,16 @@
 f._path)
-)
 , self.r
@@ -1012,12 +1012,20 @@
 def 
-full
+materialized
 _pat
@@ -1048,32 +1048,45 @@
  return super().
+materialized_
 path + '/'%0A%0A%0Acla

Update metadata to implement materialized_path