#! /usr/bin/env python
# coding: utf-8

import sys
import logging
import argparse
from JYTools import jy_input, StringTool
from JYTools.JYWorker import RedisStat, RedisQueue

__author__ = '鹛桑够'

# logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logging.basicConfig()

arg_man = argparse.ArgumentParser()
arg_man.add_argument("--debug", dest="debug", help="debug mode, print debug msg", action="store_true", default=False)


def parse_args():
    args = arg_man.parse_args()
    if args.debug is True:
        logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")
    return args


def empty_help():
    if len(sys.argv) <= 1:
        sys.argv.append("-h")


def list_queue():
    arg_man.add_argument("-w", "--work-tag", dest="work_tag", help="work tag", metavar="")
    args = parse_args()
    rs = RedisStat()
    if args.work_tag is None:
        qd = rs.list_queue()
        for key in qd:
            print(key)
    else:
        lqd = rs.list_queue_detail(args.work_tag)
        for item in lqd:
            print(item)


def list_worry_queue():
    rs = RedisStat()
    wq = rs.list_worry_queue()
    for item in wq:
        print(item)


def list_heartbeat():
    arg_man.add_argument("-w", "--work-tag", dest="work_tag", help="work tag", metavar="")
    rs = RedisStat()
    args = parse_args()
    if args.work_tag is None:
        ws = rs.list_heartbeat()
        for item in ws:
            print(item)
    else:
        hd = rs.list_heartbeat_detail(args.work_tag)
        print(hd)


def delete_heartbeat():
    empty_help()
    rs = RedisStat()
    arg_man.add_argument("-w", "--work-tag", dest="work_tag", help="work tag", metavar="", nargs="*", default=[])
    args = parse_args()
    for item in args.work_tag:
        print("delete heartbeat %s" % item)
        rs.delete_heartbeat(item)


def list_worker():
    arg_man.add_argument("-w", "--work-tag", dest="work_tag", help="work tag", metavar="")
    rs = RedisStat()
    args = parse_args()
    if args.work_tag is None:
        ws = rs.list_worker()
        for item in ws:
            print(item)
    else:
        hd = rs.list_worker_detail(args.work_tag)
        print(hd)


def auto_wash_worker_func(work_tag, redis_man=None):
    # 获取当前队列中是否任务
    # 获取当前多少个Worker
    rs = RedisStat(redis_man=redis_man)
    data = rs.list_worker_detail(work_tag)
    ids = data.keys()


def wash_worker():
    empty_help()
    arg_man.add_argument("-a", "--auto", dest="auto", help="auto set num", action="store_true", default=False)
    arg_man.add_argument("-w", "--work-tag", dest="work_tag", help="work tag", metavar="", nargs="*", default=[])
    arg_man.add_argument("-n", "--num", dest="num", help="num of wash package to send", metavar="", type=int, default=1)
    args = parse_args()
    r_queue = RedisQueue()
    rs = RedisStat(redis_man=r_queue.redis_man)

    for item in args.work_tag:
        print("wash work tag %s" % item)
        if args.auto is True:
            data = rs.list_worker_detail(item)
            num = len(data.keys())
            print("In auto mode, send %s wash package %s" % (item, num))
            r_queue.wash_worker(item, num)
        else:
            r_queue.wash_worker(item, args.num)


def look_task_item():
    arg_man.add_argument("-w", "--work-tag", dest="work_tag", help="work tag", metavar="")
    args = parse_args()
    rs = RedisStat()
    values = []
    if args.work_tag is not None:
        values.append(args.work_tag)
    while True:
        prompt_prefix = ""
        if len(values) <= 0:
            work_tag = jy_input("Please Input Work_tag", prompt_prefix=prompt_prefix)
            work_tag = work_tag.strip()
            if work_tag.lower() in ("e", "exit"):
                sys.exit(0)
            values.append(work_tag)
            continue
        if len(values) == 1:
            prompt_prefix += "[work_tag:%s]" % values[0]
            key = jy_input("Please Input Task Key", prompt_prefix=prompt_prefix)
            key = key.strip()
            if key.lower() in ("e", "exit"):
                values.remove(values[-1])
                continue
            values.append(key)
            continue
        elif len(values) >= 2:
            sub_key = None
            prompt_prefix += "[work_tag:%s][key:%s]" % (values[0], values[1])
            if len(values) >= 3:
                sub_key = StringTool.join_decode(values[2:], "_")
                prompt_prefix += "[sub_key:%s]" % sub_key
            task_items = rs.list_task_item(values[0], values[1], sub_key)
            prompt = ""
            if len(task_items["sub"].keys()) > 0:
                prompt += "Input follow value look sub item\n"
                prompt += "\n".join(task_items["sub"])
                prompt += "\n"
            if len(task_items["values"].keys()) > 0:
                prompt += "Input follow value look item value\n"
                prompt += "\n".join(task_items["values"])
            while True:
                item_key = jy_input(prompt, prompt_prefix=prompt_prefix)
                item_key = item_key.strip()
                if item_key.lower() in ("e", "exit"):
                    values.remove(values[-1])
                    break
                if item_key in task_items["sub"]:
                    values.append(item_key)
                    break
                elif item_key in task_items["values"]:
                    print(task_items["values"][item_key])
                    continue
                else:
                    continue


if __name__ == "__main__":
    sys.argv.append("--debug")
    sys.argv.extend(["-w", "Pipeline"])
    # wash_worker()
    logging.info("ssd")
    look_task_item()
