import concurrent.futures

from unitls.LogerinTxt import app_logger
from unitls.StaticDataclass import get_dic
from unitls.baseFunction import TuplefindInList , TuplefindInList3
from .flightDB import flightDB
from .getSortFlightdata import getSortFlightdata
from .waringMessageID import waringMessageID


def tableWidgetDispaly2(fdb:flightDB,selectedtime,date):#1,昨天。2今天3明天4其他
    try:
        tableWidgetDispalydata = {}
        sorttable = getSortFlightdata(fdb,selectedtime,"sortFlight2")
        nowDayStr = selectedtime
        TaskFlightinfoAll = fdb.getAlldata('TaskFlightinfo{}'.format(nowDayStr))
        waringMessageIDs = waringMessageID(fdb)
        peopleScheduleAll = fdb.getAlldata("peopleSchedule{}".format(nowDayStr))
        tasklistall = fdb.getAlldata('TaskList{}'.format(nowDayStr))
        executor = concurrent.futures.ThreadPoolExecutor()
        all_task = []
        taskType=get_dic('taskType')
        for i in range(len(sorttable)):
            all_task.append(executor.submit(functionDisplay2, sorttable[i], i, TaskFlightinfoAll, waringMessageIDs,peopleScheduleAll,taskType,tasklistall))

        for future in concurrent.futures.as_completed(all_task):
            data = future.result()

            tableWidgetDispalydata.update(data)
        newdic = {"B": '"{}"'.format(tableWidgetDispalydata)}
        if date == "1":
            fdb.upDateItem("display", newdic, "ID = '1'")
        elif date == "2":
            fdb.upDateItem("display", newdic, "ID = '2'")
        elif date == "3":
            fdb.upDateItem("display", newdic, "ID = '3'")
        return tableWidgetDispalydata
    except Exception as e:
        app_logger.log_error(e)


