from cleo.testers import CommandTester

from tests.helpers import get_package


def test_show_basic_with_installed_packages(app, poetry, installed):
    command = app.find('show')
    tester = CommandTester(command)

    cachy_010 = get_package('cachy', '0.1.0')
    cachy_010.description = 'Cachy package'

    pendulum_200 = get_package('pendulum', '2.0.0')
    pendulum_200.description = 'Pendulum package'

    installed.add_package(cachy_010)
    installed.add_package(pendulum_200)
    poetry.locker.mock_lock_data({
        'package': [{
            'name': 'cachy',
            'version': '0.1.0',
            'description': 'Cachy package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }, {
            'name': 'pendulum',
            'version': '2.0.0',
            'description': 'Pendulum package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }],
        'metadata': {
            'python-versions': '*',
            'platform': '*',
            'content-hash': '123456789',
            'hashes': {
                'cachy': [],
                'pendulum': [],
            }
        }
    })

    tester.execute([
        ('command', command.get_name()),
    ])

    expected = """\
cachy    0.1.0 Cachy package
pendulum 2.0.0 Pendulum package
"""

    assert tester.get_display() == expected


def test_show_basic_with_not_installed_packages_non_decorated(app, poetry, installed):
    command = app.find('show')
    tester = CommandTester(command)

    cachy_010 = get_package('cachy', '0.1.0')
    cachy_010.description = 'Cachy package'

    pendulum_200 = get_package('pendulum', '2.0.0')
    pendulum_200.description = 'Pendulum package'

    installed.add_package(cachy_010)
    poetry.locker.mock_lock_data({
        'package': [{
            'name': 'cachy',
            'version': '0.1.0',
            'description': 'Cachy package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }, {
            'name': 'pendulum',
            'version': '2.0.0',
            'description': 'Pendulum package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }],
        'metadata': {
            'python-versions': '*',
            'platform': '*',
            'content-hash': '123456789',
            'hashes': {
                'cachy': [],
                'pendulum': [],
            }
        }
    })

    tester.execute([
        ('command', command.get_name()),
    ])

    expected = """\
cachy        0.1.0 Cachy package
pendulum (!) 2.0.0 Pendulum package
"""

    assert tester.get_display() == expected


def test_show_basic_with_not_installed_packages_decorated(app, poetry, installed):
    command = app.find('show')
    tester = CommandTester(command)

    cachy_010 = get_package('cachy', '0.1.0')
    cachy_010.description = 'Cachy package'

    pendulum_200 = get_package('pendulum', '2.0.0')
    pendulum_200.description = 'Pendulum package'

    installed.add_package(cachy_010)
    poetry.locker.mock_lock_data({
        'package': [{
            'name': 'cachy',
            'version': '0.1.0',
            'description': 'Cachy package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }, {
            'name': 'pendulum',
            'version': '2.0.0',
            'description': 'Pendulum package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }],
        'metadata': {
            'python-versions': '*',
            'platform': '*',
            'content-hash': '123456789',
            'hashes': {
                'cachy': [],
                'pendulum': [],
            }
        }
    })

    tester.execute([
        ('command', command.get_name()),
    ], {'decorated': True})

    expected = """\
\033[32mcachy   \033[0m 0.1.0 Cachy package
\033[31mpendulum\033[0m 2.0.0 Pendulum package
"""

    assert tester.get_display() == expected


def test_show_latest_non_decorated(app, poetry, installed, repo):
    command = app.find('show')
    tester = CommandTester(command)

    cachy_010 = get_package('cachy', '0.1.0')
    cachy_010.description = 'Cachy package'
    cachy_020 = get_package('cachy', '0.2.0')
    cachy_020.description = 'Cachy package'

    pendulum_200 = get_package('pendulum', '2.0.0')
    pendulum_200.description = 'Pendulum package'
    pendulum_201 = get_package('pendulum', '2.0.1')
    pendulum_201.description = 'Pendulum package'

    installed.add_package(cachy_010)
    installed.add_package(pendulum_200)

    repo.add_package(cachy_010)
    repo.add_package(cachy_020)
    repo.add_package(pendulum_200)
    repo.add_package(pendulum_201)

    poetry.locker.mock_lock_data({
        'package': [{
            'name': 'cachy',
            'version': '0.1.0',
            'description': 'Cachy package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }, {
            'name': 'pendulum',
            'version': '2.0.0',
            'description': 'Pendulum package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }],
        'metadata': {
            'python-versions': '*',
            'platform': '*',
            'content-hash': '123456789',
            'hashes': {
                'cachy': [],
                'pendulum': [],
            }
        }
    })

    tester.execute([
        ('command', command.get_name()),
        ('--latest', True),
    ])

    expected = """\
cachy    0.1.0 0.2.0 Cachy package
pendulum 2.0.0 2.0.1 Pendulum package
"""

    assert tester.get_display() == expected


def test_show_latest_decorated(app, poetry, installed, repo):
    command = app.find('show')
    tester = CommandTester(command)

    cachy_010 = get_package('cachy', '0.1.0')
    cachy_010.description = 'Cachy package'
    cachy_020 = get_package('cachy', '0.2.0')
    cachy_020.description = 'Cachy package'

    pendulum_200 = get_package('pendulum', '2.0.0')
    pendulum_200.description = 'Pendulum package'
    pendulum_201 = get_package('pendulum', '2.0.1')
    pendulum_201.description = 'Pendulum package'

    installed.add_package(cachy_010)
    installed.add_package(pendulum_200)

    repo.add_package(cachy_010)
    repo.add_package(cachy_020)
    repo.add_package(pendulum_200)
    repo.add_package(pendulum_201)

    poetry.locker.mock_lock_data({
        'package': [{
            'name': 'cachy',
            'version': '0.1.0',
            'description': 'Cachy package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }, {
            'name': 'pendulum',
            'version': '2.0.0',
            'description': 'Pendulum package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }],
        'metadata': {
            'python-versions': '*',
            'platform': '*',
            'content-hash': '123456789',
            'hashes': {
                'cachy': [],
                'pendulum': [],
            }
        }
    })

    tester.execute([
        ('command', command.get_name()),
        ('--latest', True),
    ], {
        'decorated': True
    })

    expected = """\
\033[32mcachy   \033[0m 0.1.0 \033[33m0.2.0\033[0m Cachy package
\033[32mpendulum\033[0m 2.0.0 \033[31m2.0.1\033[0m Pendulum package
"""

    assert tester.get_display() == expected


def test_show_outdated(app, poetry, installed, repo):
    command = app.find('show')
    tester = CommandTester(command)

    cachy_010 = get_package('cachy', '0.1.0')
    cachy_010.description = 'Cachy package'
    cachy_020 = get_package('cachy', '0.2.0')
    cachy_020.description = 'Cachy package'

    pendulum_200 = get_package('pendulum', '2.0.0')
    pendulum_200.description = 'Pendulum package'

    installed.add_package(cachy_010)
    installed.add_package(pendulum_200)

    repo.add_package(cachy_010)
    repo.add_package(cachy_020)
    repo.add_package(pendulum_200)

    poetry.locker.mock_lock_data({
        'package': [{
            'name': 'cachy',
            'version': '0.1.0',
            'description': 'Cachy package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }, {
            'name': 'pendulum',
            'version': '2.0.0',
            'description': 'Pendulum package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }],
        'metadata': {
            'python-versions': '*',
            'platform': '*',
            'content-hash': '123456789',
            'hashes': {
                'cachy': [],
                'pendulum': [],
            }
        }
    })

    tester.execute([
        ('command', command.get_name()),
        ('--outdated', True),
    ])

    expected = """\
cachy    0.1.0 0.2.0 Cachy package
"""

    assert tester.get_display() == expected


def test_show_hides_incompatible_package(app, poetry, installed, repo):
    command = app.find('show')
    tester = CommandTester(command)

    cachy_010 = get_package('cachy', '0.1.0')
    cachy_010.description = 'Cachy package'

    pendulum_200 = get_package('pendulum', '2.0.0')
    pendulum_200.description = 'Pendulum package'

    installed.add_package(pendulum_200)

    poetry.locker.mock_lock_data({
        'package': [{
            'name': 'cachy',
            'version': '0.1.0',
            'description': 'Cachy package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': [],
            'requirements': {
                'python': '1.0'
            }
        }, {
            'name': 'pendulum',
            'version': '2.0.0',
            'description': 'Pendulum package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }],
        'metadata': {
            'python-versions': '*',
            'platform': '*',
            'content-hash': '123456789',
            'hashes': {
                'cachy': [],
                'pendulum': [],
            }
        }
    })

    tester.execute([
        ('command', command.get_name()),
    ])

    expected = """\
pendulum 2.0.0 Pendulum package
"""

    assert tester.get_display() == expected


def test_show_all_shows_incompatible_package(app, poetry, installed, repo):
    command = app.find('show')
    tester = CommandTester(command)

    cachy_010 = get_package('cachy', '0.1.0')
    cachy_010.description = 'Cachy package'

    pendulum_200 = get_package('pendulum', '2.0.0')
    pendulum_200.description = 'Pendulum package'

    installed.add_package(pendulum_200)

    poetry.locker.mock_lock_data({
        'package': [{
            'name': 'cachy',
            'version': '0.1.0',
            'description': 'Cachy package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': [],
            'requirements': {
                'python': '1.0'
            }
        }, {
            'name': 'pendulum',
            'version': '2.0.0',
            'description': 'Pendulum package',
            'category': 'main',
            'optional': False,
            'platform': '*',
            'python-versions': '*',
            'checksum': []
        }],
        'metadata': {
            'python-versions': '*',
            'platform': '*',
            'content-hash': '123456789',
            'hashes': {
                'cachy': [],
                'pendulum': [],
            }
        }
    })

    tester.execute([
        ('command', command.get_name()),
        ('--all', True),
    ])

    expected = """\
cachy     0.1.0 Cachy package
pendulum  2.0.0 Pendulum package
"""

    assert tester.get_display() == expected
