import sys
import math
import time
import os
import datetime
sys.path.append('../BizPit_Backend')
sys.path.append('../BizPit_Backend/BizPit_app')
from BizPit_app.models import *
sys.path.append('../Bizpit_Backend')
sys.path.append('../Bizpit_Backend/BizPit_app')

def details(bundle):
    rate_hr=0
    h_d=[]
    rate_daily=0
    rate_weekly=0
    rate_monthly=0
    capacity=0
    property_status=[]
    property_typ1=bundle.data['property_type']
    new_property_typ=property_typ1.replace("[","")
    property_typ=new_property_typ.replace("]","")
    property_type11=property_typ.split(', ')
    print "new_property_typ..............",property_type11
    property_type=','.join(property_type11)
    print "new_property_typ..............",property_type
    
    try:
        property_subtype=bundle.data['subtype']
    except:
        property_subtype=""
    property_name=bundle.data['property_name']
    location=bundle.data['location']
    address=bundle.data['address']
    latitude=bundle.data['latitude']
    longitude=bundle.data['longitude']
    starting_price=bundle.data['starting_price']
    submitted_by=bundle.data['submitted_by']
    Shared=bundle.data['Shared']
    Meeting=bundle.data['Meeting']
    Training=bundle.data['Training']
    Event_hall=bundle.data['Event_hall']
    start_time=bundle.data['start_time']
    end_time=bundle.data['end_time']
    holiday=bundle.data['holiday']
    non_workingday=bundle.data['non_workingday']
##    plug_images=bundle.data['plug_images']
##    meeting_images=bundle.data['meeting_images']
##    training_images=bundle.data['training_images']
##    event_images=bundle.data['event_images']
    
    file_name1=str(bundle.data['file_name1'])
    file_content1=str(bundle.data['file_content1'])
    file_name2=str(bundle.data['file_name2'])
    file_content2=str(bundle.data['file_content2'])
    file_name3=str(bundle.data['file_name3'])
    file_content3=str(bundle.data['file_content3'])
    file_name4=str(bundle.data['file_name4'])
    file_content4=str(bundle.data['file_content4'])
    file_name5=str(bundle.data['file_name5'])
    file_content5=str(bundle.data['file_content5'])
    frfacilities=bundle.data['frfacilities']
    paidfacilities=bundle.data['paidfacilities']
    default=bundle.data['default']
##    try:
##        default=bundle.data['default']
##    except:
##        default=property_typ1
    description=bundle.data['description']
    print "default",default

    print "frfacilities",frfacilities

    
    

    print "am herere",Shared
    if(property_type !="" and property_name !="" and location !="" and address !="" and latitude !="" and longitude !="" and starting_price !="" and submitted_by !=""):
	
	my_data=Property_Details.objects.all()

	if(my_data):
		for data in my_data:
			my_id=data.property_id
		new_id=my_id+1
	else:
		my_id=1
		new_id=my_id

        hol_day=holiday.split(', ')
        print"holi day...........",hol_day
##        hol_day.lstrip(' ')
##        hol_day.rstrip(' ')
        for h in hol_day:
            print h
            
            h1=h.strip()
            print "hhhhhhh",h1
            h_d.append(h1)
        print "-------------",h_d
        holi_day=','.join(h_d)
        print "holi....",holi_day
        new_data_holi=Holidays()
        new_data_holi.property_id=new_id
        new_data_holi.holidays=holi_day
        
        new_data_holi.save()

        non_wrk=non_workingday.split(', ')
        print"non_wrk",non_wrk
        n_wrk=','.join(non_wrk)
        print"n_wrk",n_wrk
        new_data_holi1=Non_Working_Days()
        new_data_holi1.property_id=new_id
        new_data_holi1.Non_Working_days=n_wrk
        new_data_holi1.save()

        frfec=frfacilities.split(', ')
        print frfec
        data=Property_Facility()
        data.property_id=new_id
        for f1 in frfec:
            f=f1.strip()
##            print f
##        "Wi-Fi,Water,Coffee/Tea,Charging,Seat up,Chairs,Locker,Charging Room,Library,AC"
            if(f=="Wi-Fi"):
                data.Wifi=True
##            else:
##                data.Wifi=False
            if(f=="Water"):
                data.Water_bottle=True
