from django.core.files.storage import FileSystemStorage
import os
import random,string
import stripe
from django.conf import settings

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 uploadTheImages(image):
    fullPath='media/admin'
    fs = FileSystemStorage(location=fullPath)
    fullPath2='media/admin'
    filetype = os.path.splitext(image.name)[1]
    theName=randomStringFunctionForImage()
    theImageName=str(theName)+str(filetype)
    filename = fs.save(theImageName, image)
    return str(fullPath2)+'/'+filename


def generate_random_password(length=12):
    characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.sample(characters, length))
    return password



def generate_strip_id():
	try:
		stripe.api_key = settings.STRIPE_SECRET_KEY
		customer = stripe.Customer.create(
			description='Customer stripe card',
		)
		if customer:
			strip_id = customer['id']
		else:
			strip_id = ''
	except Exception as e:
		print(e)
	return strip_id




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="USD",
			customer=customerStripeId,
			card=cardId,
			amount=totalAmount*100,
			description = "charge from user",
		)
		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