#!/usr/bin/env python

import zulip
from typing import Any, List, Dict

welcome_text = 'Hello {}, Welcome to Zulip!\n \
* The first thing you should do is to install the development environment. \
We recommend following the vagrant setup as it is well documented and used \
by most of the contributors. If you face any trouble during installation \
post it in #development help\n \
* If you have not used Zulip before play with Zulip for a while. See how \
is Zulip diffrent from other chat applications. We would love to hear your \
first impressions about Zulip. Post it in #feedback . We have mobile apps \
as well as desktop apps too. You should try them too if you have time :)\n \
* Once you are familarized with Zulip a bit you can start contributing. Some \
of the main projects you can contribute to are Zulip  \
[server](https://github.com/zulip/zulip),  \
[mobile app](https://github.com/zulip/zulip-mobile),  \
[desktop](https://github.com/zulip/zulip-electron) app etc. We even have \
a [bot](https://github.com/zulip/zulipbot) that you can contribute to!!\n \
* We host our source code on GitHub. If you are not familiar with Git or \
GitHub checkout [this](http://zulip.readthedocs.io/en/latest/git-guide.html) \
guide. You don\'t have to learn everything but please go through it and learn \
the basics. We are here to help you if you are having any trouble. Post your \
questions in #git help . \
* Once you have completed these steps you can start contributing. You \
should start with issues labelled \
[bite-size](https://github.com/zulip/zulip/issues?q=is%3Aissue+is%3Aopen+label%3A). \
Bite-size issues are meant for new contributors and can be solved easily as \
compared to other issues. Currently we have bite-size labels only in Zulip server \
but if you want a bite size issue for mobile or electron feel free post in #mobile \
or #electron .\n \
* Solving the first issue can be difficult. The key is to not give up. If you spend \
enough time on the issue you should be able to solve it no matter what.\n \
* Use `grep` command when you can\'t figure out what files to change :) For example \
if you want know what files to modify in order to change Invite more users to Add \
more users which you can see below the user status list, grep for "Invite more \
users" in terminal.\n \
* If you are stuck with something and can\'t figure out what to do you can ask \
for help in #development help . But make sure that you tried your best to figure \
out the issue by yourself\n \
* If you are here for #Outreachy 2017-2018 or #GSoC don\'t worry much about \
whether you will get selected or not. You will learn a lot contributing to \
Zulip in course of next few months and if you do a good job at that you \
will get selected too :)\n \
* Most important of all welcome to the Zulip family :octopus:'

# These streams will cause the message to be sent
streams_to_watch = ['new members']

# These streams will cause anyone who sends a message there to be removed from the watchlist
streams_to_cancel = ['development help']

def get_watchlist():
    # type: () -> List[Any]
    storage = client.get_storage()
    return list(storage['storage'].values())

def set_watchlist(watchlist):
    # type: (List[str]) -> None
    client.update_storage({'storage': dict(enumerate(watchlist))})

def handle_event(event):
    # type: (Dict[str, Any]) -> None
    try:
        if event['type'] == 'realm_user' and event['op'] == 'add':
            watchlist = get_watchlist()
            watchlist.append(event['person']['email'])
            set_watchlist(watchlist)
            return
        if event['type'] == 'message':
            stream = event['message']['display_recipient']
            if stream not in streams_to_watch and stream not in streams_to_cancel:
                return
            watchlist = get_watchlist()
            if event['message']['sender_email'] in watchlist:
                watchlist.remove(event['message']['sender_email'])
                if stream not in streams_to_cancel:
                    client.send_message({
                        'type': 'private',
                        'to': event['message']['sender_email'],
                        'content': welcome_text.format(event['message']['sender_short_name'])
                    })
                set_watchlist(watchlist)
                return
    except Exception as err:
        print(err)


def start_event_handler():
    # type: () -> None
    print("Starting event handler...")
    client.call_on_each_event(handle_event, event_types=['realm_user', 'message'])

client = zulip.Client()
start_event_handler()