##            else:
##                data.Water_bottle=False
            if(f=="Coffee/Tea"):
                data.Coffee_tea=True
            if(f=="Charging"):
                data.Charging_Point=True
            if(f=="Seat up"):
                data.Seat_up="common"
            if(f=="Chairs"):
                data.Chair="office"
            if(f=="Locker"):
                data.Locker=True
            if(f=="Charging Room"):
                data.Changing_room=True
            if(f=="Library"):
                data.Library=True
            if(f=="AC"):
                data.AC=True
                
        paidfec=paidfacilities.split(', ')
        for p1 in paidfec:
            p=p1.strip()
            if(p=="Stationary"):
                data.Stationary="basic"
            if(p=="Printer"):
                data.Printer=True
            if(p=="Projector"):
                data.Projector=True
            if(p=="Projection Screen"):
                data.Projection_screen=True
            if(p=="PC/Laptop"):
                data.PC_or_Laptop=True

        data.save()
        
	new_data=Property_Details()
	new_path=[]

##        if(plug_images):
##
##            p_images=plug_images.split(', ')
##            for img in p_images:
##                
##                new_images=img.split('|')
##                print "new_images",new_images
##                pathrelative1=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+"Plug&Play"+"/"+(new_images[0]))
##                pathrelative11_plug=str("static"+"/"+str(new_id)+"/"+"Plug&Play"+"/"+(new_images[0]))
##                
##                path= pathrelative1
##        #        print "pathsjdjjfj",path
##                d = os.path.dirname(path)
##                if not os.path.exists(d):
##                        os.makedirs(d)
##                print "d.............",d
##        ##	os.makedirs(path,"w")
##                if(new_images[0]):
##                    fh = open(pathrelative1, "wb")
##                    fh.write(new_images[1].decode('base64'))
##                    new_path.append(pathrelative11_plug)
##                    fh.close()
##        
##
##        if(meeting_images):
##
##            m_images=meeting_images.split(', ')
##            for img in m_images:
##                
##                new_images=img.split('|')
##                print "new_images",new_images
##                pathrelative1=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+"Meeting_Room"+"/"+(new_images[0]))
##                pathrelative11_meet=str("static"+"/"+str(new_id)+"/"+"Meeting_Room"+"/"+(new_images[0]))
##                
##                path= pathrelative1
##        #        print "pathsjdjjfj",path
##                d = os.path.dirname(path)
##                if not os.path.exists(d):
##                        os.makedirs(d)
##                print "d.............",d
##        ##	os.makedirs(path,"w")
##                if(new_images[0]):
##                    fh = open(pathrelative1, "wb")
##                    fh.write(new_images[1].decode('base64'))
##                    new_path.append(pathrelative11_meet)
##                    fh.close()
##
##        if(training_images):
##
##            t_images=training_images.split(', ')
##            for img in t_images:
##                
##                new_images=img.split('|')
##                print "new_images",new_images
##                pathrelative1=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+"Training_Room"+"/"+(new_images[0]))
##                pathrelative11_train=str("static"+"/"+str(new_id)+"/"+"Training_Room"+"/"+(new_images[0]))
##                
##                path= pathrelative1
##        #        print "pathsjdjjfj",path
##                d = os.path.dirname(path)
##                if not os.path.exists(d):
##                        os.makedirs(d)
##                print "d.............",d
##        ##	os.makedirs(path,"w")
##                if(new_images[0]):
##                    fh = open(pathrelative1, "wb")
##                    fh.write(new_images[1].decode('base64'))
##                    new_path.append(pathrelative11_train)
##                    fh.close()
##
##        if(event_images):
##
##            e_images=event_images.split(', ')
##            for img in e_images:
##                
##                new_images=img.split('|')
##                print "new_images",new_images
##                pathrelative1=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+"Event_Hall"+"/"+(new_images[0]))
##                pathrelative11_event=str("static"+"/"+str(new_id)+"/"+"Event_Hall"+"/"+(new_images[0]))
##                
##                path= pathrelative1
##        #        print "pathsjdjjfj",path
##                d = os.path.dirname(path)
##                if not os.path.exists(d):
##                        os.makedirs(d)
##                print "d.............",d
##        ##	os.makedirs(path,"w")
##                if(new_images[0]):
##                    fh = open(pathrelative1, "wb")
##                    fh.write(new_images[1].decode('base64'))
##                    new_path.append(pathrelative11_event)
##                    fh.close()
            
            
        
##        path_of_log="static"+"/"+"Developer"+"/"+str(data.developer)+".jpg"
	pathrelative1=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+(file_name1))
	pathrelative11=str("static"+"/"+str(new_id)+"/"+(file_name1))
	
	path= pathrelative1
