from django.conf import settings
from elasticsearch import TransportError

from pillowtop.checkpoints.manager import PillowCheckpoint
from pillowtop.es_utils import ElasticsearchIndexInfo
from pillowtop.pillow.interface import ConstructedPillow

TEST_ES_META = {
    "settings": {
        "analysis": {
            "analyzer": {
                "default": {
                    "type": "custom",
                    "tokenizer": "whitespace",
                    "filter": ["lowercase"]
                },
            }
        }
    }
}

TEST_ES_MAPPING = {
    '_meta': {
        'comment': 'You know, for tests',
        'created': '2015-10-07 @czue'
    },
    "properties": {
        "doc_type": {
            "index": "not_analyzed", "type": "string"
        },
    }
}

TEST_ES_TYPE = 'test_doc'
TEST_ES_INDEX = 'test_pillowtop_index'
TEST_ES_ALIAS = 'pillowtop_tests'
TEST_INDEX_INFO = ElasticsearchIndexInfo(
    index=TEST_ES_INDEX,
    alias=TEST_ES_ALIAS,
    type=TEST_ES_TYPE,
    meta=TEST_ES_META,
    mapping=TEST_ES_MAPPING
)


def get_doc_count(es, index, refresh_first=True):
    if refresh_first:
        # we default to calling refresh since ES might have stale data
        es.indices.refresh(index)
    stats = es.indices.stats(index)
    return stats['indices'][index]['total']['docs']['count']


def get_index_mapping(es, index, doc_type):
    def _format_mapping_for_es_version(mapping):
        if settings.ELASTICSEARCH_VERSION < 1.0:
            return mapping[doc_type]
        else:
            return mapping[index]['mappings'][doc_type]
    try:
        return _format_mapping_for_es_version(es.indices.get_mapping(index, doc_type))
    except TransportError:
        return {}


class FakeConstructedPillow(ConstructedPillow):
    pass


def make_fake_constructed_pillow(pillow_id, checkpoint_id):
    from pillowtop.feed.mock import RandomChangeFeed
    from pillowtop.processors import LoggingProcessor

    pillow = FakeConstructedPillow(
        name=pillow_id,
        checkpoint=PillowCheckpoint(checkpoint_id),
        change_feed=RandomChangeFeed(10),
        processor=LoggingProcessor(),
    )
    return pillow

@@ -243,300 +243,8 @@
 ow%0A%0A
-TEST_ES_META = %7B%0A    %22settings%22: %7B%0A        %22analysis%22: %7B%0A            %22analyzer%22: %7B%0A                %22default%22: %7B%0A                    %22type%22: %22custom%22,%0A                    %22tokenizer%22: %22whitespace%22,%0A                    %22filter%22: %5B%22lowercase%22%5D%0A                %7D,%0A            %7D%0A        %7D%0A    %7D%0A%7D%0A
 %0ATES
@@ -696,31 +696,8 @@
 PE,%0A
-    meta=TEST_ES_META,%0A
     

remove test_es_meta