import json

from idcloud.enums.capture_method import CaptureMethod
from idcloud.enums.document_format import DocumentFormat
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 VerifyDocumentResource(BaseResource):
    NAME = 'Verify_Document'

    def initiate_document_verification(
        self,
        front: str,
        back: str = None,
        capture_method: CaptureMethod = None,
        document_type: DocumentType = None,
        document_size: DocumentSize = None,
        document_format: DocumentFormat = None
    ) -> Scenario:
        document = self._fill_content(
            front=front,
            back=back,
            captureMethod=capture_method,
            type=document_type,
            size=document_size,
            format=document_format
        )

        payload = {
            'name': self.NAME,
            'input': {'document': document}
        }
        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
        )
