from .client import Client

class Campaign:
    def __init__(self, client: Client):
        self.client = client

    def get_templates(self):
        """
        Get the templates
        """
        return self.client.get("GetTemplates")
    
    def update_template(self, template_id, description=None, caller_id=None, incoming_policy=None, customer_default=None, max_acive_channels=None, max_bridged_channels=None, originate_timeout=None, vm_detect=None, filter_enabled=None, max_dail_attempts=None, redial_wait=None, redail_policy=None, yemot_context=None, bridge_to=None, play_private_msg=None, remove_request=None):
        """
        Update the template
        """
        
        if not isinstance(template_id, int):
            return "The template_id must be a integer"
        
        if incoming_policy not in [None, 'OPEN', 'BLACKLIST', 'WHITELIST', 'BLOCKED']:
            return "The incoming_policy must be one of the following: 'OPEN', 'BLACKLIST', 'WHITELIST', 'BLOCKED'"
        
        if customer_default not in [None, '0', '1']:
            return "The customer_default must be one of the following: '0', '1'"
        
        if max_acive_channels and not isinstance(max_acive_channels, int):
            return "The max_acive_channels must be a integer"
        
        if max_bridged_channels and not isinstance(max_bridged_channels, int):
            return "The max_bridged_channels must be a integer"
        
        if originate_timeout and not isinstance(originate_timeout, int):
            return "The originate_timeout must be a integer"
        
        if vm_detect not in [None, '0', '1']:
            return "The vm_detect must be one of the following: '0', '1'"
        
        if filter_enabled not in [None, '0', '1']:
            return "The filter_enabled must be one of the following: '0', '1'"
        
        if max_dail_attempts and not isinstance(max_dail_attempts, int):
            return "The max_dail_attempts must be a integer"
        
        if redial_wait and not isinstance(redial_wait, int):
            return "The redial_wait must be a integer"
        
        if redail_policy not in [None, 'NONE', 'CONGESTIONS', 'FAILED']:
            return "The redail_policy must be one of the following: 'NONE', 'CONGESTIONS', 'FAILED'"
        
        if yemot_context not in [None, 'SIMPLE', 'REPEAT', 'MESSAGE', 'VOICEMAIL', 'BRIDGE']:
            return "The yemot_context must be one of the following: 'SIMPLE', 'REPEAT', 'MESSAGE', 'VOICEMAIL', 'BRIDGE'"
        
        if bridge_to and not isinstance(bridge_to, int):
            return "The bridge_to must be a integer"
        
        if play_private_msg not in [None, '0', '1']:
            return "The play_private_msg must be one of the following: '0', '1'"
        
        if remove_request not in [None, 'SILENT', 'WITH_MESSAGE']:
            return "The remove_request must be one of the following: 'SILENT', 'WITH_MESSAGE'"
        
        data = {
            "templateId": template_id,
            "description": description,
            "callerId": caller_id,
            "incomingPolicy": incoming_policy,
            "customerDefault": customer_default,
            "maxActiveChannels": max_acive_channels,
            "maxBridgedChannels": max_bridged_channels,
            "originateTimeout": originate_timeout,
            "vmDetect": vm_detect,
            "filterEnabled": filter_enabled,
            "maxDailAttempts": max_dail_attempts,
            "redialWait": redial_wait,
            "redailPolicy": redail_policy,
            "yemotContext": yemot_context,
            "bridgeTo": bridge_to,
            "playPrivateMsg": play_private_msg,
            "removeRequest": remove_request
        }
        return self.client.post("UpdateTemplate", data=data)
    
    def upload_template_file(self, file, name, type, convertAudio=None):
        """
        Upload the template file
        """
        if name == None:
            name = "Default"
        if type not in ['VOICE', 'SMS', 'BRIDGE', 'PRIVATE_FIRST', 'PRIVATE_MSG']:
            return "The type must be one of the following: 'VOICE', 'SMS', 'BRIDGE' - for a msg befor bridge, 'PRIVATE_FIRST', 'PRIVATE_MSG'"
        
        if not file:
            return "The file is required"
        
        if type == 'VOICE':
            path = f'{name}.wav'
        if type == 'SMS':
            path = f'{name}.tts'
        if type == 'BRIDGE':
            path = f'{name}-MoreInfo.wav'
        if type == 'PRIVATE_FIRST':
            path = f'{name}-First.wav'
        if type == 'PRIVATE_MSG':
            path = f'PrivateMsg/{name}.wav'
        
        files = {
            "file": open(file, "rb")
        }
        
        return self.client.post("UploadFile", data={"path": path, "convertAudio": convertAudio}, files=files)
    
    def downlaoad_template_file(self, name, type):
        """
        Download the template file
        """
        if name == None:
            name = "Default"
        if type not in ['VOICE', 'SMS', 'BRIDGE', 'PRIVATE_FIRST', 'PRIVATE_MSG']:
            return "The type must be one of the following: 'VOICE', 'SMS', 'BRIDGE' - for a msg befor bridge, 'PRIVATE_FIRST', 'PRIVATE_MSG'"
        
        if type == 'VOICE':
            path = f'{name}.wav'
        if type == 'SMS':
            path = f'{name}.tts'
        if type == 'BRIDGE':
            path = f'{name}-MoreInfo.wav'
        if type == 'PRIVATE_FIRST':
            path = f'{name}-First.wav'
        if type == 'PRIVATE_MSG':
            path = f'PrivateMsg/{name}.wav'
        
        return self.client.get("DownloadFile", {"path": path})
    
    

        