# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
import logging

import maya
import mock

from gcdt_testtools.helpers import logcapture
from gcdt_lookups.lookups import _resolve_lookups, _identify_stacks_recurse, \
    lookup, _find_matching_certificate, _acm_lookup
from gcdt_lookups.credstash_utils import ItemNotFound
from gcdt.gcdt_defaults import CONFIG_READER_CONFIG


def test_identify_stacks_recurse():
    # make sure result is unique
    # sample from data-platform, ingest
    config = {
        'PolicyLambdaDefaultVar' : "lookup:stack:dp-dev-operations-common:PolicyLambdaDefault",
        'PolicyLambdaDefaultVar2': "lookup:stack:dp-dev-operations-common:PolicyLambdaDefault"
    }
    assert _identify_stacks_recurse(config, ['stack']) == \
           set([('dp-dev-operations-common', None)])

    # sample from pnb-ftp, ftpbackend
    config = {
        'VpcId': "lookup:stack:pnb-dev:DefaultVPCId"
    }
    assert _identify_stacks_recurse(config, ['stack']) == set([('pnb-dev', None)])


@mock.patch('gcdt_lookups.lookups.get_outputs_for_stack')
@mock.patch('gcdt_lookups.lookups.stack_exists', return_value=True)
def test_stack_lookup_value(mock_stack_exists, mock_get_outputs_for_stack):
    mock_get_outputs_for_stack.return_value = {
        'EC2BasicsLambdaArn':
            'arn:aws:lambda:eu-west-1:1122233:function:dp-preprod-lambdaEC2Basics-12',
    }
    # sample from data-platform, operations
    context = {
        '_awsclient': 'my_awsclient',
        'tool': 'ramuda'
    }

    config = {
        'LambdaLookupARN': 'lookup:stack:dp-preprod:EC2BasicsLambdaArn'
    }
    _resolve_lookups(context, config, ['stack'])
    mock_get_outputs_for_stack.assert_called_once_with(
        'my_awsclient', 'dp-preprod', None)

    assert config.get('LambdaLookupARN') == \
           'arn:aws:lambda:eu-west-1:1122233:function:dp-preprod-lambdaEC2Basics-12'

@mock.patch('gcdt_lookups.lookups.get_outputs_for_stack')
@mock.patch('gcdt_lookups.lookups.stack_exists', return_value=True)