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

import markupsafe

from django.db import models

from addons.base import exceptions
from addons.base.models import (BaseOAuthNodeSettings, BaseOAuthUserSettings,
                                BaseStorageAddon)

from addons.bitbucket.api import BitbucketClient
from addons.bitbucket.serializer import BitbucketSerializer
from addons.bitbucket import settings as bitbucket_settings
from addons.bitbucket.exceptions import NotFoundError
from framework.auth import Auth
from osf.models.external import ExternalProvider
from osf.models.files import File, Folder, BaseFileNode
from website import settings
from website.util import web_url_for

hook_domain = bitbucket_settings.HOOK_DOMAIN or settings.DOMAIN


class BitbucketFileNode(BaseFileNode):
    _provider = 'bitbucket'


class BitbucketFolder(BitbucketFileNode, Folder):
    pass


class BitbucketFile(BitbucketFileNode, File):
    version_identifier = 'commitSha'

    def touch(self, auth_header, revision=None, commitSha=None, branch=None, **kwargs):
        revision = revision or commitSha or branch
        return super(BitbucketFile, self).touch(auth_header, revision=revision, **kwargs)

    @property
    def _hashes(self):
        try:
            return {'commit': self._history[-1]['extra']['commitSha']}
        except (IndexError, KeyError):
            return None

class BitbucketProvider(ExternalProvider):