from gcm import GCM
import time
from datetime import datetime
import sys
sys.path.append('../BizPit_Backend')
sys.path.append('../BizPit_Backend/BizPit_app')
from BizPit_app.models import *

def notify():
    gcm = GCM("AIzaSyBB__xbySh6sq-WbQUuG7ZO_sRr_twbuYM")
    user_data=Payment.objects.all()
    for u in user_data:
        user_id=u.User_id
        property_id=int(u.property_id)
        print "property_id",property_id
	property_name=str(u.Property_name)
	property_status_data=Property_Details.objects.filter(property_id=property_id)
	for p_s_d in property_status_data:
            property_status=str(p_s_d.status)
	if(property_status=="active" or property_status=="Active"):
            new_data=Login.objects.get(user_id=user_id)
            if(new_data):
                gcm_id=str(new_data.gcm_id)
                if(gcm_id != " "):
                    if(u.notification==1 and u.rate=="ND"):
                        b_date=u.Booking_date
                        if(b_date != "ND"):
                            book_type=str(u.Booking_type)
                            if(book_type=="Hourly"):
                                list_date=b_date.split(", ")
                                for dt in list_date:
                                    if(dt != ""):
                                        last_dt=dt
                                        if("|" in last_dt):
                                            splt_dt=last_dt.split("|")
                                            for s_dt in splt_dt:
                                                lastt=splt_dt[0]
                                                print lastt
                                        elif("-" in last_dt):
                                            splt_dt=last_dt.split("-")
                                            for s_dt in splt_dt:
                                                lastt=splt_dt[0]
                                                print lastt
                                        else:
                                            pass
                            if(book_type=="Daily" or book_type=="HalfDay"):
                                list_date=b_date.split(", ")
                                for dt in list_date:
                                    if(dt != ""):
                                        last_dt=dt
                                        splt_dt=last_dt.split("-")
                                        for s_dt in splt_dt:
                                            lastt=splt_dt[0]
                                            print lastt
                            if(book_type=="Weekly"):
                                list_date=b_date.split(", ")
                                for dt in list_date:
                                    if(dt != ""):
                                        last_dt=dt
                                        splt_dt=last_dt.split(" - ")
                                        for s_dt in splt_dt:
                                            splt_dt1=s_dt.split("-")
                                            lastt=splt_dt1[0]
                                            print lastt
                            if(book_type=="Monthly"):
                                list_date=b_date.split(", ")
                                for dt in list_date:
                                    if(dt != ""):
                                        last_dt=dt
                                        splt_dt=last_dt.split("-")
                                        for s_dt in splt_dt:
                                            lastt=splt_dt[-1]
                                            print lastt
                            book_dt=datetime.strptime(lastt, '%d/%m/%Y')
                            if(book_dt.date() < datetime.today().date()):
                                data={'Property_id': str(property_id), 'param4': 'message', 'property_name': str(property_name)}
                                reg_ids=[]
                                print "data...",data
                                reg_ids.append(gcm_id)
                                response = gcm.json_request(registration_ids=reg_ids, data=data)
                                res = gcm.json_request(registration_ids=reg_ids, data=data,
                        collapse_key='uptoyou', delay_while_idle=True, time_to_live=3600
                )





notify()
