from django.core.files.storage import FileSystemStorage
import os
import stripe
from django.conf import settings
from rest_framework.response import Response
from rest_framework import status
from panel_apis.models import *

def randomStringFunctionForImage():
	import random,string
	str1 = ''.join((random.choice(string.ascii_letters) for x in range(8)))
	str1 += ''.join((random.choice(string.digits) for x in range(4)))
	sam_list = list(str1)
	random.shuffle(sam_list)
	finalString = ''.join(sam_list)
	return finalString

def uploadTheIdpicture(image):
	fullPath='media/id_picture'
	fs = FileSystemStorage(location=fullPath)
	fullPath2='media/id_picture'
	filetype = os.path.splitext(image.name)[1]
	theName=randomStringFunctionForImage()
	theImageName=str(theName)+str(filetype)
	filename = fs.save(theImageName, image)
	return str(fullPath2)+'/'+filename

def uploadThemessagepicture(image):
	fullPath='media/message'
	fs = FileSystemStorage(location=fullPath)
	fullPath2='media/message'
	filetype = os.path.splitext(image.name)[1]
	theName=randomStringFunctionForImage()
	theImageName=str(theName)+str(filetype)
	filename = fs.save(theImageName, image)
	return str(fullPath2)+'/'+filename

def uploadServiceRequest(image):
	fullPath='media/service_request'
	fs = FileSystemStorage(location=fullPath)
	fullPath2='media/service_request'
	filetype = os.path.splitext(image.name)[1]
	theName=randomStringFunctionForImage()
	theImageName=str(theName)+str(filetype)
	filename = fs.save(theImageName, image)
	return str(fullPath2)+'/'+filename

def createCustomerOnStripe():
	customerStripeID = ""
	try:
		stripe.api_key  = settings.STRIPE_SECRET_KEY
		createStripeCusObject = stripe.Customer.create(description="Customer for stripe card")
		if createStripeCusObject:
			customerStripeID = createStripeCusObject['id']
	except Exception as e:
		print("Exception",e)
	return customerStripeID


def getStripeKey():
	stripkey = stripeSettings.objects.filter(is_active=True).first()
	if stripkey:
		stripeSecret = stripkey.secretKey
		return stripeSecret
	else:
		return Response({'status_code':status.HTTP_500_INTERNAL_SERVER_ERROR,'status_message':'server error'},status=status.HTTP_500_INTERNAL_SERVER_ERROR)


def gernateOrderId():
	lastObj=orderPack.objects.all().last()
	if lastObj:
		if lastObj.orderId=='':return 'ADR00001'
		theId=lastObj.orderId
		theId=theId[3:]
		theId=int(theId)+1
		theId=str(theId)
		theId=theId.zfill(5)
		return "ORD"+str(theId)
	else:
		return 'ORD00001'


def paymentWithExistingCard(customerStripeId,cardId,totalAmount):
	response = {}
	try:
		totalAmount = str(totalAmount)
		totalAmount = float(totalAmount)
		totalAmount = int(totalAmount)
		print('working1')
		totalAmount  = totalAmount
		print('working2')
		totalAmount = int(totalAmount)
		stripe.api_key=settings.STRIPE_SECRET_KEY
		#stripe.api_key = getStripeKey()
		stripeAmountCharges = stripe.Charge.create(
			currency="SAR",
			customer=customerStripeId,
			card=cardId,
			amount=totalAmount*100,
			description = "charge from user",
		)
		print('stripeAmountCharges',stripeAmountCharges)
		if stripeAmountCharges:
			response['status'] = True
			response['data'] = stripeAmountCharges
			# return Response({'status_code':status.HTTP_200_OK,'status_message':'success','data':stripeAmountCharges})
			
	except Exception as e:
		response['status'] = False
		response['message'] = str(e)
	return response