# -*- coding: utf-8 -*-
"""
Copyright (c) 2016 Biuro Urządzania Lasu i Geodezji Leśnej

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
OTHER DEALINGS IN THE SOFTWARE.
"""
from Mapa_PU.openpyxl import load_workbook

#from Mapa_PU.openpyxl.reader.excel import load_workbook

import datetime, shutil

class ExcelError:
    """Komunikat błędu excel"""
    
    def __init__(self,group:str,error:str,type=''):
        self.__error=error
        self.__group=group
        self.__type=type
        
    @property
    def group(self):
        """Grupa kontroli"""
        return self.__group

    @property
    def error(self):
        """Opis błedu"""
        return self.__error

    @property
    def type(self):
        """Rodzaj kontroli"""
        return self.__type
        # if hasattr(self.__control,'dodatkowa')==False:
        #     return ''
        # if self.__control.dodatkowa:
        #     return 'POMOCNICZA'
        # else:
        #     return 'OBLIGATORYJNA'

class Excel:
    """Obsługa pliku excell"""
        
    
    def save_to_excell(self,errors:[],root:str,filename:str,heading:()):
        """Zapis błędów w  pliku excel
        :params path:Ścierzka do katalogu projektu
        :params root:Ścierzka do katalogu aplikacji
        :params filename:Nazwa pliku wynikowego
        :params heading:Wersja MapaPU
        """
        try:
            
#             try:
#                 from openpyxl import load_workbook
#             except:
#                 import pip
#                 pip.main(['install','openpyxl'])
#                 from openpyxl import load_workbook
                                    
            #Skopiownie pliku szablonu do katalogu kontrole
            #filename='{}\Kontrole\{}_{}.xlsx'.format(path,name,datetime.date.today())
            template='{}\szablony\wyniki_kontroli.xlsx'.format(root)
            shutil.copy2(template,filename)
            
            workbook = load_workbook(filename=filename)
            workbook.sheetnames
            sheet = workbook.active
            sheet['A1']=heading[0]
            sheet['A2']=heading[1]
            sheet['A3']=heading[2]
            idx_row=len(list(sheet.rows))
            for error in errors:
                #for e in error.error:
                idx_row+=1
                sheet['A{}'.format(idx_row)]=error.group
                sheet['B{}'.format(idx_row)]=error.error
                sheet['C{}'.format(idx_row)]=error.type
            
            workbook.save(filename=filename)
            return filename
        except Exception as e:
            raise e
    
    
