# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf import settings
from api.utils.command import list_files_and_folders
from git.repo.base import Repo
from git.repo.fun import is_git_dir
from git.util import join_path
from git.exc import GitCommandError
import os

'''
'''
def get_users_folders():
    fullpath = join_path(settings.GIT_PATH)
    folders = list_files_and_folders(fullpath)

    return folders.get('items', [])

'''
'''
def get_repos_paths():
    users_folders = get_users_folders()
    repos = []

    for folder in users_folders:
        fullpath = join_path(settings.GIT_PATH, folder)
        subfolders = list_files_and_folders(fullpath)

        for subfolder in subfolders.get('items', []):
            fullpath = join_path(settings.GIT_PATH, folder, subfolder)

            if not is_git_dir(fullpath):
                continue

            repos.append(fullpath)

    return repos

'''
'''
def resolve_owner_by_name(name=None):
    if not name:
        return None
    
    paths = [r for r in get_odoo_modules_repos_paths() if r.endswith('/%s.git' % name)]

    if len(paths) == 0:
        return None

    return paths[0].replace('%s/' % settings.GIT_PATH, '').replace('/%s.git' % name, '')


'''
'''
def clone_repo(name=None, to=None, branch='master'):
    return clone_repos([name], to, branch)

'''
'''
def clone_repos(repo_names=[], to=None, branch='master'):
    if len(repo_names) == 0:
        return False

    for user_folder in get_users_folders():
        for repo_name in repo_names:
            repo_path = '%s.git' % join_path(settings.GIT_PATH, user_folder, repo_name)

            if not os.path.exists(repo_path):
                continue
            
            try:
                repo = Repo(repo_path)
                repo.clone(join_path(to, repo_name), branch=branch)
                repo.close()
            except GitCommandError:
                pass

    return True

'''
'''
def pull_repo(name=None, to=None):
    if not name or not to:
        return False

'''
'''
def get_odoo_modules_repos_paths():
    repos_paths = get_repos_paths()
    modules_paths = []

    for repo_path in repos_paths:
        r = Repo(repo_path)

        try:
            if '__openerp__.py' in [o.path for o in r.tree()]:
                modules_paths.append(repo_path)
        except ValueError:
            continue
        finally:
            r.close()

    return modules_paths

'''
'''
def get_odoo_modules_repos_names(include_owners=False):
    repos_paths = get_odoo_modules_repos_paths()
    repos_names = []

    for repos_path in repos_paths:
        repo_with_user = repos_path.replace('%s/' % settings.GIT_PATH, '')
        repo_with_user = repo_with_user.split('/')

        repo_with_user[1] = repo_with_user[1].replace('.git', '')

        if not include_owners:
            repos_names.append(repo_with_user[1])
            continue

        repos_names.append({
            'name': repo_with_user[1],
            'owner': repo_with_user[0]
        })

    return repos_names
