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

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

    def create_geofence(self):
        cm= CommonVolumes(self._pgconn)
        message='Example of some restrictions'
        creationdate_metadata=datetime.datetime(2020,1,1)
        lastupdate_metadata=datetime.datetime(2020,1,1)
        author='Ana'
        country='ESP'
        code_zone_type=2
        code_restriction=[2,3]
        code_uspace= [2,4,7]
        code_zone_reason=[2,4]
        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=3

        #time_period 1:

        start_date_time=datetime.datetime(2020,2,1)
        end_date_time=datetime.datetime(2020,3,30)
        start_time1 = time.strftime("%m/%d/%Y %H:%M:%S", time.localtime(time.mktime((2018,9,9,8,0,0,0,0,0))))
        end_time1 = time.strftime("%m/%d/%Y %H:%M:%S", time.localtime(time.mktime((2018,9,9,12,0,0,0,0,0))))
        slots=[[start_time1,end_time1]]
        days=['M','W','Th']
        time_period_time_period_id1=cm.specification_time(start_date_time,end_date_time,slots,days)

        #time period 2:
        
        start_date_time=datetime.datetime(2020,2,1)
        end_date_time=datetime.datetime(2020,2,28)
        start_time1 = time.strftime("%H:%M:%S", time.localtime(time.mktime((2018,9,9,11,0,0,0,0,0))))
        end_time1 = time.strftime("%H:%M:%S", time.localtime(time.mktime((2018,9,9,15,0,0,0,0,0))))
        slots=[[start_time1,end_time1]]
        days=['F','Sa']
        time_period_time_period_id2=cm.specification_time(start_date_time,end_date_time,slots,days)
        
        time_period_time_period_id=[time_period_time_period_id1, time_period_time_period_id2]

        metadata_metadata_id=cm.specification_metadata(creationdate_metadata,lastupdate_metadata,author)
        volume_limitation_def=[1,7]
        condition_expression=[1,3]
        upperlimitnorestriction=50
        name='EUROCAE_example_1'
        geometry= '''MULTIPOLYGON(((-2.8001205514147 37.8923252965836,-2.67564686446385 37.9656428983705,-2.72231818391457 37.8916144355108,-2.72428122429462 37.8879845103478,
        -2.70226559709314 37.8605123144506,-2.8001205514147 37.8923252965836)))
        '''
        buffer=0
        layer_id=cm.get_volumelayer('sensitive_volume')
        f_id=0
        identifier=""
        intervalbefore=""
        active="true"


        vol_id=cm.insertIntoVolumes(name,0,150,geometry,buffer,layer_id, f_id,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,
        upperlimitnorestriction,period_type,identifier, intervalbefore, active)