import pika, json, os

APP_ENVIRONMENT = os.getenv('APP_ENVIRONMENT')

NIC_SMS_OTP_TEMPLATE_ID = ''
CDAC_SMS_USERNAME = ''
CDAC_SMS_SDID = ''
CDAC_SMS_SECURE_KEY = ''
SMS_SERVICES_OPTION_1 = 'NIC'
SMS_SERVICES_OPTION_2 = 'CDAC'

class RabbitmqRegisterFamily:
    def __init__(self):
        try:
            self.credentials = pika.PlainCredentials(os.getenv('RABBITMQ_ACCOUNTS_USERNAME'),os.getenv('RABBITMQ_ACCOUNTS_PASSWORD'))
            self.accounts_connection = pika.BlockingConnection(pika.ConnectionParameters(os.getenv('RABBITMQ_ACCOUNTS_HOST'),os.getenv('RABBITMQ_ACCOUNTS_PORT'),os.getenv('RABBITMQ_ACCOUNTS_VHOST'), self.credentials))
            self.accounts_channel = self.accounts_connection.channel()
            print(self.accounts_channel)
        except Exception as e:
            print(str(e))
            return None

    def createUser(self, data):
        try:
            exchange_name = 'Create_User_Xchange';
            routing_key = 'Create_NonDemo_User_' + APP_ENVIRONMENT;
            print(routing_key)
            body = data
            self.accounts_channel.basic_publish(exchange=exchange_name,
                                       routing_key=routing_key,
                                       body=json.dumps(body),
                                       properties=pika.BasicProperties(
                                           delivery_mode=2,  # makes persistent job
                                           priority=0,  # default priority
                                           #timestamp=int(timestamp),  # timestamp of job creation
                                           #expiration='6000'
                                           ))
            return {'status': 1, 'msg': 'Request Sent'}
        except Exception as e:
            print(str(e))
            return {'status': 0, 'msg': 'Connection Error!'}

    def send_sms(self, to, msg, sms_provider, sms_type='singlemsg', object_id='',
                 key='', template_id=NIC_SMS_OTP_TEMPLATE_ID):
        try:
            sms_queue_type = 'OTP' if sms_type == 'otpmsg' else 'REG'
            exchange_name = 'SMS_Xchange';
            routing_key = sms_provider + '_SMS_' + sms_queue_type + '_' + APP_ENVIRONMENT;
            body = {'phone_number': to,
                    'content': msg,
                    'sms_type': sms_type,
                    'id': str(object_id),
                    'key': key,
                    'template_id': template_id}
            
            self.accounts_channel.basic_publish(exchange=exchange_name,
                                       routing_key=routing_key,
                                       body=json.dumps(body),
                                       properties=pika.BasicProperties(
                                           delivery_mode=2,  # makes persistent job
                                           priority=0,  # default priority
                                           ))
            return {'status': 1, 'msg': 'SMS SENT'}
        except Exception as e:
            print(str(e))
            return {'status': 0, 'msg': 'Connection Error!'}


    def __del__(self):
        try:
            self.accounts_connection.close()
        except Exception as e:
            print(str(e))
            return None

