
class WebmailType(object):
    HORDE = 'horde'
    SMARTERMAIL = 'smwebmail'
    REMOTE_SMARTERMAIL = 'smremotewebmail'


def select_target_webmail(source_webmail, available_webmails=None, remote_smarter_mail=False):
    """Select webmail on the target server according to webmail on the source and list of available webmails on target 
    
    :type source_webmail: str | unicode | None 
    :type available_webmails: list[str | unicode] | None 
    :type remote_smarter_mail: bool 
    :rtype: str | unicode | None 
    """
    if available_webmails is None:
        return source_webmail

    if source_webmail is None or source_webmail == 'none':
        return source_webmail

    webmail = source_webmail

    if webmail == 'true':
        webmail = WebmailType.HORDE
    elif remote_smarter_mail and webmail == WebmailType.SMARTERMAIL:
        webmail = WebmailType.REMOTE_SMARTERMAIL
    elif not remote_smarter_mail and webmail == WebmailType.REMOTE_SMARTERMAIL:
        webmail = WebmailType.SMARTERMAIL

    if webmail not in available_webmails:
        if len(available_webmails) > 0:
            webmail = available_webmails[0]
        else:
            webmail = None

    return webmail
