# -*- coding: utf-8 -*-
'''
Copyright 2015, 2016 University College London.

This file is part of PyORACC.

PyORACC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PyORACC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PyORACC. If not, see <http://www.gnu.org/licenses/>.
'''


import codecs
from unittest import TestCase, skip
import pytest

from pyoracc.atf.common.atffile import AtfFile
from pyoracc.test.fixtures import belsunu, output_filepath

from pyoracc.atf.common.atflex import AtfLexer
from pyoracc.atf.common.atfyacc import AtfParser
from pyoracc.model.line import Line


def parse(any_str):
    """
    Parse input string, could be just a line or a whole file content.
    """
    parsed = AtfFile(any_str)
    return parsed


def serialize(any_object):
    """
    Serialize input object, from a simple lemma to a whole AtfFile object.
    """
    serialized = any_object.serialize()
    return serialized


def parse_then_serialize(any_str):
    """
    Shorthand for testing serialization.
    """
    return serialize(parse(any_str))


def test_belsunu_serializer():
    """
    Parse belsunu.atf, then serialize, parse again, serialize again,
    compare.
    Comparing serialized output with input file would bring up differences
    that might not be significant (white spaces, newlines, etc).
    The solution is to parse again the serialized file, serialize again,
    then compare the two serializations.
    """
    serialized_1 = parse_then_serialize(belsunu())
    serialized_2 = parse_then_serialize(serialized_1)
    assert serialized_1 == serialized_2


@pytest.mark.xfail
def test_line_word():
    """
    Get a sample word with unicode chars and check serialization is
    correct.
    """
    line = Line("1")
    line.words.append(u"\u2086")
    line_ser = line.serialize()
    assert line_ser == "1.\t" + u"\u2086"


def test_line_words():
    """
    Get a sample line of words with unicode chars and test serialization.
    1. [MU] 1.03-KAM {iti}AB GE₆ U₄ 2-KAM
    """
    atf_file = AtfFile(belsunu())
    uline = atf_file.text.children[0].children[0].children[0]
    uwords = uline.words
    gold = [u'[MU]', u'1.03-KAM', u'{iti}AB', u'GE\u2086', u'U\u2084',
            u'2-KAM']
    assert uwords == gold


def test_line_lemmas():
    """
    Get a sample line of lemmas with unicode chars and test serialization.
    šatti[year]N; n; Ṭebetu[1]MN; mūša[at night]AV; ūm[day]N; n
    """
    atf_file = AtfFile(belsunu())
    uline = atf_file.text.children[0].children[0].children[0]
    ulemmas = uline.lemmas
    gold = [u' \u0161atti[year]N', u'n', u'\u1e6cebetu[1]MN',
            u'm\u016b\u0161a[at night]AV', u'\u016bm[day]N', u'n']
    assert ulemmas == gold


# TODO: Build list of atf files for testing and make a test to go through the
# list of test and try serializing each of them.
@pytest.mark.xfail
def test_text_code_and_description():
    """
    Check if serializing works for the code/description case - first line
    of ATF texts.
    """
    text = "&X001001 = JCS 48, 089\n"
    atf = parse(text)
    serialized = serialize(atf)
    assert serialized.strip()+"\n" == text


@pytest.mark.xfail
def test_text_project():
    """
    Check if serializing works for the project lines.
    """
    text = "#project: cams/gkab\n"
    serialized = parse_then_serialize(text)
    assert serialized.strip()+"\n" == text


@pytest.mark.skip("test_text_language is not implemented yet")
def test_text_language():
    pass


@pytest.mark.skip("test_text_protocols is not implemented yet")
def test_text_protocols():
    pass

@@ -707,58 +707,8 @@
 '%0A%0A%0A
-import codecs%0Afrom unittest import TestCase, skip%0A
 impo
@@ -782,182 +782,65 @@
 acc.
-test.fixtures import belsunu, output_filepath%0A%0Afrom pyoracc.atf.common.atflex import AtfLexer%0Afrom pyoracc.atf.common.atfyacc import AtfParser%0Afrom pyoracc.model.line
+model.line import Line%0Afrom pyoracc.test.fixtures
  import 
 Line
@@ -835,20 +835,23 @@
  import 
-Line
+belsunu
 %0A%0A%0Adef p

Remove unused includes.