#        print "pathsjdjjfj",path
        d = os.path.dirname(path)
        if not os.path.exists(d):
        	os.makedirs(d)
        print "d.............",d
##	os.makedirs(path,"w")
        if(file_name1):
            fh = open(pathrelative1, "wb")
            fh.write(file_content1.decode('base64'))
            new_path.append(pathrelative11)
            fh.close()

        pathrelative2=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+(file_name2))
	pathrelative12=str("static"+"/"+str(new_id)+"/"+(file_name2))
	
	path= pathrelative2
#        print "pathsjdjjfj",path
        d = os.path.dirname(path)
        if not os.path.exists(d):
        	os.makedirs(d)
        print "d.............",d
        if(file_name2):
            print "iam in 2......."
            fh = open(pathrelative2, "wb")
            fh.write(file_content2.decode('base64'))
            new_path.append(pathrelative12)
            fh.close()

        pathrelative3=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+(file_name3))
	pathrelative13=str("static"+"/"+str(new_id)+"/"+(file_name3))
	
	path= pathrelative3
#        print "pathsjdjjfj",path
        d = os.path.dirname(path)
        if not os.path.exists(d):
        	os.makedirs(d)
        print "d.............",d
        if(file_name3):
            print "iam in 3......."
            fh = open(pathrelative3, "wb")
            fh.write(file_content3.decode('base64'))
            new_path.append(pathrelative13)
            fh.close()


        pathrelative4=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+(file_name4))
	pathrelative14=str("static"+"/"+str(new_id)+"/"+(file_name4))
	
	path= pathrelative4
#        print "pathsjdjjfj",path
        d = os.path.dirname(path)
        if not os.path.exists(d):
        	os.makedirs(d)
        print "d.............",d
        if(file_name4):
            print "iam in 4......."
            fh = open(pathrelative4, "wb")
            fh.write(file_content4.decode('base64'))
            new_path.append(pathrelative14)
	    fh.close()
##        fh.close()


        pathrelative5=str("../BizPit_Backend/static"+"/"+str(new_id)+"/"+(file_name5))
	pathrelative15=str("static"+"/"+str(new_id)+"/"+(file_name5))
	
	path= pathrelative5
#        print "pathsjdjjfj",path
        d = os.path.dirname(path)
        if not os.path.exists(d):
        	os.makedirs(d)
        print "d.............",d
        if(file_name5):
            print "iam in 5......."
            fh = open(pathrelative5, "wb")
            fh.write(file_content5.decode('base64'))
            new_path.append(pathrelative15)
       
            fh.close()


        print "new_path.........",new_path
        newww_path=', '.join(new_path)
        print "newww_path",newww_path

        
        
	new_data.property_id=new_id
	new_data.status="active"
	new_data.property_type=property_type
	new_data.property_subtype=property_subtype
	new_data.property_name=property_name
	new_data.location=location
	new_data.address=address
	new_data.latitude=latitude
	new_data.longitude=longitude
	new_data.starting_price=starting_price
	new_data.submitted_by=submitted_by
	new_data.operating_strattime=start_time
	new_data.operating_stoptime=end_time
	new_data.images=newww_path
	new_data.timestamp=time.strftime('%d-%m-%Y %H:%M')
	new_data.default_type=str(default)
	new_data.description=description
	new_data.save()
        book=Booking_Type()
        book.property_id=new_id
        book_type=[]
        
        
        if("Training Room" in property_type):
            book.property_type="Training Room"
            
##            capa=bundle.data['capacity']
            
##            plug.save()
            if(Training!=""):
                my_data=Training.split("||")
                print "mydata............",my_data
                room_no=1
                rate_hr=0
                rate_daily=0
                rate_weekly=0
                rate_monthly=0
                for m in my_data:  
                    data_value=(m.split(","))
                    print "sammama",data_value

                    for val in data_value:
                        print"val",val
                        vall=val.split("|")
                        print"vall",vall
                        for i in vall:
                            string=str(vall[0])
                            value=int(vall[1])
                            print"real value date",string,type(string)
                            print"time",value
                            
    ##                        book_type.append(string)
    ##                        book.book_type=book_type
                            #new_data1=Shared_area()
                            #new_data1.property_id=new_id
                            plug=Training_room()
                            plug.property_id=new_id
                            if(string=="HalfDay"):
                                print"am in Halfday\n",value
                                rate_hr=value
                                book_type.append("HalfDay")
                                print "book_type......-----------",book.book_type
                            if(string=="FullDay"):
                                rate_daily=value
                                book_type.append("FullDay")
                            if(string=="Weekly"):
                                rate_weekly=value
                                book_type.append("Weekly")
                            if(string=="Monthly"):
                                rate_monthly=value
                                book_type.append("Monthly")
                            if(string=="Capacity"):
                                capacity=value
                            plug.shared_images=""
                            plug.capacity=capacity
                    book_type=list(set(book_type))
                    print "book_type......",book_type
                    
                    b_typ=','.join(book_type)
                    print "book_type......",b_typ
                    book_type=[]
                    book.book_type=b_typ
                    plug.rate_half_day=rate_hr
                    plug.rate_full_day=rate_daily
                    plug.rate_weekly=rate_weekly
                    plug.rate_monthly=rate_monthly
                    plug.training_room_no=room_no
