import os, re, hashlib, time
from dotenv import load_dotenv

load_dotenv()
MAX_CONTENT_LENGTH = os.getenv('MAX_CONTENT_LENGTH')


class CommonLib:
    def __init__(self):
        self.test = 's'

    @staticmethod
    def filter_input(field):
        try:
            if field is None:
                return field
            # pat = r'[\[\]\:=*?<>+&|^~+\"\']'
            pat = r'[^A-Za-z0-9\.\s\@\&\-\_]+'
            return re.sub(pat, "", field)
        except Exception as e:
            return str(e)

    @staticmethod
    def filter_path(path):
        try:
            if path is None or path == '':
                return None
            pat = r'[^A-Za-z0-9\.\s\@\&\-\_\/]+'
            return re.sub(pat, "", path)
        except Exception as e:
            return str(e)

    def isValidClientid(self, clientid):
        pattern = r'^[a-zA-Z0-9]{8}$'
        return re.match(pattern, clientid)
    
    def isValidTimestamp(self, ts):
        pattern = r'^[0-9]{10}$'
        return re.match(pattern, ts)
    
    def isValidhmac(self, hmac):
        pattern = r'^[a-zA-Z0-9]{64}$'
        return re.match(pattern, hmac)    
    
    def isValidAadhaar(self, str):
        pattern = r'^[2-9]{1}[0-9]{11}$'
        return re.match(pattern, str)
    
    def isValidName(self, str):
        pattern = r'^[a-zA-Z]{4,}(?: [a-zA-Z]+){0,2}$'
        return re.match(pattern, str)
        
    
    def validate_input(self, request):
        uid = request.values.get('uid')
        fullname = request.values.get('name')
        gender = request.values.get('gender')
        dob = request.values.get('dob')
                
        clientid = request.values.get('clientid')
        ts = request.values.get('ts')
        # print('=------', uid, fullname, gender, dob, clientid, ts)
        # exit()
        if clientid is None or self.isValidClientid(clientid) is None:
            return {'status': 'error', 'error_description': 'Invalid Clientid provided.'}, 400
        
        if ts is None or self.isValidTimestamp(ts) is None:
            return {'status': 'error', 'error_description': 'Invalid Timestamp provided.'}, 400
            
        if uid is None or self.isValidAadhaar(uid) is None:
                return {'status': 'error', 'error_description': 'Invalid Aadhaar provided.'}, 400
        
        if fullname is None or self.isValidName(fullname) is None:
                return {'status': 'error', 'error_description': 'Invalid Name provided.'}, 400
                
                            
        return [uid, fullname, gender, dob, clientid, ts], 200
    
    def check_secret_validate_demoauth(self, data, clientid, ts, hmac):
        
        secret = os.getenv('DEMO_AUTH_API_CLIENT_SECRET')
        plain_text = secret + clientid + ts + data['uid'] + data['name'] + data['gender'] +data['dob']+data['consent']
        hmac_generated = hashlib.sha256(plain_text.encode()).hexdigest()
        
        expire_time = int(os.getenv('HMAC_EXPIRY')) + int(ts)
       
        if expire_time < int(time.time()):
            return {'status':'error', 'error_description':'HMAC does not match.'}, 400
        
        if hmac != hmac_generated:
            return {'status':'error', 'error_description':'Invalid HMAC Provided.'}, 400
        else:
            return True, 200