# -*- coding: utf-8 -*-
from Mapa_PU.swdl.kontroler.database_helper import DatabaseHelper
from Mapa_PU.swdl.model.swdl.natural_values import NaturalValue


class ForestNaturalValuesHelper():
    """Klasa pomocnicza generująca listę walorów przyrodniczych"""
    
    def __init__(self,database_helper:DatabaseHelper):
        self.__database_helper=database_helper
        
    def create_forest_natural_values(self,address_forest:str)->[]:
        try:
            self.__database_helper.open()
            #Normalizacja adresu do obrębu
            if  len(address_forest) > 10:
                address_forest = address_forest[0:10]
            sql= "SELECT F_ENV_PROTECT.COUNTY_CD, F_ENV_PROTECT.DISTRICT_CD, F_ENV_PROTECT.MUNICIPALITY_CD, F_ENV_PROTECT.COMMUNITY_CD, F_ENV_PROTECT.ENV_COUNT, F_ENV_PROTECT.PROTEC_AREA_CD, F_ENV_PROTECT.TOTAL_AREA, F_ENV_PROTECT.PUL_AREA, F_ENV_PROTECT.FOREST_AREA, F_ENV_PROTECT.FOREST_AREA_P, F_ENV_PROTECT.NFOREST_AREA_P, F_ENV_PROTECT.NFOREST_AREA FROM F_ENV_PROTECT INNER JOIN F_COUNTY ON F_ENV_PROTECT.COUNTY_CD = F_COUNTY.COUNTY_CD WHERE((([F_COUNTY].[COUNTY_LTR] & [F_ENV_PROTECT].[DISTRICT_CD] & [F_ENV_PROTECT].[MUNICIPALITY_CD] & [F_ENV_PROTECT].[COMMUNITY_CD]) Like '{}%'));".format(address_forest);
            return [NaturalValue(county,district,municipality,community,env_count,natural_value_cd,total_area,pul_area,forest_area,forest_area_p,nforest_area_p,nforest_area) for county,district,municipality,community,env_count,natural_value_cd,total_area,pul_area,forest_area,forest_area_p,nforest_area_p,nforest_area in self.__database_helper.get_data(sql)]
        except Exception as e:
            raise e
        finally:
            self.__database_helper.close()