import requests, json
from mattermostdriver import Driver
import bridges

channels = {
    "zc69ns9jwpbn7c3wkxdf6x8nto": 1125968295967850559, #comforter
    "ap61bgmm63f8irths1gk91zowh": 954201387770736754, # lounge
    #"s6muherhotfoircc1yzmwr5wty": 1119502004721557554,  # kweh
    "fkcqa3qj83gu3bfikcu55sfwww": 1367978276185964584 #misskey
    #"na4doo5f83ykbc45m9a5dn513a": 1167176429797113926, #photos-from-another-star
    #"9ydcz9orepbtmedncb7idh43hr": 1119508652844404816, #bulletin-board
    #"81qmzfzeeif7mmfhpy7hkxnjuc": 955394194766192690, #photos-of-the-gang
    #"u1ffegpqj3gg7yephh6rnso74o": 1381391455574167653 #stream
}

webhooks = {
    1125968295967850559: "https://discord.com/api/webhooks/1153490247905189909/pm_cDa8XqsTIlAsZY1uUFmSSvDZKjaI8u30mjFzmvHcMhMQFirQcWbI9rEee4OFmLNnu",
    954201387770736754: "https://discord.com/api/webhooks/1127805939425235029/c4RvvRDUfDuE0-cvTzlPQPYbaTN6UdZzEPfLypOQCGSejax93Gh99E3_xgbCirrh4CqH"
}

post_webhooks_exemptions = ["u1ffegpqj3gg7yephh6rnso74o", "fkcqa3qj83gu3bfikcu55sfwww"]

users = {
    "f3nja8t9fpy73cxeh5ykzrozaw": 407247496008433675,
    "3byr3scix3f78xs5bpmgqzc6pc": 189202462442389514,
    "kr54p4xupt8kxj1s1c39be4w3r": 1183070660621250702
}

async def event_handler(event):
    event = json.loads(event)
    if 'event' in event.keys() and event['event'] == "posted":
        print("event:", event)
        event_data = event['data']
        post = event_data['post']
        
        post = json.loads(post)
        if post['channel_id'] in channels.keys():
            print(f"watched channel {post['channel_id']}")
            if 'from_webhook' in post['props'].keys():
                is_webhook = post['props']['from_webhook']
            else:
                is_webhook = "false"
            print(is_webhook)
            if is_webhook != "true":
                # add file syncing means you need to get the "file_ids" key from the post and then download them into blobs and pass those along
                # to the request as files, on the discord side those files would then get attached to the webhook.
                discord_channel_id = channels[post['channel_id']]
                print(discord_channel_id)
                user = mattermostDriver.users.get_user(user_id=post['user_id'])
                #print(user)
                post['user'] = user
                discord_id = None
                print("user:", user)
                avatar_url = ""
                if user['id'] in users.keys():
                    discord_user_id = users[post['user']['id']]
                    status, username, avatar_url = bridges.getDiscordUserProfile(discord_user_id)
                    post['user']['nickname'] = username
                    print(avatar_url)

                post['discord_id'] = discord_id
                print(webhooks[discord_channel_id])
                bridges.send_mattermost_to_discord(post, webhook_url=webhooks[discord_channel_id], avatar_url=avatar_url)
            elif post['channel_id'] in post_webhooks_exemptions and is_webhook == "true":
                print("exempt channel!")
                username = post['props']['override_username']
                avatar_url = post['props']['override_icon_url']
                user = {"nickname": username}
                post['user'] = user
                post['discord_id'] = None
                bridges.send_mattermost_to_discord(post, webhook_url=webhooks[discord_channel_id], avatar_url=avatar_url)

mattermostDriver = Driver({
    "url": "192.168.1.67",
    "port": 8065,
    "scheme": "http",
    "token": "dmefeb8t7pditf3ot9377hrxch"
})

mattermostDriver.login()
mattermostDriver.init_websocket(event_handler)
