import argparse
import asyncio
import json
import os
import shutil
import subprocess
import sys
import time
from queue import Queue
from threading import Thread

import aiohttp
from gidgetlab.aiohttp import GitLabBot

import gitlab
from nittymcpick.cls.job import Job
from nittymcpick.cls.linter import Linter

_args = None
bot = GitLabBot("nittymcpick")
sgl = None
_linter = []
job_queue = Queue()

def create_args():
    parser = argparse.ArgumentParser(
        prog="nittymcpick", description='Your friendly linting bot for gitlab')
    parser.add_argument("--token", default=os.getenv("GL_ACCESS_TOKEN"),
                        help="Access token to use (default:GL_ACCESS_TOKEN from environment)")
    parser.add_argument("--onlynew", default=False, action="store_true",
                        help="Comment only on changes (default:false)")
    parser.add_argument("--nowip", default=False, action="store_true", help="Ignore WIP merge requests (default:false)")
    parser.add_argument("--host", default="127.0.0.1",
                        help="IP to bind to (default:127.0.0.1)")
    parser.add_argument("--port", type=int, default=8888,
                        help="Port to bind to (default:8888)")
    parser.add_argument("gitlab", help="Url of the gitlab server. E.g. http://foo.bar.corp.com")
    parser.add_argument("botname", help="Username of the bot in GitLab")
    parser.add_argument("config", help="config file")
    return parser.parse_args()

def queue_runner():
    while True:
        if not job_queue.empty():
            x = job_queue.get()
            print("Run Job for: {}".format(x))
            try:
                x.Run()
            except Exception as e:
                print("Job {} failed: {}".format(x, e))
        else:
            time.sleep(0.5)

@bot.router.register("Merge Request Hook")
async def merge_request_event(event, gl, *args, **kwargs):
    x = Job(event, gl, _args, sgl, _linter)
    print("Add Job for: {}".format(x))
    job_queue.put(x)

if __name__ == "__main__":
    # Create all the needed global objects
    _args = create_args()
    if not _args.token:
        raise ValueError("Access token is not set - Can't proceed")
    sgl = gitlab.Gitlab(_args.gitlab, private_token=_args.token)
    _linter = Linter.SetupLinter(_args)
    # Start job queue
    worker = Thread(target=queue_runner)
    worker.setDaemon(True)
    worker.start()
    # Start webhook server
    bot.run(host=_args.host, port=_args.port)
