from django.core.files.storage import FileSystemStorage
import os
from panel_apis.models import *
from auth_apis.models import *




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 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 uploadTheProfile(image):

	fullPath='media/buyer_profile'
	fs = FileSystemStorage(location=fullPath)
	fullPath2='media/buyer_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 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 gernateUserId():
    lastObj=Buyer.objects.all().last()
    if lastObj:
        if lastObj.customer_id=='':return 'USR00001'
        theId=lastObj.customer_id
        theId=theId[3:]
        theId=int(theId)+1
        theId=str(theId)
        theId=theId.zfill(5)
        return "USR"+str(theId)
    else:
        return 'USR00001'