##                    if(training_images):
##                        plug.training_room_images=pathrelative11_train
##                    else:
##                        plug.training_room_images=""
                    plug.save()

                    room_no=room_no+1
            book.save()

        if("Event_Hall" in property_type):
            book.property_type="Event_Hall"
##            capa=bundle.data['capacity']

            if(Event_hall!=""):
                my_data=Event_hall.split("||")
                print "mydata............",my_data
                room_no=1
                rate_hr=0
                rate_daily=0
                rate_weekly=0
                rate_monthly=0
                for m in my_data:
                        data_value=(m.split(","))
                        print "sammama",data_value

                        for val in data_value:
                            print"val",val
                            vall=val.split("|")
                            print"vall",vall
        ##                    book_type.append(string)
        ##                    book.book_type=book_type
                            for i in vall:
                                string=str(vall[0])
                                value=int(vall[1])
                                print"real value date",string,type(string)
                                print"time",value
                                plug=Event_Halls()
                                plug.property_id=new_id
        ##                        book.book_type=string
                                #new_data1=Shared_area()
                                #new_data1.property_id=new_id
                                
                                if(string=="HalfDay"):
                                    print"am in Hourly\n",value
                                    rate_hr=value
                                    book_type.append("HalfDay")
                                if(string=="FullDay"):
                                    rate_daily=value
                                    book_type.append("FullDay")
                                if(string=="Weekly"):
                                    rate_weekly=value
                                    book_type.append("Weekly")
                                if(string=="Monthly"):
                                    rate_monthly=value
                                    book_type.append("Monthly")
                                if(string=="Capacity"):
                                    capacity=value
                                plug.shared_images=""
                                plug.capacity=capacity
                        book_type=list(set(book_type))
                        print "book_type......",book_type
                        
                        b_typ=','.join(book_type)
                        print "book_type......",b_typ
                        book.book_type=b_typ
                        
                        book_type=[]
                        
                        plug.rate_halfday=rate_hr
                        plug.rate_daily=rate_daily
                        plug.room_no=room_no
        ##                plug.rate_weekly=rate_weekly
        ##                plug.rate_monthly=rate_monthly
##                        if(event_images):
##                            plug.shared_images=pathrelative11_event
##                        else:
##                            plug.shared_images=""
                    
                        plug.save()
                        room_no=room_no+1
                book.save()
        if("Plug & Play" in property_type):
		if(Shared!=""):
		    book.property_type="Plug & Play"
		    rate_hr=0
		    rate_daily=0
		    rate_weekly=0
		    rate_monthly=0
		    capacity=0
		    data_value=(Shared.split(","))
		    print "sammama",data_value

		    for val in data_value:
		        print"val",val
		        vall=val.split("|")
		        print"vall",vall
		        for i in vall:
		            string=str(vall[0])
		            value=int(vall[1])
		            print"real value date",string,type(string)
		            print"time",value
	##                    book_type.append(string)
	##                    print "book_type........",book_type
	##                    book.book_type=string
		            new_data1=Shared_area()
		            new_data1.property_id=new_id
		            
		            if(string=="Hourly"):
		                print"am in Hourly\n",value
		                rate_hr=value
		                book_type.append("Hourly")
		            if(string=="Daily"):
		                rate_daily=value
		                book_type.append("Daily")
		            if(string=="Weekly"):
		                rate_weekly=value
		                book_type.append("Weekly")
		            if(string=="Monthly"):
		                rate_monthly=value
		                book_type.append("Monthly")
		            if(string=="Capacity"):
		                capacity=value
		            new_data1.shared_images=""
		    
		    book_type=list(set(book_type))
		    print "book_type......",book_type
		    
		    b_typ=','.join(book_type)
		    print "book_type......",b_typ
		    book.book_type=b_typ
		    
		    book_type=[]
		    print"sadskadfkjasdfkjasjdfjasdfjnasdjfnjasdf",new_data1.capacity,new_data1.rate_hr,rate_hr
		    new_data1.rate_hr=rate_hr
		    new_data1.rate_daily=rate_daily
		    new_data1.rate_weekly=rate_weekly
		    new_data1.rate_monthly=rate_monthly
		    new_data1.capacity=capacity
