from common_fixtures import *  # NOQA
from cattle import ApiError


def _create_stack(client):
    env = client.create_stack(name=random_str())
    env = client.wait_success(env)
    assert env.state == "active"
    return env


def test_service_add_remove_service_link(client, context):
    env = _create_stack(client)

    image_uuid = context.image_uuid
    launch_config = {"imageUuid": image_uuid}

    service1 = client.create_service(name=random_str(),
                                     stackId=env.id,
                                     launchConfig=launch_config)
    service1 = client.wait_success(service1)

    service2 = client.create_service(name=random_str(),
                                     stackId=env.id,
                                     launchConfig=launch_config)
    service2 = client.wait_success(service2)

    # link service2 to service1
    service_link = {"serviceId": service2.id}
    service1 = service1.addservicelink(serviceLink=service_link)
    _validate_add_service_link(service1, service2, client)

    # remove service link
    service1 = service1.removeservicelink(serviceLink=service_link)
    _validate_remove_service_link(service1, service2, client)

    # validate adding link with the name
    service_link = {"serviceId": service2.id, "name": 'myLink'}
    service1 = service1.addservicelink(serviceLink=service_link)
    service_maps = client. \
        list_serviceConsumeMap(serviceId=service1.id,
                               consumedServiceId=service2.id, name='myLink')
    assert len(service_maps) == 1


def test_links_after_service_remove(client, context):
    env = _create_stack(client)

    image_uuid = context.image_uuid
    launch_config = {"imageUuid": image_uuid}
    service1 = client.create_service(name=random_str(),
                                     stackId=env.id,
                                     launchConfig=launch_config)
    service1 = client.wait_success(service1)

    service2 = client.create_service(name=random_str(),
                                     stackId=env.id,
                                     launchConfig=launch_config)
    service2 = client.wait_success(service2)

    # link servic2 to service1
    service_link = {"serviceId": service2.id}
    service1 = service1.addservicelink(serviceLink=service_link)
    _validate_add_service_link(service1, service2, client)

    # link service1 to service2
    service_link = {"serviceId": service1.id}
    service2 = service2.addservicelink(serviceLink=service_link)
    _validate_add_service_link(service2, service1, client)

    # remove service1
    service1 = client.wait_success(service1.remove())

    _validate_remove_service_link(service1, service2, client)

    _validate_remove_service_link(service2, service1, client)


def test_link_services_from_diff_env(client, context):
    env1 = _create_stack(client)

    image_uuid = context.image_uuid
    launch_config = {"imageUuid": image_uuid}

    service1 = client.create_service(name=random_str(),
                                     stackId=env1.id,
                                     launchConfig=launch_config)
    service1 = client.wait_success(service1)

    env2 = _create_stack(client)
    service2 = client.create_service(name=random_str(),
                                     stackId=env2.id,
                                     launchConfig=launch_config)
    service2 = client.wait_success(service2)

    # try to link - should work
    service_link = {"serviceId": service2.id}
    service1.addservicelink(serviceLink=service_link)
    _validate_add_service_link(service1, service2, client)


def test_set_service_links(client, context):
    env1 = _create_stack(client)

    image_uuid = context.image_uuid
    launch_config = {"imageUuid": image_uuid}

    service1 = client.create_service(name=random_str(),
                                     stackId=env1.id,
                                     launchConfig=launch_config)
    service1 = client.wait_success(service1)

    service2 = client.create_service(name=random_str(),
                                     stackId=env1.id,
                                     launchConfig=launch_config)
    service2 = client.wait_success(service2)

    service3 = client.create_service(name=random_str(),
                                     stackId=env1.id,
                                     launchConfig=launch_config)
    service3 = client.wait_success(service3)

    # set service2, service3 links for service1
    service_link1 = {"serviceId": service2.id, "name": "link1"}
    service_link2 = {"serviceId": service3.id, "name": "link2"}