from application.domain import TableService
from model.customexception import InvalidTipError


class DoChangeTipInteractor(object):
    def __init__(self, table_service):
        # type: (TableService) -> None
        self.table_service = table_service

    def execute(self, pos_id, table_id, percentage, amount):
        # type: (str, str, str, str) -> str

        if (percentage != "" and percentage != "0") and (amount != "" and amount != "0"):
            raise InvalidTipError(code=InvalidTipError.ErrorCode.CANNOT_USE_TIP_BY_AMOUNT_AND_PERCENTAGE)
        elif percentage == "" and amount == "":
            raise InvalidTipError(code=InvalidTipError.ErrorCode.TIP_AMOUNT_MUST_BE_VALID)

        if percentage != "":
            return self.table_service.change_service_tip(pos_id, table_id, percentage)
        if amount != "":
            return self.table_service.change_service_tip(pos_id, table_id, '', amount)
