from utils.base64_parsing import base64_encode
import requests
import logging
import json
from datetime import date
import config


def create_template(customer_id, name, crn, lender_id, param, kyc_type):
    is_solar = param.get("is_solar", True) if isinstance(param, dict) else True
    if kyc_type == int(config.DIGIO_TWO_WAY_VKYC):
        status, auth_key = base64_encode(config.CF_DIGIO_CLIENT_ID + ':' + config.CF_DIGIO_CLIENT_SECRET)
        headers = {'Authorization': 'Basic ' + auth_key, 'Content-type': 'application/json'}
        template_name = config.CF_DIGIO_VKYC_WORKFLOW if is_solar else config.CF_DIGIO_NON_SOLAR_VKYC_WORKFLOW
        request_details = {
            "Father_Name": param.get("father_name"),
            "Email_Id": param.get("email_id"),
            "Current_location_and_city": param.get("current_location_and_city"),
            "Loan_purpose": param.get("loan_purpose"),
            "Loan_amount": param.get("loan_amount"),
            "EMI_amount": param.get("emi_amount"),
            "Today_Date": date.today().strftime("%Y-%m-%d"),
        }
        if is_solar and param.get("installation_address"):
            request_details["Installation_address"] = param.get("installation_address")

        payload = {
            "customer_identifier": customer_id,
            "customer_name": name,
            "request_details": request_details,
            "reference_id": crn,
            "template_name": template_name,
            "notify_customer": "false",
            "generate_access_token": "true",
        }
        logging.info('request: ' + json.dumps(payload))
    elif lender_id != int(config.P2P_LENDER_ID):
        status, auth_key = base64_encode(config.CF_DIGIO_CLIENT_ID + ':' + config.CF_DIGIO_CLIENT_SECRET)
        headers = {'Authorization': 'Basic ' + auth_key, 'Content-type': 'application/json'}
        payload = {"customer_identifier": customer_id,
                   "customer_name": name,
                   "reference_id": crn,
                   "template_name": config.CF_DIGIO_WORKFLOW,
                   "notify_customer": 'false',
                   "generate_access_token": 'true'}
        logging.info('request: ' + json.dumps(payload))
    else:
        status, auth_key = base64_encode(config.P2P_DIGIO_CLIENT_ID + ':' + config.P2P_DIGIO_CLIENT_SECRET)
        headers = {'Authorization': 'Basic ' + auth_key, 'Content-type': 'application/json'}
        payload = {"customer_identifier": customer_id,
                   "customer_name": name,
                   "reference_id": crn,
                   "template_name": config.P2P_DIGIO_WORKFLOW,
                   "notify_customer": 'false',
                   "generate_access_token": 'true'}
        logging.info('request: ' + json.dumps(payload))
    print(payload)
    response = requests.post(config.TEMPLATE_URL, headers=headers, json=payload)
    response_data = response.json()
    logging.info('response: ' + json.dumps(response_data))
    return response_data


def regenerate_token(id, lender_id):
    if lender_id != int(config.P2P_LENDER_ID):
        status, auth_key = base64_encode(config.CF_DIGIO_CLIENT_ID + ':' + config.CF_DIGIO_CLIENT_SECRET)
    else:
        status, auth_key = base64_encode(config.P2P_DIGIO_CLIENT_ID + ':' + config.P2P_DIGIO_CLIENT_SECRET)
    headers = {'Authorization': 'Basic ' + auth_key, 'Content-type': 'application/json'}
    payload = {"entity_id": id}
    logging.info('request: ' + json.dumps(payload))
    response = requests.post(config.TOKEN_URL, headers=headers, json=payload)
    response_data = response.json()
    logging.info('response: ' + json.dumps(response_data))
    return response_data
