import concurrent.futures

from unitls.LogerinTxt import app_logger
from unitls.StaticDataclass import get_dic
from unitls.baseFunction import TuplefindInList
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))
        flightstsdataAll = fdb.getAlldata('flightsts{}'.format(nowDayStr))
        waringMessageIDs = waringMessageID(fdb)
        peopleScheduleAll = fdb.getAlldata("peopleSchedule{}".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, flightstsdataAll, waringMessageIDs,peopleScheduleAll,taskType))

        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,flightstsdataAll,waringMessageIDs,peopleScheduleAll,taskType):

    TaskFlightinfo = TuplefindInList(TaskFlightinfoAll,row[1])

    #TaskFlightinfo = fdb.queryTabel('TaskFlightinfo{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))
    e = ""
    F = ""
    g = ""

    if TaskFlightinfo != None:
        JWA = TaskFlightinfo[10]
        JWB = TaskFlightinfo[11]
        if JWB!="" and JWA =="":
            JWA=JWB
        if JWA!="" and JWB =="":
            JWB=JWA
        if TaskFlightinfo[16] != "":
            e = "保留"
        if TaskFlightinfo[18] != "":
            F = "工作包"
        if TaskFlightinfo[21] != "":
            g = TaskFlightinfo[21]
    else:
        JWA =  ""
        JWB =  ""
    wx_people = ""
    fx_people = ""
    ecsj_people = ""
    ta_td = ""
    type31 = ""
    # print(TaskFlightinfo)
    if TaskFlightinfo != None:
        wx_people = TaskFlightinfo[12]
        fx_people = TaskFlightinfo[13]
        ecsj_people = TaskFlightinfo[19]
        ta_td = TaskFlightinfo[9]
        type31 = TaskFlightinfo[3]

    #flightinfodata = fdb.queryTabel('flightinfo{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))[0]
    # print(flightinfodataAll)
    # print(row[0])
    #flightinfodata = TuplefindInList(flightinfodataAll, row[1])
    # print(flightinfodata)
    # flightstsdata = fdb.queryTabel('flightsts{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))[0] if \
    #     len(fdb.queryTabel('flightsts{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))) != 0 else ["", "",
    #                                                                                                      "", "", "", "",
    #                                                                                                      "", "", "", "",
    #                                                                                                      "", "", "", ""]
    flightstsdata = TuplefindInList(flightstsdataAll,row[0])if TuplefindInList(flightstsdataAll,row[0])!=None else ["", "","", "", "", "","", "", "", "","", "", "", ""]
    jjdw = "√" if flightstsdata[3] != "" else ""
    FX = "√" if flightstsdata[4] != "" else ""
    XJ = "√" if flightstsdata[5] != "" else ""
    ESDW = "√" if flightstsdata[7] != "" else ""
    SJDW = "√" if flightstsdata[6] != "" else ""

    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 = ''
    d = TaskFlightinfo[15] if "随机" in str(TaskFlightinfo[15]) else ""

    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[1]
        color_code2 = QColorList[1]
        color_code3 = QColorList[1]
    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,
                    "16": jjdw,
                    "17": "--",
                    "18": "--",
                    "19": "--",
                    "20": d,
                    "21": "--",
                    "22": e,
                    "23": F,
                    "24": postion,
                    "25": wx_people,  # flightinfodata[22],
                    "26": "",
                    "27": fx_people,  # flightinfodata[23],
                    "28": row[1],
                    "29": g,
                    "30": ta_td,
                    "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": "TSK",
                    "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,
                    "16": jjdw,
                    "18": FX,
                    "19": XJ,
                    "17": "--",
                    "20": d,
                    "21": SJDW,
                    "22": e,
                    "23": F,
                    "24": postion,
                    "25": wx_people,  # flightinfodata[22],
                    "26": "",
                    "27": fx_people,  # flightinfodata[23],
                    "28": row[1],
                    "29": g,
                    "30": ta_td,
                    "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,
                    "16": jjdw,
                    "18": FX,
                    "19": "--",
                    "17": ESDW,
                    "20": d,
                    "21": SJDW,
                    "22": e,
                    "23": F,
                    "24": postion,
                    "25": wx_people,  # flightinfodata[22],
                    "26": ecsj_people,
                    "27": fx_people,  # flightinfodata[23],
                    "28": row[1],
                    "29": g,
                    "30": ta_td,
                    "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,
                    "16": jjdw,
                    "18": FX,
                    "19": "--",
                    "17": "--",
                    "20": d,
                    "21": SJDW,
                    "22": e,
                    "23": F,
                    "24": postion,
                    "25": wx_people,  # flightinfodata[22],
                    "26": "",
                    "27": fx_people,  # flightinfodata[23],
                    "28": row[1],
                    "29": g,
                    "30": ta_td,
                    "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}


