import traceback
import time
import uuid
from fwd_api.celery_worker.worker import app
from fwd_api.models import SubscriptionRequest
from django.utils.crypto import get_random_string


def print_id(rq_id):
    print(" [x] Received {rq}".format(rq=rq_id))


def to_status(result):
    print('X')
    if 'status' in result and result['status'] not in [200, 201, 202]:
        return 4
    return 3


def update_user(rq: SubscriptionRequest):
    sub = rq.subscription
    predict_status = rq.status
    if predict_status == 3:
        from fwd_api.utils import ProcessUtil
        sub.current_token += ProcessUtil.token_value(int(rq.process_type))
        sub.save()

@app.task(name='process_sap_invoice_result')
def process_invoice_sap_result(rq_id, result):
    from fwd_api.models import SubscriptionRequest
    from fwd_api.constant.common import ProcessType

    print_id(rq_id)

    try:
        rq: SubscriptionRequest = \
            SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.INVOICE.value)[0]
        status = to_status(result)

        rq.predict_result = result
        rq.status = status
        rq.save()

        update_user(rq)
    except IndexError as e:
        print(e)
        print("NotFound request by requestId, %d", rq_id)
    except Exception as e:
        print(e)
        print("Fail Invoice %d", rq_id)
        return "FailInvoice"


@app.task(name='process_fi_invoice_result')
def process_invoice_fi_result(rq_id, result):
    from fwd_api.models import SubscriptionRequest
    from fwd_api.constant.common import ProcessType

    print_id(rq_id)
    print(result)
    try:
        rq: SubscriptionRequest = \
            SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.FI_INVOICE.value)[0]
        status = to_status(result)

        rq.predict_result = result
        rq.status = status
        rq.save()

        update_user(rq)
    except IndexError as e:
        print(e)
        print("NotFound request by requestId, %d", rq_id)
    except Exception as e:
        print(e)
        print("Fail Invoice %d", rq_id)
        return "FailInvoice"
    
@app.task(name='process_manulife_invoice_result')
def process_invoice_manulife_result(rq_id, result):
    from fwd_api.models import SubscriptionRequest
    from fwd_api.constant.common import ProcessType

    print_id(f"[DEBUG]: Received manulife request with id {rq_id}")
    try:
        rq: SubscriptionRequest = \
            SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.MANULIFE_INVOICE.value)[0]
        status = to_status(result)

        rq.predict_result = result
        rq.status = status
        rq.save()

        update_user(rq)
    except IndexError as e:
        print(e)
        print("NotFound request by requestId, %d", rq_id)
    except Exception as e:
        print(e)
        print("Fail Invoice %d", rq_id)
        return "FailInvoice"

@app.task(name='process_sbt_invoice_result')
def process_invoice_sbt_result(rq_id, result):
    from fwd_api.models import SubscriptionRequest
    from fwd_api.constant.common import ProcessType

    print_id(f"[DEBUG]: Received SBT request with id {rq_id}")
    print_id(f"[DEBUG]: result: {result}")
    try:
        rq: SubscriptionRequest = \
            SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.SBT_INVOICE.value)[0]
        status = to_status(result)

        rq.predict_result = result
        rq.status = status
        rq.save()
        update_user(rq)
    except IndexError as e:
        print(e)
        print("NotFound request by requestId, %d", rq_id)
    except Exception as e:
        print(e)
        print("Fail Invoice %d", rq_id)
        return "FailInvoice"


# @app.task(name='process_id_result', queue='id_card_rs')
# def process_id_result(rq_id, result):
#     from fwd_api.models import SubscriptionRequest
#     from fwd_api.constant.common import ProcessType
#     from fwd_api.models import SubscriptionRequestFile
#     from fwd_api.constant.common import FileCategory

#     print_id(rq_id)

#     try:
#         s_time = time.time()
#         print("Start")
#         j_time = time.time()
#         print("Json {}".format(j_time - s_time))

#         rq: SubscriptionRequest = \
#             SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.ID_CARD.value)[0]
        
#         if 'content' in result and 'pages' in result['content']:
#             pages = result['content']['pages']
#             if isinstance(pages, list):
#                 new_pages = []
#                 for idx, page in enumerate(pages):
#                     if 'path_image_croped' in page:
#                         img_name = f'crop_{idx}_{get_random_string(3)}.jpg'
#                         path = page['path_image_croped']
#                         rq_file: SubscriptionRequestFile = SubscriptionRequestFile(file_name=img_name, request=rq,
#                                                                                    file_category=FileCategory.CROP.value,
#                                                                                    file_path=path,
#                                                                                    code=f'IDC{uuid.uuid4().hex}')
#                         rq_file.save()
#                         page['path_image_croped'] = rq_file.code

