import subprocess

from gitgud.structures import Skill
from gitgud.level_builder import LevelBase, RepoLevel

from gitgud.user_messages import simulate_command
from gitgud.user_messages import default_fail_no_reset

from gitgud import operations
from gitgud.operations import ExtraGitAPI

from git import GitConfigParser, Repo, InvalidGitRepositoryError

class Welcome(RepoLevel):
    def post_setup(self):
        self.cat_file("post-setup.txt")

    def status(self):
        self.cat_file("status-1.txt")
        simulate_command("git log")

    def test_failed(self):
        default_fail_no_reset()

    def test(self):
        return True

def get_name_and_email():
    repo = operations.get_operator().repo

    if repo is not None:
        get_config_value = repo.config_reader().get_value
    else:
        get_config_value = GitConfigParser().get_value

    name = get_config_value('user', 'name', default='')
    email = get_config_value('user', 'email', default='')

    return name, email


class Config(LevelBase):
    def setup(self):
        pass

    def post_setup(self):
        self.cat_file("post-setup.txt")

    def status(self):
        self.cat_file("status-1.txt")

        name, email = get_name_and_email()

        print()
        print('user.name: "{}"'.format(name))
        print('user.email: "{}"'.format(email))

        self.cat_file("status-2.txt")
    
    def test(self):
        name, email = get_name_and_email()
        return bool(name and email)

    def test_failed(self):
        default_fail_no_reset()


class Init(LevelBase):
    def setup(self):
        # Make sure we are not in a git repo
        file_operator = operations.get_operator()
        ExtraGitAPI(file_operator.path).destroy_repo()

    def post_setup(self):
        self.cat_file("post-setup.txt")

    def status(self):
        simulate_command("git status")

    def test(self):
        # Check if we are in a git repo
        file_operator = operations.get_operator()
        return file_operator.repo is not None

    def test_failed(self):
        default_fail_no_reset()


import subprocess

from gitgud.structures import Skill
from gitgud.level_builder import LevelBase, RepoLevel

from gitgud.user_messages import simulate_command
from gitgud.user_messages import default_fail_no_reset

from gitgud import operations
from gitgud.operations import ExtraGitAPI

from git import GitConfigParser, Repo, InvalidGitRepositoryError

class Welcome(RepoLevel):
    def post_setup(self):
        self.cat_file("post-setup.txt")

    def status(self):
        self.cat_file("status-1.txt")
        simulate_command("git log")

    def test_failed(self):
        default_fail_no_reset()

    def test(self):
        return True

def get_name_and_email():
    repo = operations.get_operator().repo

    if repo is not None:
        get_config_value = repo.config_reader().get_value
    else:
        get_config_value = GitConfigParser().get_value

    name = get_config_value('user', 'name', default='')
    email = get_config_value('user', 'email', default='')

    return name, email


class Config(LevelBase):
    def setup(self):
        pass

    def post_setup(self):
        self.cat_file("post-setup.txt")

    def status(self):
        self.cat_file("status-1.txt")

        name, email = get_name_and_email()

        print()
        print('user.name: "{}"'.format(name))
        print('user.email: "{}"'.format(email))

        self.cat_file("status-2.txt")
    
    def test(self):
        name, email = get_name_and_email()
        return bool(name and email)

    def test_failed(self):
        default_fail_no_reset()


class Init(LevelBase):
    def setup(self):
        # Make sure we are not in a git repo
        file_operator = operations.get_operator()
        ExtraGitAPI(file_operator.path).destroy_repo()

    def post_setup(self):
        self.cat_file("post-setup.txt")

    def status(self):
        simulate_command("git status")

    def test(self):
        # Check if we are in a git repo
        file_operator = operations.get_operator()
        return file_operator.repo is not None

    def test_failed(self):
        default_fail_no_reset()


# Make sure we are not in a git repo # Check if we are in a git repo