from abc import ABC

from idcloud.configuration import Configuration
from idcloud.enums.status import Status
from idcloud.requestor import Requestor
from idcloud.responses.info import Info
from idcloud.responses.scenario import Scenario


class BaseResource(ABC):
    def __init__(self, requestor: Requestor, configuration: Configuration):
        self._requestor = requestor
        self._configuration = configuration

    @staticmethod
    def _fill_content(**kwargs) -> dict:
        values = {}
        for k in kwargs:
            if kwargs[k] is not None:
                values[k] = kwargs[k]
        return values

    @property
    def requestor(self) -> Requestor:
        return self._requestor

    @property
    def configuration(self) -> Configuration:
        return self._configuration

    def info(self) -> Info:
        response_snapshot = self.requestor.request('GET', '/scs/v1/info')

        return Info(
            version=response_snapshot.response_body.get('version'),
            description=response_snapshot.response_body.get('description'),
        )

    def get_scenario(self, scenario_id: str):
        response_snapshot = self.requestor.request('GET', f'/scs/v1/scenarios/{scenario_id}')

        return Scenario(
            id=response_snapshot.response_body.get('id'),
            name=response_snapshot.response_body.get('name'),
            status=Status(response_snapshot.response_body.get('status').lower()),
            steps=response_snapshot.response_body['state'].get('steps'),
            result=response_snapshot.response_body['state'].get('result'),
            raw_request={
                'body': response_snapshot.request_body,
                'headers': response_snapshot.request_headers
            },
            raw_response={
                'body': response_snapshot.response_body,
                'headers': response_snapshot.response_headers
            }
        )
