import os, json, requests

from dotenv import load_dotenv
from datetime import datetime
from lib.redislib import RedisLib
from lib.validations import Validation
from lib.rabbitmqlib import RabbitMQAccounts

load_dotenv()

'''Constants'''
url = os.getenv('UIDAI_DEMO_AUTH_API_URL')

class Nominee_services:
    def __init__(self):
        self.rs = RedisLib()
        self.rq = RabbitMQAccounts()

    def validate_demoauth(self,data):
        try:
            txn =  data['uid'] + datetime.now().strftime("%Y%m%d%H%M%S") + "DGLKR" 
            
            access_token = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' 
            payload = 'uid='+data['uid']+'&fullname='+data['name']+'&gender='+data['gender']+'&dob='+data['dob']+'&pin='+''+'&aadhaar_token='+access_token+'&transaction_id='+txn+'&consent='+data['consent']
            headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
            }
            
            curl_response = requests.request("POST", url, headers=headers, data=payload)
            return json.loads(curl_response.text)
            
        except Exception as e:
            return {"status": str(e)+"error in validate demo auth"}
          
    def set_redis_data(self, key, value, ex=None):
        try:
            if (key ,value, ex):                        
                #storing in redis
                redis_status = self.rs.set(key = key, value = value, ex = ex)
                return {'status':'success','redis_status':redis_status}
            else:
                return {'status':'error'}
        except Exception as e:
            print(str(e))
            return {'status':'error in set redis function','error_description':str(e)}

    def remove_redis_data(self, key):
        try:
            if (key):                        
                #removing from redis
                redis_status = self.rs.remove(key = key)
                return {'status':'success','redis_status':redis_status}
            else:
                return {'status':'error'}
        except Exception as e:
            print(str(e))
            return {'status':'error in remove redis function','error_description':str(e)}
    
    def get_redis_data(self, key):
        try:
            if key:
                redis_status = self.rs.get(key = key)
                return {'status':'success','redis_data':redis_status}
            else:
                return {'status':'error'}
        except Exception as e:
            print(str(e))
            return {"status": "error in get redis function", "error_description": str(e)}

    def form_validation(self, mobile, email, dob, aadhaar, name, gender, relation):
        try:
            valid_aadhaar = Validation.aadhaar_validation(aadhaar)
            valid_name = Validation.name_validation(name)
            valid_dob = Validation.dob_validation(dob)
            valid_gender = Validation.gender_validation(gender)
            valid_relation = Validation.relation_validation(relation)
            valid_mobile = Validation.mobile_validation(mobile)
            valid_email = Validation.email_validation(email)

            if valid_aadhaar[1]['status'] == 'error':
                return {'status': 'error', 'error_description': valid_aadhaar[1]['error_description']}
            
            elif valid_name[1]['status'] == 'error':
                return {'status': 'error', 'error_description': valid_name[1]['error_description']}
            
            elif valid_dob[1]['status'] == 'error':
                return {'status': 'error', 'error_description': valid_dob[1]['error_description']}
            
            elif valid_gender[1]['status'] == 'error':
                return {'status': 'error', 'error_description': valid_gender[1]['error_description']}
            
            elif valid_relation[1]['status'] == 'error':
                return {'status': 'error', 'error_description': valid_relation[1]['error_description']}
            
            elif valid_mobile[1]['status'] == 'error':
                return {'status': 'error', 'error_description': valid_mobile[1]['error_description']}
                
            elif valid_email[1]['status'] == 'error':
                return {'status': 'error', 'error_description': valid_email[1]['error_description']}
            
            else:
                return {'status':'success'}
            
        except Exception as e:
            return {"status": "error in form validation function", "error_description": str(e)}