import json
from abc import ABC, abstractmethod
from lib.common import rest_client
from lib.services.api_gateway.base_client import BaseClient


class ProjectsClient(BaseClient):
    """Service client for API Gateway Projects resource."""

    def __init__(self, auth_provider, service, region, client=None, **kwargs):
        super(ProjectsClient, self).__init__(auth_provider, service, region, **kwargs)
        self.client = client

    def _parse_response(self, resp, body, schema):
        """Parse and validate response."""
        if body:
            body = json.loads(body)
        else:
            body = {}
        self.validate_response(schema, resp, body)
        return rest_client.ResponseBody(resp, body)

    def show_project(self):
        """Show project settings."""
        url = self.client.get_project_path(self.project_name)
        resp, body = self.get(url)
        return self._parse_response(
            resp, body, self.client.get_schema().show_project
        )

    def update_project(self, **kwargs):
        """Update project settings."""
        url = self.client.get_project_path(self.project_name)
        patch_body = json.dumps(kwargs)
        resp, body = self.patch(url, patch_body)
        return self._parse_response(
            resp, body, self.client.get_schema().update_project
        )


class ProjectsClientInterface(ABC):
    """Interface for cloud-specific implementations."""

    @abstractmethod
    def get_schema(self):
        """Return response schemas module."""
        pass

    @abstractmethod
    def get_project_path(self, project):
        """Return API endpoint path."""
        pass
