#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import corpus2
import sys


class SumoProcessor():
    """
    klasa bazowa dla ccl processora i static processora
    posiada podstawowe metody do odczytywania pliku, zapisywania pliku
    i przetwarzania ccl
    """
    def __init__(self):
        pass

    def __read_file(self, file_path):
        tagset = corpus2.get_named_tagset(self.tagset)
        doc_reader = corpus2.CclRelReader(tagset, str(file_path),
                                          str(file_path))
        doc_reader.set_option("disamb_only")
        document = doc_reader.read()
        del doc_reader
        return document

    def __read_ccl_stream(self):
        """
        IO method for ccl stream reading. It returns corpus2.Document

            @rtype: corpus2.Document
            """
        cr2_tagset = corpus2.get_named_tagset(self.tagset)
        doc_reader = corpus2.TokenReader_create_stdin_reader('ccl', cr2_tagset)
        document = corpus2.Document()
        while True:
            chunk = doc_reader.get_next_chunk()
            if not chunk:
                break
            document.add_paragraph(chunk.clone_shared())
        del doc_reader
        return document

    def __write_ccl_file(self, document, document_path):
        tagset = corpus2.get_named_tagset(self.tagset)
        writer = corpus2.TokenWriter_create_path_writer('ccl', document_path,
                                                        tagset)
        for parag in document.paragraphs():
            writer.write_chunk(parag)
        writer.finish()
        del writer

    def __print_ccl_file(self, document):
        tagset = corpus2.get_named_tagset(self.tagset)
        printer = corpus2.TokenWriter_create_stdout_writer('ccl', tagset)
        for parag in document.paragraphs():
            printer.write_chunk(parag)
        printer.finish()
        del printer

    def process_document(self):
            pass

    def process_ccl(self, input_path, output_path=None):
        if input_path == "-":
            document = self.__read_ccl_stream()
        else:
            document = self.__read_file(input_path)
        processed_doc = self.process_document(document)
        if output_path:
            self.__write_ccl_file(processed_doc, output_path)
        else:
            self.__print_ccl_file(processed_doc)