import json

from idcloud.enums.capture_method import CaptureMethod
from idcloud.enums.document_size import DocumentSize
from idcloud.enums.document_type import DocumentType
from idcloud.enums.status import Status
from idcloud.resources.base import BaseResource
from idcloud.responses.scenario import Scenario
from idcloud.utils import JSONEncoder


class ConnectVerifyDocumentResource(BaseResource):
    NAME = 'Connect_Verify_Document'

    def initiate_document_verification(
        self,
        front_white_image: str = None,
        back_white_image: str = None,
        front_ir_image: str = None,
        back_ir_image: str = None,
        front_uv_image: str = None,
        back_uv_image: str = None,
        capture_method: CaptureMethod = None,
        document_type: DocumentType = None,
        document_size: DocumentSize = None
    ) -> Scenario:
        input_data = self._fill_content(
            frontWhiteImage=front_white_image,
            backWhiteImage=back_white_image,
            frontIRImage=front_ir_image,
            backIRImage=back_ir_image,
            frontUVImage=front_uv_image,
            backUVImage=back_uv_image,
            captureMethod=capture_method,
            type=document_type,
            size=document_size
        )

        payload = {
            'name': self.NAME,
            'input': input_data
        }
        response = self.requestor.request('POST', '/scs/v1/scenarios', payload=payload)

        return Scenario(
            id=response.get('id'),
            name=response.get('name'),
            status=Status(response.get('status').lower()),
            steps=response['state'].get('steps'),
            result=response['state'].get('result'),
            raw_request=json.loads(json.dumps(payload, cls=JSONEncoder)),
            raw_response=response
        )

    def add_image(
        self,
        scenario_id: str,
        front_white_image: str = None,
        back_white_image: str = None,
        front_ir_image: str = None,
        back_ir_image: str = None,
        front_uv_image: str = None,
        back_uv_image: str = None,
    ) -> Scenario:
        input_data = self._fill_content(
            frontWhiteImage=front_white_image,
            backWhiteImage=back_white_image,
            frontIRImage=front_ir_image,
            backIRImage=back_ir_image,
            frontUVImage=front_uv_image,
            backUVImage=back_uv_image
        )

        if len(list(input_data.keys())) != 1:
            raise Exception('Connect verify document patch method allows just one image argument.')

        payload = {
            'name': self.NAME,
            'input': input_data
        }

        response = self.requestor.request(
            'PATCH', f'/scs/v1/scenarios/{scenario_id}/state/steps/{list(input_data.keys())[0]}',
            payload=payload
        )

        return Scenario(
            id=response.get('id'),
            name=response.get('name'),
            status=Status(response.get('status').lower()),
            steps=response['state'].get('steps'),
            result=response['state'].get('result'),
            raw_request=json.loads(json.dumps(payload, cls=JSONEncoder)),
            raw_response=response
        )

    def verify_results(self, scenario_id: str):
        payload = {
            'name': self.NAME
        }

        response = self.requestor.request(
            'PATCH', f'/scs/v1/scenarios/{scenario_id}/state/steps/verifyResults',
            payload=payload
        )

        return Scenario(
            id=response.get('id'),
            name=response.get('name'),
            status=Status(response.get('status').lower()),
            steps=response['state'].get('steps'),
            result=response['state'].get('result'),
            raw_request=json.loads(json.dumps(payload, cls=JSONEncoder)),
            raw_response=response
        )