#         l_time = time.time()
#         print("Save {}".format(l_time - j_time))

#         status = to_status(result)

#         rq.predict_result = result

#         rq.status = status
#         rq.save()

#         update_user(rq)
#         e_time = time.time()
#         print("End {}".format(e_time - l_time))

#     except IndexError as e:
#         traceback.format_exc()
#         print(e)
#     except Exception as e:
#         traceback.format_exc()
#         print(e)
#         print("Fail ID %d", rq_id)
#         return "Fail"

#     return "Success"


# @app.task(name='process_driver_license_result')
# def process_driver_license_result(rq_id, result):
#     from fwd_api.models import SubscriptionRequest
#     from fwd_api.models import SubscriptionRequestFile
#     from fwd_api.constant.common import FileCategory
#     from fwd_api.constant.common import ProcessType

#     print_id(rq_id)
#     try:
#         rq: SubscriptionRequest = \
#             SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.DRIVER_LICENSE.value)[0]

#         if 'content' in result and 'pages' in result['content']:
#             pages = result['content']['pages']
#             if isinstance(pages, list):
#                 new_pages = []
#                 for idx, page in enumerate(pages):
#                     if 'path_image_croped' in page:
#                         img_name = f'crop_{idx}_{get_random_string(3)}.jpg'
#                         path = page['path_image_croped']
#                         rq_file: SubscriptionRequestFile = SubscriptionRequestFile(file_name=img_name, request=rq,
#                                                                                    file_category=FileCategory.CROP.value,
#                                                                                    file_path=path,
#                                                                                    code=f'DLC{uuid.uuid4().hex}')
#                         rq_file.save()
#                         page['path_image_croped'] = rq_file.code
#         status = to_status(result)

#         rq.predict_result = result
#         rq.status = status
#         rq.save()

#         update_user(rq)

#     except IndexError as e:
#         print(e)
#     except Exception as e:
#         print(e)
#         print("Fail DL %d", rq_id)
#         return "Fail"

#     return "Success"


# @app.task(name='process_invoice_result')
# def process_invoice_result(rq_id, result):
#     from fwd_api.models import SubscriptionRequest
#     from fwd_api.constant.common import ProcessType

#     print_id(rq_id)
#     try:
#         rq: SubscriptionRequest = \
#             SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.INVOICE.value)
#         print(rq)
#         rq: SubscriptionRequest = \
#             SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.INVOICE.value)[0]
#         status = to_status(result)

#         rq.predict_result = result
#         rq.status = status
#         rq.save()

#         update_user(rq)
#     except IndexError as e:
#         print(e)
#         print("NotFound request by requestId, %d", rq_id)
#     except Exception as e:
#         print(e)
#         traceback.format_exc()
#         print("Fail Invoice %d", rq_id)
#         return "FailInvoice"

#     return "Success"


# @app.task(name='process_ocr_with_box_result')
# def process_ocr_with_box_result(rq_id, result):
#     from fwd_api.models import SubscriptionRequest
#     from fwd_api.constant.common import ProcessType

#     print_id(rq_id)

#     try:
#         rq: SubscriptionRequest = \
#             SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.OCR_WITH_BOX.value)[0]
#         status = to_status(result)

#         rq.predict_result = result
#         rq.status = status
#         rq.save()

#         update_user(rq)
#     except IndexError as e:
#         traceback.format_exc()
#         print(e)
#     except Exception as e:
#         traceback.format_exc()
#         print(e)
#         print("Fail OCR %d", rq_id)
#         return "FailOCR"

#     return "Success"


# @app.task(name='process_template_matching_result')
# def template_matching_result(rq_id, result, align_img):
#     from fwd_api.models import SubscriptionRequest
#     from fwd_api.constant.common import ProcessType
#     from fwd_api.constant.common import FileCategory
#     from fwd_api.models import SubscriptionRequestFile

#     print_id(rq_id)
#     try:
#         rq: SubscriptionRequest = \
#             SubscriptionRequest.objects.filter(request_id=rq_id, process_type=ProcessType.TEMPLATE_MATCHING.value)[0]

#         if align_img:
#             from fwd_api.constant.common import IMAGE_NAME
#             rq_file: SubscriptionRequestFile = SubscriptionRequestFile(file_name=IMAGE_NAME, request=rq,
#                                                                        file_category=FileCategory.CROP.value,
#                                                                        file_path=align_img)
#             rq_file.save()
#         status = to_status(result)

#         rq.predict_result = result
#         rq.status = status
#         rq.save()

#         update_user(rq)

#     except IndexError as e:
#         traceback.format_exc()
#         print(e)
#     except Exception as e:
#         traceback.format_exc()
#         print(e)
#         print("Fail Template %d", rq_id)
#         return "FailTemplate"

#     return "Success"
