from B01Common import CommonSql
from E01CommonVolumes import CommonVolumes
from A08AddPR import AddColumnPR
import datetime
import os

class FillVolumeFeature(CommonSql):
    def __init__(self,pgconn):
        CommonSql.__init__(self,pgconn)

    def Create_volume_layer(self, name, buffer, code_zone_reason_g,code_uspace_class_type_g):
        cm = CommonVolumes(self._pgconn)
            #LayerId = cm.getUuID()

        #we need all the information of the layer evaluated(table cartography.layers) and insert it on table layers
            #to know the geometry codification code we have to know the priority type of geometry (do a function)
      
        layer_name_geom=cm.get_layer(name)
        
        layer_vol_id=cm.create_layer(layer_name_geom[2],layer_name_geom[1])
        
        #then we need the features (from view cartography.(viewlayer))
            #now we have the name of the view so we select the features information from this view
            #layer_name_geom[0]-> layer id
            #layer_name_geom[1]-> type of geometry
            #layer_name_geom[2]-> eurocae layer name
            #layer_name_geom[3]-> new layer id
        type_of_geometry=layer_name_geom[1]

        if type_of_geometry== 2:
            geometries_type=[2]
        else:
            if buffer==0:
                geometries_type=[3]
            else:
                geometries_type=[1,3]

        for type_geom in geometries_type:
            
            features= cm.get_features(layer_name_geom[0],type_geom)
                #features[0][0]-> geometry
                #features[0][1]-> name of feature
                #features[0][2]-> id
            for f in features:
                message='None'
                creationdate_metadata=datetime.datetime(2020,1,1)
                lastupdate_metadata=datetime.datetime(2020,1,1)
                author='OSM'
                country='ESP'
                code_zone_type=1
                code_restriction=[1]
                code_uspace= [code_uspace_class_type_g]
                code_zone_reason=[code_zone_reason_g]
                prohibition='FALSE'
                region=1
                authority_id=cm.authority(name='Enaire',service='',contact_name='',site_url='',email=' info@enaire.es',phone='913210211')
                notification_id=cm.authority(name='Enaire',service='',contact_name='',site_url='',email=' info@enaire.es',phone='913210211')
                period_type_id=1
                time_period_time_period_id=[]
                metadata_metadata_id=cm.specification_metadata(creationdate_metadata,lastupdate_metadata,author)
                volume_limitation_def=[7]
                condition_expression=[]
                upperlimitnorestriccion=0
                identifier=""
                intervalbefore=""
                
            
                v=cm.insertIntoVolumes(f[1],0,150,f[0],buffer,layer_vol_id, f[2],message, author, country, code_zone_type, code_restriction,
                code_uspace, code_zone_reason,prohibition, region,authority_id, notification_id, metadata_metadata_id,time_period_time_period_id, volume_limitation_def,condition_expression,
                upperlimitnorestriccion,period_type_id, identifier, intervalbefore, 'True')
                if v[0]==False :
                    print("The insertion of the entity through to an exception, its properties would be deleted on the ddbb")
                    if len(v)>2 : 
                        cm.delete_geom(v[1])
                    elif(len(v)>1):
                        for time_id in time_period_time_period_id:
                            query_delete_time=""" DELETE FROM slot_time WHERE time_period_time_period_id='{}';
                                            DELETE FROM time_period WHERE time_period_id='{}';""".format(time_id, time_id)
                            try:
                                self._cursor.execute(query_delete_time)
                                self._pgconn.connection.commit()

                            except Exception as ex :
                                print("ERROR: ", type(ex), ex.args)
                        
                        
                        query_delete= """DELETE FROM metadata WHERE metada_id='{}';
                                        DELETE FROM authority WHERE authority_oid='{}';
                                        DELETE FROM authority WHERE authority_oid='{}';
                                    """.format(metadata_metadata_id,authority_id,notification_id)
                        try:
                            self._cursor.execute(query_delete)
                            self._pgconn.connection.commit()

                        except Exception as ex :
                            print("ERROR: ", type(ex), ex.args)




             

          
            