import gc
import logging

import pytest
import spacy

from zshot import PipelineConfig
from zshot.linker.linker_regen.linker_regen import LinkerRegen
from zshot.linker.linker_regen.trie import Trie
from zshot.linker.linker_regen.utils import load_wikipedia_trie, load_dbpedia_trie, create_input
from zshot.tests.config import EX_DOCS, EX_ENTITIES
from zshot.tests.mentions_extractor.test_mention_extractor import DummyMentionsExtractor

logger = logging.getLogger(__name__)


@pytest.fixture(scope="module", autouse=True)
def teardown():
    logger.warning("Starting regen tests")
    yield True
    gc.collect()


def test_regen_linker():
    nlp = spacy.blank("en")
    config = PipelineConfig(
        mentions_extractor=DummyMentionsExtractor(),
        linker=LinkerRegen(),
        entities=EX_ENTITIES
    )
    nlp.add_pipe("zshot", config=config, last=True)
    assert "zshot" in nlp.pipe_names

    doc = nlp(EX_DOCS[1])
    assert len(doc.ents) > 0
    doc = nlp("")
    assert len(doc.ents) == 0
    docs = [doc for doc in nlp.pipe(EX_DOCS)]
    assert all(len(doc.ents) > 0 for doc in docs)
    del nlp.get_pipe('zshot').mentions_extractor, nlp.get_pipe('zshot').entities, nlp.get_pipe('zshot').nlp
    del nlp.get_pipe('zshot').linker.tokenizer, nlp.get_pipe('zshot').linker.trie, \
        nlp.get_pipe('zshot').linker.model, nlp.get_pipe('zshot').linker
    nlp.remove_pipe('zshot')
    del doc, nlp, config


def test_regen_linker_wikification():
    nlp = spacy.blank("en")
    trie = Trie()
    trie.add([794, 536, 1])
    trie.add([794, 357, 1])
    config = PipelineConfig(
        mentions_extractor=DummyMentionsExtractor(),
        linker=LinkerRegen(trie=trie),
    )
    nlp.add_pipe("zshot", config=config, last=True)
    assert "zshot" in nlp.pipe_names

    doc = nlp(EX_DOCS[1])
    assert len(doc.ents) > 0
    del nlp.get_pipe('zshot').mentions_extractor, nlp.get_pipe('zshot').entities, nlp.get_pipe('zshot').nlp
    del nlp.get_pipe('zshot').linker.tokenizer, nlp.get_pipe('zshot').linker.trie, \
        nlp.get_pipe('zshot').linker.model, nlp.get_pipe('zshot').linker
    nlp.remove_pipe('zshot')
    del doc, nlp, config


@pytest.mark.skip(reason="Too expensive to run on every commit")
def test_load_wikipedia_trie():  # pragma: no cover
    trie = load_wikipedia_trie()
    assert len(list(trie.trie_dict.keys())) == 6952


@pytest.mark.skip(reason="Too expensive to run on every commit")
def test_load_dbpedia_trie():  # pragma: no cover
    trie = load_dbpedia_trie()
    assert len(list(trie.trie_dict.keys())) == 7156


def test_create_input():
    start_delimiter = "[START]"
    end_delimiter = "[END]"
    max_length = 10

    times_rep = 6
    sentence = "[START]" + " test" * times_rep + " [END]"
    input_sentence = create_input(sentence, max_length, start_delimiter, end_delimiter)
    assert input_sentence == sentence
    times_rep = 12
    sentence = "[START]" + " test" * times_rep + " [END]"
    input_sentence = create_input(sentence, max_length, start_delimiter, end_delimiter)
    assert input_sentence == " ".join(["test" for i in range(9)])

    text = f"IBM headquarters are located in {start_delimiter} New York {end_delimiter} ."
    input_ = create_input(text, max_length=4, start_delimiter=start_delimiter, end_delimiter=end_delimiter)
    assert start_delimiter in input_ and end_delimiter in input_
