from Customer.models import *
from Driver.models import *
from AdminApp.models import *
from Customer.commonfunctions import sendfcmnotification
from django.template.loader import get_template 
from django.core.mail import EmailMultiAlternatives
from django.conf import settings

def assigndriverforparcel(customerjobid, driverid):
	print('adminfunctions.py - in assigndriverforparcel function')
	try:
		
		driver_id = driverid
		customerjob_id = customerjobid

		driver_obj = DriverModel.objects.filter(id = driver_id).first()
		customerjob_obj = CustomerJob.objects.filter(id = customerjob_id).first()
		if driver_obj and customerjob_obj:
			driverjob_obj = DriverJob.objects.create(driver = driver_obj, customerjob = customerjob_obj)
			if driverjob_obj:
				try:
					Notifications.objects.create(user = customerjob_obj.customer.user, customerjobid = customerjob_obj.id, forcustomer = True, notificationtype = 'Driver Assigned', notificationtext = 'Driver has been assigned for your parcel request.', status = 'unread')
				except Exception as e:
					print(e)
				try:
					sendfcmnotification(customerjob_obj.customer.fcmtoken, 'customer', 'statuschange', 'KwickDelivery', 'Driver has been assigned for your parcel request')
				except Exception as e:
					print(e)
				
				try:
					Notifications.objects.create(user_id = driver_obj.user.id, customerjobid = customerjob_obj.id, fordriver = True, notificationtype = 'Parcel Pickup Request', notificationtext = 'You have one pickup request.', status = 'unread')
				except Exception as e:
					print(e)
				try:
					sendfcmnotification(driver_obj.fcmtoken, 'driver', 'statuschange', 'KwickDelivery', 'You have one pickup request')
				except Exception as e:
					print(e)
				return(True)
				# return JsonResponse({'status_code' : 200, 'status_message' : 'Driver is assigned for this job.'})
			else:
				return(False)
				# return JsonResponse({'status_code' : 400, 'status_message' : 'Bad request.'})
	except Exception as e:
		print(e)
		return(False)
		return JsonResponse({'status_code' : 500, 'status_message' : 'Internal Server Error.'})



def sendnewusermail(senderemail, recipientemail, subject, username, useremail, userpassword, loginurl):
	try:
		html = """
				<html>
				   <head>
				      <title>email-template</title>
				      <link href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@700&display=swap" rel="stylesheet">
				      <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&family=Roboto+Slab:wght@700&display=swap" rel="stylesheet">
				   </head>
				   <body style="background: #f5f5f5;">
				      <table border="0" cellpadding="0" cellspacing="0" style="width: 100%;margin:0 auto;padding: 35px;">
				         <tr>
				            <td>
				               <table cellpadding="0" cellspacing="0" align="center" style="background: #fff;border-collapse:collapse;text-align: center;border-top: 4px solid #9a55ff; padding: 10px;background: #fff;width: 550px;">
				                 
				                  <tr>
				                     <td align="center" height="30"></td>
				                  </tr>
				                  <tr>
				                     <td align="center" height="40">
				                        <span style="font-size:20px;font-weight:700;margin-bottom: 22px;font-family: 'Open Sans',sans-serif;color:#000;">Hello """ + username + """</span>
				                     </td>
				                  </tr>
				                  <tr>
				                     <td style="text-align: center;">
				                        <span  style="display:block;margin-bottom:25px;font-family: 'Open Sans', sans-serif;font-size:18px;color:#000;">   Your Account has been Approved by Admin.   </span>
				                     </td>
				                  </tr>
				                  <tr>
				                     <td align="center" height="100">
				                        <img src='""" + str(settings.WEB_BASE_URL) + """/static/images/logo1.png' style="width:100px;">
				                     </td>
				                  </tr>
				                  <tr>
				                     <td style="text-align: center;">
				                       
				                        <span  style="color:#000; display:block;margin-bottom:10px;margin-top:20px; font-family: 'Open Sans', sans-serif;font-size:16px;">   Email :  <a href="" target="_blank" style="color:#000;text-decoration:none;font-weight: 600;
				                           ">  """ + useremail + """  </a>   </span>
				                        <span  style="color:#000; display:block;margin-bottom:10px;margin-top:10px; font-family: 'Open Sans', sans-serif;font-size:16px;">   Password :  <a href="" target="_blank" style="color:#000;text-decoration:none;font-weight: 600;
				                           ">  """ + userpassword + """ </a>   </span>
				                        <span  style="color:#000;display:block;margin-bottom:50px;margin-top:10px; font-family: 'Open Sans', sans-serif;font-size:16px;">   Link :  <a href='""" + loginurl + """' target="_blank" style="color:#9a55ff;text-decoration:none;font-weight: 600;
				                           ">  """ + loginurl + """  </a>   </span>
				                     </td>
				                  </tr>
				               </table>
				            </td>
				         </tr>
				      </table>
				   </body>
				</html>

		"""
		print(html)
		subject, from_email, to = subject, senderemail, recipientemail
		html_content = html
		msg = EmailMultiAlternatives(subject, 'text_content', from_email, [to])
		msg.attach_alternative(html_content, "text/html")
		res = msg.send()
		print(res)
		if res == 1:
			return True
		else:
			return False

	except Exception as e:
		print(e)
		return False