import json
from typing import Tuple

from requests import Response, Session
from requests.exceptions import HTTPError

from .config_helper import ConfigHelper


class EnterpriseHelper:
    def __init__(self, session: Session):
        self.session = session

    def get_enterprises(self) -> Tuple[dict, Response]:
        """Get all enterprises."""

        content = {}

        try:
            r = self.session.get(f"{ConfigHelper.host}/enterprises")
            r.raise_for_status()
        except HTTPError as e:
            pass
        else:
            content = json.loads(r.content.decode("utf-8"))
        finally:
            ConfigHelper.show_status("    Get Enterprises", r)

        return content, r

    def post_enterprise(self, name: str) -> Tuple[dict, Response]:
        """Create an enterprise."""

        content = {"name": name}

        try:
            r = self.session.post(f"{ConfigHelper.host}/enterprises", json=content)
            r.raise_for_status()
        except HTTPError as e:
            pass
        else:
            content = json.loads(r.content.decode("utf-8"))
        finally:
            ConfigHelper.show_status("    Post Enterprise", r)

        return content, r

    def get_enterprise(self, id: int) -> Tuple[dict, Response]:
        """Get a specific enterprise."""

        content = {}

        try:
            r = self.session.get(f"{ConfigHelper.host}/enterprises/{id}")
            r.raise_for_status()
        except HTTPError as e:
            pass
        else:
            content = json.loads(r.content.decode("utf-8"))
        finally:
            ConfigHelper.show_status("    Get Enterprise", r)

        return content, r

    def put_enterprise(self, id: int, name: str) -> Tuple[dict, Response]:
        """Update an enterprise."""

        content = {"name": name}

        try:
            r = self.session.put(f"{ConfigHelper.host}/enterprises/{id}", json=content)
            r.raise_for_status()
        except HTTPError as e:
            pass
        finally:
            ConfigHelper.show_status("    Put Enterprise", r)

        return {}, r

    def delete_enterprise(self, id: int) -> Tuple[dict, Response]:
        """Delete an enterprise."""

        try:
            r = self.session.delete(f"{ConfigHelper.host}/enterprises/{id}")
            r.raise_for_status()
        except HTTPError as e:
            pass
        finally:
            ConfigHelper.show_status("    Delete Enterprise", r)

        return {}, r

    def get_enterprise_users(self, id: int) -> Tuple[dict, Response]:
        """Get the users of an enterprise if the user is part of the enterprise"""

        content = {}

        try:
            r = self.session.get(f"{ConfigHelper.host}/enterprises/{id}/users")
            r.raise_for_status()
        except HTTPError as e:
            pass
        else:
            content = json.loads(r.content.decode("utf-8"))
        finally:
            ConfigHelper.show_status("    Get Enterprise Users", r)

        return content, r

    def get_enterprise_rooms(self, id: int) -> Tuple[dict, Response]:
        """Get all rooms for an entreprise (needs to be an admin user)."""

        content = {}

        try:
            r = self.session.get(f"{ConfigHelper.host}/enterprises/{id}/rooms")
            r.raise_for_status()
        except HTTPError as e:
            pass
        else:
            content = json.loads(r.content.decode("utf-8"))
        finally:
            ConfigHelper.show_status("    Get Enterprise Rooms", r)

        return content, r

    def get_enterprise_factors(self, enterprise: int) -> Tuple[dict, Response]:
        """Get all factors with selected factors of an enterprise."""

        content = {}

        try:
            r = self.session.get(
                f"{ConfigHelper.host}/enterprises/{enterprise}/factors"
            )
            r.raise_for_status()
        except HTTPError as e:
            pass
        else:
            content = json.loads(r.content.decode("utf-8"))
        finally:
            ConfigHelper.show_status("    Get Enterprise Factors", r)

        return content, r

    def put_entreprise_factors(self, enterprise: int) -> Tuple[dict, Response]:
        """Purge and recreate selected factors of an enterprise."""
        raise NotImplementedError("Not implemented yet")
