#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import logging
import os
import sys
from argparse import ArgumentParser

from .worker import ManagerWorker
from . import __version__

logger = logging.getLogger("pyqs")


def main():
    parser = ArgumentParser(description="""
Run PyQS workers for the given queues
""")
    parser.add_argument(
        "-c",
        "--concurrency",
        type=int,
        dest="concurrency",
        default=1,
        help='Worker concurrency',
        action="store",
    )

    parser.add_argument(
        "queues",
        metavar="QUEUE_NAME",
        nargs="+",
        type=str,
        help='Queues to process',
        action="store",
    )

    parser.add_argument(
        "--loglevel",
        "--log_level",
        "--log-level",
        dest="logging_level",
        type=str,
        default="WARN",
        help=(
            'Set logging level. '
            'This must be one of the python default logging levels'
        ),
        action="store",
    )

    parser.add_argument(
        "--access-key-id",
        dest="access_key_id",
        type=str,
        default=None,
        help='AWS_ACCESS_KEY_ID used by Boto',
        action="store",
    )

    parser.add_argument(
        "--secret-access-key",
        dest="secret_access_key",
        type=str,
        default=None,
        help='AWS_SECRET_ACCESS_KEY used by Boto',
        action="store",
    )

    parser.add_argument(
        "--region",
        dest="region",
        type=str,
        default=None,
        help='AWS Region to connect to SQS',
        action="store",
    )

    parser.add_argument(
        "--interval",
        dest="interval",
        type=float,
        default=0.0,
        help='Time waited by a worker after processesing a message.',
        action="store",
    )

    parser.add_argument(
        "--batchsize",
        dest="batchsize",
        type=int,
        default=10,
        help='How many messages to download at a time from SQS.',
        action="store",
    )

    parser.add_argument(
        "--prefetch-multiplier",
        dest="prefetch_multiplier",
        type=int,
        default=2,
        help=(
            'Multiplier on the size of the internal queue '
            'for prefetching SQS messages.'
        ),
        action="store",
    )

    args = parser.parse_args()

    _main(
        queue_prefixes=args.queues,
        concurrency=args.concurrency,
        logging_level=args.logging_level,
        region=args.region,
        access_key_id=args.access_key_id,
        secret_access_key=args.secret_access_key,
        interval=args.interval,
        batchsize=args.batchsize,
        prefetch_multiplier=args.prefetch_multiplier
    )


def _add_cwd_to_path():
    cwd = os.getcwd()
    if cwd not in sys.path:
        sys.path.insert(0, cwd)


def _main(queue_prefixes, concurrency=5, logging_level="WARN",
          region=None, access_key_id=None, secret_access_key=None,
          interval=1, batchsize=10, prefetch_multiplier=2):
    logging.basicConfig(
        format="[%(levelname)s]: %(message)s",
        level=getattr(logging, logging_level),
    )
    logger.info("Starting PyQS version {}".format(__version__))
    manager = ManagerWorker(
        queue_prefixes, concurrency, interval, batchsize,
        prefetch_multiplier=prefetch_multiplier, region=region,
        access_key_id=access_key_id, secret_access_key=secret_access_key,
    )
    _add_cwd_to_path()
    manager.start()
    manager.sleep()

@@ -186,16 +186,37 @@
 erWorker
+, SimpleManagerWorker
 %0Afrom . 
@@ -2372,24 +2372,164 @@
 re%22,%0A    )%0A%0A
+    parser.add_argument(%0A        '--simple-worker',%0A        dest='simple_worker',%0A        default=False,%0A        action='store_true'%0A    )%0A%0A
     args = p
@@ -2547,16 +2547,16 @@
 _args()%0A
-
 %0A    _ma
@@ -2913,16 +2913,58 @@
 ltiplier
+,%0A        simple_worker=args.simple_worker
 %0A    )%0A%0A
@@ -3259,16 +3259,47 @@
 iplier=2
+,%0A          simple_worker=False
 ):%0A    l
@@ -3482,16 +3482,43 @@
 ion__))%0A
+%0A    if simple_worker:%0A    
     mana
@@ -3523,16 +3523,22 @@
 nager = 
+Simple
 ManagerW
@@ -3544,16 +3544,230 @@
 Worker(%0A
+            queue_prefixes, concurrency, batchsize,%0A            region=region, access_key_id=access_key_id,%0A            secret_access_key=secret_access_key,%0A        )%0A    else:%0A        manager = ManagerWorker(%0A    
         
@@ -3808,32 +3808,36 @@
 val, batchsize,%0A
+    
         prefetch
@@ -3884,32 +3884,36 @@
 region,%0A        
+ 
+   
 access_key_id=ac
@@ -3962,25 +3962,30 @@
 ss_key,%0A    
-)
+    )%0A
 %0A    _add_cw

Add a simple-worker flag to use the SimpleManagerWorker class