from django.core.files.storage import FileSystemStorage
import os
import math, random, pytz, string
import base64
from io import BytesIO
from django.core.files.storage import FileSystemStorage
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.conf import settings
from PIL import Image
import os
import random
import string

def gernateToken():
	import random
	import array
	MAX_LEN = 66
	DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 
	LOCASE_CHARACTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h','i', 'j', 'k', 'm', 'n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y','z']
	COMBINED_LIST = DIGITS + LOCASE_CHARACTERS
	rand_digit = random.choice(DIGITS)
	rand_lower = random.choice(LOCASE_CHARACTERS)
	temp_pass = rand_digit + rand_lower
	for x in range(MAX_LEN - 4):
		temp_pass = temp_pass + random.choice(COMBINED_LIST)
		temp_pass_list = array.array('u', temp_pass)
		random.shuffle(temp_pass_list)
	token = ""
	for x in temp_pass_list:
			token = token + x
	return token

def GernateOTP():
	'''Function for gernate otp'''
	digits = "0123456789"
	OTP = ""
	for i in range(4) :
		OTP += digits[math.floor(random.random() * 10)]
	return OTP

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 save_base64_image(base64_data, file_name):
    # Decode the base64 string
    image_data = base64.b64decode(base64_data)
    image = Image.open(BytesIO(image_data))

    # Generate the file path
    fullPath = 'media/customer_profile'
    fs = FileSystemStorage(location=fullPath)

    # Save the image as a file
    filetype = '.png'  # You can choose other file types depending on your input data.
    theName = randomStringFunctionForImage()
    final_image_name = f"{theName}{filetype}"

    # Save the image
    image_path = fs.save(final_image_name, BytesIO(image_data))
    return f"{fullPath}/{image_path}"



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


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

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