def functionDisplay2(row, rowNum, TaskFlightinfoAll,waringMessageIDs,peopleScheduleAll,taskType,TaskListAll):

    TaskFlightinfo = TuplefindInList(TaskFlightinfoAll,row[1])
    jjdq="--"
    hbdq="--"
    esdq="--"
    tasksts=""
    if '3U' in TaskFlightinfo[21] or 'CSC' in TaskFlightinfo[21]:
        tasksts = TuplefindInList3(TaskListAll, TaskFlightinfo[0], 0, 8)
        if TaskFlightinfo[3] == "AP":
            hbdq = "√" if 'P-PRE-FLIGHT' in str(tasksts)  else ""
            esdq = "√" if  'DEPARTURE-SERVICE' in str(tasksts) else ""
        elif TaskFlightinfo[3] == "TR" :
            jjdq = "√" if 'ARRIVAL-SERVICE' in str(tasksts) else ""
            hbdq = "√" if 'P-TRANSIT' in str(tasksts) else ""
            esdq = "√" if 'DEPARTURE-SERVICE' in str(tasksts) else ""
        elif TaskFlightinfo[3] == "AF":
            jjdq = "√" if 'ARRIVAL-SERVICE' in str(tasksts) else ""
            hbdq = "√" if 'P-POST-FLIGHT' in str(tasksts) else ""


    if TaskFlightinfo != None:
        JWA = TaskFlightinfo[10]
        JWB = TaskFlightinfo[11]
        if JWB!="" and JWA =="":
            JWA=JWB
        if JWA!="" and JWB =="":
            JWB=JWA
    else:
        JWA =  ""
        JWB =  ""
    wx_people = ""
    fx_people = ""
    ecsj_people = ""
    type31 = ""
    # print(TaskFlightinfo)
    if TaskFlightinfo != None:
        wx_people = TaskFlightinfo[12]
        fx_people = TaskFlightinfo[13]
        ecsj_people = TaskFlightinfo[19]
        type31 = TaskFlightinfo[3]

    if TaskFlightinfo[3] == "AF":
        postion = str(TaskFlightinfo[23]).split("</br>")[0]
    elif TaskFlightinfo[3] == "TR" or TaskFlightinfo[3] == "TAF":
        postion = str(TaskFlightinfo[23]).replace("</br>天府","")
    elif "</br>" in str(TaskFlightinfo[23]):
        postion = str(TaskFlightinfo[23]).split("</br>")[1]
    else:
        postion ="无信息"


    if TaskFlightinfo[4] != "" and (TaskFlightinfo[8] == "" or TaskFlightinfo[30] == ""):  #如果sta不为空，且eta或flying为空，显示sta
        a = TaskFlightinfo[4][-8:-3]
    else:
        a=""
    if TaskFlightinfo[6] != "" :   #当ata不为空显示ata，eta不为空显示eta否则为空
        b = TaskFlightinfo[6][-8:-3]
    elif TaskFlightinfo[8] != "":
        b = TaskFlightinfo[8][-8:-3]
    else:
        b = ''
    if TaskFlightinfo[7] != "" :
        c = '[实]' + TaskFlightinfo[7][-8:-3]
    elif TaskFlightinfo[5] != "":
        c = '[预]' + TaskFlightinfo[5][-8:-3]
    else:
        c = ''

    QColorList = ['black', 'white', '#F3ECD9', '#BAD1D1', '#DDE2E3', '#647277', '#00FFFF', 'blue']
    #QColorList = ['black', 'white', '#F3ECD9', '#BAD1D1', '#DDE2E3', '#647277', 'red', 'blue']
    # waringMessageIDs = waringMessageID(fdb)
    if str(row[1]) in str(waringMessageIDs["全部警告"]):
        color_code = QColorList[6]
        color_code1 = QColorList[0]
        color_code2 = QColorList[1]
        color_code3 = QColorList[0]
    elif row[5] == "4":
        color_code = QColorList[5]
        color_code1 = QColorList[1]
        color_code2 = QColorList[1]
        color_code3 = QColorList[1]
    elif row[5] == "3":
        color_code = QColorList[3]
        color_code1 = QColorList[0]
        color_code2 = QColorList[0]
        color_code3 = QColorList[0]
    elif row[5] == "2":
        color_code = QColorList[2]
        color_code1 = QColorList[0]
        color_code2 = QColorList[0]
        color_code3 = QColorList[0]
    elif row[5] == "1":
        if (rowNum + 2) % 2 == 0:
            color_code = QColorList[4]
        else:
            color_code = QColorList[1]
        color_code1 = QColorList[0]
        color_code2 = QColorList[6]
        color_code3 = QColorList[7]

    findid = row[0] + "-B"
    #peopleSchedule = fdb.queryTabel("peopleSchedule{}".format(nowDayStr), "*", "编号='%s'" % findid)
    peopleSchedule = TuplefindInList(peopleScheduleAll, findid)
    if peopleSchedule != None:
        fx = peopleSchedule[3]
        qw1 = peopleSchedule[4]
        qw2 = peopleSchedule[5]
        g1 = peopleSchedule[6]
        g2 = peopleSchedule[7]
        g3 = peopleSchedule[8]
        fj = peopleSchedule[9]
    else:
        fx = ""
        qw1 = ""
        qw2 = ""
        g1 = ""
        g2 = ""
        g3 = ""
        fj = ""

    if TaskFlightinfo[3] == "AF":
        item_dic = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": a,
                    "6": b,
                    "7": "--",
                    "8": JWA,
                    "9": taskType[TaskFlightinfo[3]],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "19": "",
                    "16": jjdq,
                    "18": esdq,
                    "17": hbdq,
                    "20": "",
                    "21": "",
                    "22": "",
                    "23": "",
                    "24": postion,
                    "25": wx_people,  # flightinfodata[22],
                    "26": "",
                    "27": fx_people,  # flightinfodata[23],
                    "28": row[1],
                    "29": tasksts,
                    "30": "",
                    "31": type31,
                    "32": fj,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5]
                    }
        # tableWidgetDispalydata[rowNum] = item_dic
        # rowNum += 1
        return {rowNum: item_dic}
    elif TaskFlightinfo[3] == "停场":
        item_dic = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": "--",
                    "5": a,
                    "6": "--",
                    "7": "--",
                    "8": JWA,
                    "9": taskType[TaskFlightinfo[3]],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "16": "--",
                    "17": "--",
                    "18": "--",
                    "19": "--",
                    "20": "--",
                    "21": "--",
                    "22": "--",
                    "23": "--",
                    "24": "--",  # flightinfodata[22],
                    "25": "--",  # flightinfodata[23],
                    "26": "--",
                    "27": "--",
                    "28": row[1],
                    "29": tasksts,
                    "30": "--",
                    "31": "--",
                    "32": fj,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5]

                    }
        # tableWidgetDispalydata[rowNum] = item_dic
        # rowNum += 1
        return {rowNum: item_dic}
    elif TaskFlightinfo[3] == "AP":
        item_dic = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": "",
                    "6": "",
                    "7": c,
                    "8": JWB,
                    "9": taskType[TaskFlightinfo[3]],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "19": "",
                    "16": jjdq,
                    "18": esdq,
                    "17": hbdq,
                    "20": "",
                    "21": "",
                    "22": "",
                    "23": "",
                    "24": postion,
                    "25": wx_people,  # flightinfodata[22],
                    "26": "",
                    "27": fx_people,  # flightinfodata[23],
                    "28": row[1],
                    "29": tasksts,
                    "30": "",
                    "31": type31,
                    "32": fj,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5]
                    }
        # tableWidgetDispalydata[rowNum] = item_dic
        # rowNum += 1
        return {rowNum: item_dic}
    elif row[3] == "短停接":
        item_dic = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": a,
                    "6": b,
                    "7": c,
                    "8": JWA,
                    "9": taskType[TaskFlightinfo[3]],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "19": "",
                    "16": jjdq,
                    "18": esdq,
                    "17": hbdq,
                    "20": "",
                    "21": "",
                    "22": "",
                    "23": "",
                    "24": postion,
                    "25": wx_people,  # flightinfodata[22],
                    "26": ecsj_people,
                    "27": fx_people,  # flightinfodata[23],
                    "28": row[1],
                    "29": tasksts,
                    "30": "",
                    "31": type31,
                    "32": fj,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5]
                    }
        # tableWidgetDispalydata[rowNum] = item_dic
        # rowNum += 1
        return {rowNum: item_dic}
    elif row[3] == "特后前接":
        item_dic = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": a,
                    "6": b,
                    "7": c,
                    "8": JWA,
                    "9": taskType[TaskFlightinfo[3]],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "19": "",
                    "16": jjdq,
                    "18": esdq,
                    "17": hbdq,
                    "20": "",
                    "21": "",
                    "22": "",
                    "23": "",
                    "24": postion,
                    "25": wx_people,  # flightinfodata[22],
                    "26": "",
                    "27": fx_people,  # flightinfodata[23],
                    "28": row[1],
                    "29": tasksts,
                    "30": "",
                    "31": type31,
                    "32": fj,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5]
                    }
        #tableWidgetDispalydata[rowNum] = item_dic
        #rowNum += 1
        return {rowNum:item_dic}


