"""Modół raportu"""

from Mapa_PU.model.wynik_kontroli import Result, WynikKontroli
import datetime
import os

class Report():
    """Raport"""
    
    def __init__(self,file_path,file_name,heading:str):
        self.__datetime ='{}.txt'.format(datetime.date.today()) #str(datetime.datetime.now())[:-7].replace(':','').replace(' ','_') +".txt"
        self.__file = file_name + self.__datetime
        #self.file_path = os.path.join( os.path.join(konfiguracja.sciezka,'Warstwy'), self.__file )
        self.file_path=os.path.join(file_path,self.__file)
        self.file_header(heading)
        self.__is_error=False
        self.__results=list()
    
    def file_header(self,heading:str):
        """Nagłówek raportu"""
        
        #Nagłowek w pliku
        self.report =  u'|============================================================|\n'
        self.report += u'|' + heading + '|\n'
        self.report += u'|============================================================|\n'
        self.report += u'\n\n'
    
    def control_header(self,title,layer_name=None):
        """Nagłówek kontroli"""
        self.report += '|============================================================|\n'
        self.report += '|' + (u'   {}'.format(title)).ljust(60) + '|\n'
        if layer_name is not None:
            self.report += '|' + (u'   dla warstwy :' + layer_name).ljust(60) + '|\n'
        self.report += '|============================================================|\n'
        
    def end_body(self):
        """Zakonczenie bloku raportu"""
        self.report += '|============================================================|\n\n'
    
    def body(self,errors:[]):
        """Błędy raportu"""
        for error in errors:
            if error.message != '':
                self.report += '|' + (error.type_str+ u' - '+error.message).center(60) + '|\n'
            for e in error.errors:
                self.report += '|' + (e).center(60) + '|\n'
            #self.report += '|' + ''.center(60) + '|\n'
            
    @property
    def is_error(self):
        """Wskaznik czy raport zawiera błędy"""
        return self.__is_error
    
    @is_error.getter
    def is_error(self)->bool:
        """Wskaznik czy raport zawiera błędy"""
        return self.__is_error
    
    @is_error.setter
    def is_error(self,value)->bool:
        """Wskaznik czy raport zawiera błędy"""
        self.__is_error=value
        
    @property
    def results(self):
        """Wyniki orginalne kontroli"""
        return self.__results
    
    def add_result(self,results:list):
        """Dodanie wyniku do listy
        :params results:Wynik kontroli
        """
        try:
            self.__results=self.__results+results
        except Exception as e:
            raise e