##		    if(plug_images):
##                        new_data1.shared_images=pathrelative11_plug
##                    else:
##                        new_data1.shared_images=""
##		    new_data1.save()
            
                book.save()  
	new_data.save()

	#property_status1="Property added successfully..."

	if("Meeting_Room" in property_type):
            	book.property_type="Meeting_Room"
		if(Meeting!=""):
##		    book.property_type=property_type
		    my_data=Meeting.split("||")
		    print "mydata............",my_data
                    room_no=1
		    for m in my_data:

                        data_value=(m.split(","))
                        print "sammama meeting",data_value

                        for val in data_value:
                            print"val",val
                            vall=val.split("|")
                            print"vall",vall
                            for i in vall:
                                string=str(vall[0])
                                value=int(vall[1])
                                print"real value date",string,type(string)
                                print"time",value
            ##                    book.book_type=string
                                new_data1=Meeting_room()
                                new_data1.property_id=new_id
                                
                                if(string=="Hourly"):
                                    print"am in Hourly\n",value
                                    rate_hr=value
                                    book_type.append("Hourly")
                                if(string=="Daily"):
                                    rate_daily=value
                                    book_type.append("Daily")
                                if(string=="Weekly"):
                                    rate_weekly=value
                                    book_type.append("Weekly")
                                if(string=="Monthly"):
                                    rate_monthly=value
                                    book_type.append("Monthly")
                                if(string=="Capacity"):
                                    capacity=value
                                new_data1.shared_images=""
            ##                    book_type.append(string)
                        book_type=list(set(book_type))
                        print "book_type......",book_type
                        
                        b_typ=','.join(book_type)
                        print "book_type......",b_typ
                        book.book_type=b_typ
                        
                        book_type=[]
                            
                        new_data1.rate_hr=rate_hr
                        new_data1.rate_daily=rate_daily
                        new_data1.rate_weekly=rate_weekly
                        new_data1.rate_monthly=rate_monthly
                        new_data1.capacity=capacity
                        new_data1.room_no=room_no
##                        if(meeting_images):
##                            new_data1.meeting_room_images=pathrelative11_meet
##                        else:
##                            new_data1.meeting_room_images=""
                        new_data1.save()

                        room_no=room_no+1
            
            	book.save()
	new_data.save()

	property_status1="Property added successfully..."

	
##
##	if(Training!=""):
##
##            data_value=(Shared.split(","))
##            print "sammama",data_value
##
##            for val in data_value:
##                print"val",val
##                vall=val.split("|")
##                print"vall",vall
##                for i in vall:
##                    string=str(vall[0])
##                    value=int(vall[1])
##                    print"real value date",string,type(string)
##                    print"time",value
##
##                    new_data1=Training_room()
##                    new_data1.property_id=new_id
##                    
##                    if(string=="Hourly"):
##                        print"am in Hourly\n",value
##                        rate_hr=value
##                    if(string=="Daily"):
##                        rate_daily=value
##                    if(string=="Weekly"):
##                        rate_weekly=value
##                    if(string=="Monthly"):
##                        rate_monthly=value
##                    if(string=="Capacity"):
##                        capacity=value
##                    new_data1.shared_images=""
##            print"sadskadfkjasdfkjasjdfjasdfjnasdjfnjasdf",new_data1.capacity,new_data1.rate_hr,rate_hr
##            new_data1.rate_hr=rate_hr
##            new_data1.rate_daily=rate_daily
##            new_data1.rate_weekly=rate_weekly
##            new_data1.rate_monthly=rate_monthly
##            new_data1.capacity=capacity
##            new_data1.training_room_no=1
##            new_data1.meeting_room_images=""
##            new_data1.save()
##            

##	new_data.save()

        property_status="Some fields are empty.."


    else:
	
	print "Some fields are empty.."
	property_status=("Some fields are empty..")


    ssnote={
                 
                
		"result":property_status1,
                
                
                }        


    return ssnote
	

