import pytest

from tests.utils import async

import io
import os
import json
import base64
import hashlib

import aiohttpretty

from waterbutler.core import streams
from waterbutler.core import exceptions
from waterbutler.core.path import WaterButlerPath
from waterbutler.core.provider import build_url

from waterbutler.providers.github import GitHubProvider
from waterbutler.providers.github import settings as github_settings
from waterbutler.providers.github.metadata import GitHubRevision
from waterbutler.providers.github.metadata import GitHubFileTreeMetadata
from waterbutler.providers.github.metadata import GitHubFolderTreeMetadata
from waterbutler.providers.github.metadata import GitHubFileContentMetadata
from waterbutler.providers.github.metadata import GitHubFolderContentMetadata


@pytest.fixture
def auth():
    return {
        'name': 'cat',
        'email': 'cat@cat.com',
    }


@pytest.fixture
def credentials():
    return {'token': 'naps'}


@pytest.fixture
def settings():
    return {
        'owner': 'cat',
        'repo': 'food',
    }


@pytest.fixture
def file_content():
    return b'hungry'


@pytest.fixture
def file_like(file_content):
    return io.BytesIO(file_content)


@pytest.fixture
def file_stream(file_like):
    return streams.FileStreamReader(file_like)


@pytest.fixture
def upload_response():
    return {
        "content": {
            "name": "hello.txt",
            "path": "notes/hello.txt",
            "sha": "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
            "size": 9,
            "url": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
            "html_url": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt",
            "git_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
            "type": "file",
            "_links": {
                "self": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
                "git": "https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
                "html": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt"
            }
        },
        "commit": {
            "sha": "7638417db6d59f3c431d3e1f261cc637155684cd",
            "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd",
            "html_url": "https://github.com/octocat/Hello-World/git/commit/7638417db6d59f3c431d3e1f261cc637155684cd",
            "author": {
                "date": "2010-04-10T14:10:01-07:00",
                "name": "Scott Chacon",
                "email": "schacon@gmail.com"
            },
            "committer": {
                "date": "2010-04-10T14:10:01-07:00",
                "name": "Scott Chacon",
                "email": "schacon@gmail.com"
            },
            "message": "my commit message",
            "tree": {
                "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
                "sha": "691272480426f78a0138979dd3ce63b77f706feb"
            },
            "parents": [
                {
                    "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/1acc419d4d6a9ce985db7be48c6349a0475975b5",
                    "html_url": "https://github.com/octocat/Hello-World/git/commit/1acc419d4d6a9ce985db7be48c6349a0475975b5",
                    "sha": "1acc419d4d6a9ce985db7be48c6349a0475975b5"
                }
            ]
        }
    }


@pytest.fixture
def create_folder_response():
    return {
        "content": {
            "name": ".gitkeep",
            "path": "i/like/trains/.gitkeep",
            "sha": "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
            "size": 9,
            "url": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
            "html_url": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt",
            "git_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
            "type": "file",
            "_links": {
                "self": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
                "git": "https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
                "html": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt"
            }
        },
        "commit": {
            "sha": "7638417db6d59f3c431d3e1f261cc637155684cd",
            "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd",
            "html_url": "https://github.com/octocat/Hello-World/git/commit/7638417db6d59f3c431d3e1f261cc637155684cd",
            "author": {
                "date": "2010-04-10T14:10:01-07:00",
                "name": "Scott Chacon",
                "email": "schacon@gmail.com"
            },
            "committer": {
                "date": "2010-04-10T14:10:01-07:00",
                "name": "Scott Chacon",
                "email": "schacon@gmail.com"
            },
            "message": "my commit message",
            "tree": {
                "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
                "sha": "691272480426f78a0138979dd3ce63b77f706feb"
            },
            "parents": [
                {
                    "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/1acc419d4d6a9ce985db7be48c6349a0475975b5",
                    "html_url": "https://github.com/octocat/Hello-World/git/commit/1acc419d4d6a9ce985db7be48c6349a0475975b5",
                    "sha": "1acc419d4d6a9ce985db7be48c6349a0475975b5"
                }
            ]
        }
    }


@pytest.fixture
def repo_metadata():
    return {
        'full_name': 'octocat/Hello-World',
        'permissions': {
            'push': False,
            'admin': False,
            'pull': True
        },
        'has_downloads': True,
        'notifications_url': 'https://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}',
        'releases_url': 'https://api.github.com/repos/octocat/Hello-World/releases{/id}',
        'downloads_url': 'https://api.github.com/repos/octocat/Hello-World/downloads',
        'merges_url': 'https://api.github.com/repos/octocat/Hello-World/merges',
        'owner': {
            'avatar_url': 'https://avatars.githubusercontent.com/u/583231?v=3',
            'organizations_url': 'https://api.github.com/users/octocat/orgs',
            'type': 'User',
            'starred_url': 'https://api.github.com/users/octocat/starred{/owner}{/repo}',
            'url': 'https://api.github.com/users/octocat',
            'html_url': 'https://github.com/octocat',
            'received_events_url': 'https://api.github.com/users/octocat/received_events',
            'subscriptions_url': 'https://api.github.com/users/octocat/subscriptions',
            'site_admin': False,
            'gravatar_id': '',
            'repos_url': 'https://api.github.com/users/octocat/repos',
            'gists_url': 'https://api.github.com/users/octocat/gists{/gist_id}',
            'id': 583231,
            'events_url': 'https://api.github.com/users/octocat/events{/privacy}',
            'login': 'octocat',
            'following_url': 'https://api.github.com/users/octocat/following{/other_user}',
            'followers_url': 'https://api.github.com/users/octocat/followers'
        },
        'html_url': 'https://github.com/octocat/Hello-World',