from ludialudom.errors import SignatureError
from ludialudom.resources.base import BaseResource


class Project(BaseResource):
    def activate(self, project_code: int) -> bool:
        payload = {
            "projectCode": str(project_code),
            "sign": self.sign(str(project_code))
        }

        response = self.requestor.request('POST', '/activateProject', payload=payload)

        if response.get('sign') != self.sign(f"{response.get('result')}{response.get('code')}"):
            raise SignatureError()

        return 'OK' == response.get('result')

    def deactivate(self, project_code: int) -> bool:
        payload = {
            "projectCode": str(project_code),
            "sign": self.sign(str(project_code))
        }

        response = self.requestor.request('POST', '/deactivateProject', payload=payload)

        if response.get('sign') != self.sign(f"{response.get('result')}{response.get('code')}"):
            raise SignatureError()

        return 'OK' == response.get('result')

    def is_active(self, project_code: int) -> bool:
        payload = {
            "projectCode": str(project_code),
            "sign": self.sign(str(project_code))
        }

        response = self.requestor.request('POST', '/getProjectStatus', payload=payload)
        response_sign = self.sign(f"{response.get('result')}{response.get('code')}{response.get('status')}")

        if response.get('sign') != response_sign:
            raise SignatureError()

        return 'OK' == response.get('result')
