import ast
import pickle

from bs4 import BeautifulSoup

import amro_progress
#张翔B
import os
import time
import random

date_list =['2023-02-01', '2023-02-02', '2023-02-05', '2023-02-04', '2023-02-08', '2023-02-09', '2023-02-10',
            '2023-02-12', '2023-02-13', '2023-02-14', '2023-02-16', '2023-02-17', '2023-02-18', '2023-02-20',
            ]

part1 =['陈尚',
        '石常林',
        '谭文强',
        '张祖文',
        '林世鹏',
        '徐培昊',
        '张杨第',
        '肖兴虎',
        '李南',#李南
        '王瑁',
        '郭子豪',
        '宋朱陶',
        '李洪洋',
        '胡陈文焕',
        '张潇爽',
        '那超']
#任轩宇
part2 =['周凯',
        '雷一鸣',
        '符颖奇',
        '谢明杰',
        '谢友为',
        '叶林青',
        '陈浠平',
        '王文智',
        '杨宜欣',
        '权志勤',
        '陈思嘉B',
        '陶彦桥',
        '吴昕阳',
        '李俊峰',
        '李成斌',
        '张礼雍']
#于冬雪
part3 =[
        '黄立博',
        '孙伟C',
        '任悍',
        '张良',   #张良A
        '孙胜涛',
        '金星宇',
        '滕昭科',
        '陶柏旭',
        '谢德成',
        '雷扬',
        '杨国晟',
        '王勇I',
        '王思捷',
        '汤其秋',
        '张路平',
        ]

nameAll = part1+part2+part3

def getAllData(path):
    tasktype = {"AP": "航前", "TR": "短停", "AF": "航后"}
    keyList = ['WXRY', 'TA', 'TD', 'FLIGHT_NO', 'ACTYPE', 'ARR_CH1', 'DEP_CH1', 'BAY', 'FXRY', 'ENG_TYPE', 'ACNO',
               'TASKTYPE', 'REMARK_BUSINESS']
    with open(path,'r', encoding='utf-8') as f:
        text = f.read()
        data = ast.literal_eval(text)
        uselly = data['data']

        dic = {}
        number = 0
        for key in uselly:
            number +=1
            bay = 'BAY'
            for kl in keyList:

                if kl not in key:

                    if kl == bay:
                        # print(key)
                        bay = 'BAY_2'
                        if key[bay] == '':
                            bay = 'BAY_1'
                        # print(bay)
                    else:
                        key[kl] = ''
                        # print(key[kl])
            try:
                dic[number] = {'FX':'{},{}'.format(key['FXRY'],key['WXRY']),'bay':'{}'.format(key[bay]),
                               'bnum':'{}'.format(key['ACNO']), 'tasktype':'{}'.format(tasktype[key['TASKTYPE']]),
                                                'MODIFY_TIME':'{}'.format(key['MODIFY_TIME'])}
            except KeyError as e:
                print(e)

        #print(dic)
        return dic

def dataprocess():
    dic_list = []
    new_data = []
    for _, _, name in os.walk('./hb'):
        for i in name:
            #print(os.path.join('hb', i))
            dic = getAllData(os.path.join('hb', i))

            dic_list.append(dic)

    for dic in dic_list:
        #print(dic)
        flag = 0
        for key in dic:
            subdata = ['','','','']
            # print(dic[key])
            lr = ''
            ltype = ''
            lbay = ''
            lbnum = ''
            ltime = ''
            for i in dic[key]:

                if i == 'FX':
                    flag = 0
                    nameList = dic[key][i].split(',')

                    for n in nameList:

                        if n in nameAll:
                            flag = 1
                        else:
                            flag = 0

                if flag == 1:

                    if i == 'FX':
                        lr = dic[key][i]

                    elif i == 'tasktype':
                        ltype = dic[key][i]

                    elif i == 'bnum':
                        lbnum = "B-{}".format(dic[key][i])

                    elif i == 'MODIFY_TIME':
                        ntime = time.strptime(dic[key][i], "%Y-%m-%d %H:%M:%S")
                        ltime="{}.{}.{}".format(ntime[0], ntime[1], ntime[2])

                    else:
                        lbay = dic[key][i]
            if ltime!='':
                if len(lbay)>2 and (lbay[0]=='1' or lbay[0]=='2' or lbay[0]=='6'):
                    ldic={}
                    ldic['时间']=ltime
                    ldic['类型'] = ltype
                    ldic['人员'] = lr
                    ldic['机号'] = lbnum
                    ldic['机位'] = lbay
                    new_data.append(ldic)
                    #print("时间：{}，类型：{}，人员：{}，机号：{}，机位：{}".format(ltime,ltype,lr,lbnum,lbay))
    #print(new_data)
    return new_data

def chooseData():
    hq = []
    chq = []
    dt = []
    cdt = []
    hh = []
    chh = []
    data = dataprocess()
    for i in data:

        if i['类型'] == '航前':
            hq.append(i)
        if i['类型'] == '航后':
            hh.append(i)
        if i['类型'] == '短停':
            dt.append(i)

    print(len(hq),len(dt),len(hh))
    if len(hq)<10:
        chq=hq
    else:
        for i in range(10):
            num = random.randint(1,len(hq)-1)
            #print(num)
            #print(hq[num])
            chq.append(hq[num])
    if len(dt)<10:
        cdt = dt
    else:
        for i in range(10):
            num = random.randint(1,len(dt)-1)
            #print(num)
            #print(dt[num])
            cdt.append(dt[num])
    if len(hh)<10:
        chh = hh
    else:
        for i in range(10):
            num = random.randint(1,len(hh)-1)
            #print(num)
            #print(hh[num])
            chh.append(hh[num])
    # print(chq)
    # print(len(chq))
    return chq,cdt,chh

def getDataFromAmro(*args):
    if args!=():
        AMRO = args[0]
    else:
        AMRO = amro_progress.amro()
    AMRO.openAmro()
    AMRO.openMaintitle("航线维修")
    # AMRO.openSubTitle("工作指令(WO)管理")
    AMRO.openSubTitle("航班动态")
    data_all_list = []

    data_list = AMRO.getFlightData()
    data_all_list.append(data_list)
    print(data_all_list)
    # for i in range(len(date_list)):
    #
    #     with open('flightDate{}.pkl'.format(date_list[i]), 'wb') as f:
    #         pickle.dump(data_all_list[i], f, pickle.HIGHEST_PROTOCOL)

def searchData(amro):
    data = amro.searchFlight()
    print('searchFLight---')
    print(data)
    return data

def loadData(*args):

    if args != ():
        with open('./static/flightdata/flight{}.txt'.format(args[0]),'rb') as f:
            flightDataContent = f.read().decode('utf-8')
    else:
        with open('./static/flightdata/flight{}.txt'.format(time.strftime("%Y%m%d", time.localtime())),'rb')as f:
            flightDataContent = f.read().decode('utf-8')

    soup = BeautifulSoup(flightDataContent, "html5lib")
    flight_list = []

    for idx, tr in enumerate(soup.find_all('tr')):
        tds = tr.find_all('td')
        temp_list = []
        for i in tds:

            try:
                temp = i.contents[0].contents[0].contents[0]
            except:
                try:
                    temp = i.contents[0].contents[0]
                except:
                    temp = ""

            temp_list.append(temp)

        flight_list.append({
            "机号": temp_list[0],
            "机型": temp_list[1],
            "发动机": temp_list[2],
            "航班号": temp_list[3],

            "进港起飞": temp_list[4],
            "进港到达时间": temp_list[5],
            "航站": temp_list[6],
            "离港起飞时间": temp_list[7],
            "离港到达": temp_list[8],
            "进港机位": temp_list[9],
            "离港机位": temp_list[10],
            "保留": temp_list[11],
            "任务类型": temp_list[12],
            "随机": temp_list[13],
            "维修人员": temp_list[14],
            "放行人员": temp_list[15],
            "工作状态": temp_list[16],
            "工作状态时间": temp_list[17],
            "重要航班": temp_list[18],
            "备降/返航": temp_list[19],
            "延误": temp_list[20],
            "附加信息": temp_list[21],

        })
    print(flight_list)
    #         print(flight_list)
    # print(data_all)
    return flight_list

def changedata(flight_list):

    data = {}
    for i in flight_list:
        if i['离港起飞时间'].find('实') == -1:
            if i['任务类型'] == '航前':
                if i['离港机位'] !='':
                    if i['离港机位'] in data:
                        print('航前重复机位：{}'.format(i['离港机位']))
                    data[i['离港机位']]={"bnum":i['机号'], 'actype':i['机型'], 'engtype':i["发动机"],
                                     'tasktype':i['任务类型'], 'timein':i['进港到达时间'],'timeout':i['离港起飞时间'],
                                     'ftnum':i['航班号'], 'pos1':i['进港起飞'], 'pos2':i["离港到达"],
                                     'FX':i["放行人员"], 'WX':i["维修人员"], 'remark':i["附加信息"]}
            else:
                if i['进港机位'] != '':
                    if i['进港机位'] in data:
                        print('重复机位:{}'.format(i['进港机位']))
                    data[i['进港机位']] = {"bnum": i['机号'], 'actype': i['机型'], 'engtype': i["发动机"],
                                       'tasktype': i['任务类型'], 'timein': i['进港到达时间'], 'timeout': i['离港起飞时间'],
                                       'ftnum': i['航班号'], 'pos1': i['进港起飞'], 'pos2': i["离港到达"],
                                       'FX': i["放行人员"], 'WX': i["维修人员"], 'remark': i["附加信息"]}

    print(data)
    for key in data:
        print(key,data[key])
    return data

def changeAlldata(flight_list):
    dic = {}
    number = 0
    for i in flight_list:
        number +=1
        if i["任务类型"]=='航前':
            dic[number] = {'bay':'{}'.format(i["离港机位"]),'bnum': '{}'.format(i["机号"]),
                           'actype': '{}'.format(i["机型"]), 'engtype': '{}'.format(i["发动机"]),
                           'tasktpye': '{}'.format(i["任务类型"]),'timein':'{}'.format(i['进港到达时间']),
                           'timeout':'{}'.format(i['离港起飞时间']), 'ftnum':'{}'.format(i['航班号']),
                           'pos1':'{}'.format(i['进港起飞']),'pos2':'{}'.format(i['离港到达']),'FX':'{}'.format(i['放行人员']),
                           'WX':'{}'.format(i['维修人员']),'remark':'{}'.format(i['附加信息']),
                           }
        else:
            dic[number] = {'bay': '{}'.format(i["进港机位"]), 'bnum': '{}'.format(i["机号"]),
                           'actype': '{}'.format(i["机型"]), 'engtype': '{}'.format(i["发动机"]),
                           'tasktpye': '{}'.format(i["任务类型"]), 'timein': '{}'.format(i['进港到达时间']),
                           'timeout': '{}'.format(i['离港起飞时间']), 'ftnum': '{}'.format(i['航班号']),
                           'pos1': '{}'.format(i['进港起飞']), 'pos2': '{}'.format(i['离港到达']), 'FX': '{}'.format(i['放行人员']),
                           'WX': '{}'.format(i['维修人员']), 'remark': '{}'.format(i['附加信息']),
                           }

    return dic

def chooseData2():
    data = loadData()
    flag = 0
    hq = []
    dt = []
    hh = []
    for i in data:
        for ii in i:
            if ii['任务类型'] == '航前':
                wxry = ii['维修人员'].split(',')
                fxry = ii['放行人员'].split(',')
                ry = wxry+fxry
                for name in ry:

                    if name in nameAll:
                        flag =1
                    else:
                        flag =0
                        break
                if flag ==1 :
                    hq.append(ii)
            if ii['任务类型'] == '短停':
                wxry = ii['维修人员'].split(',')
                fxry = ii['放行人员'].split(',')
                ry = wxry+fxry
                for name in ry:

                    if name in nameAll:
                        flag =1
                    else:
                        flag =0
                        break
                if flag ==1 :
                    dt.append(ii)
            if ii['任务类型'] == '航后':
                wxry = ii['维修人员'].split(',')
                fxry = ii['放行人员'].split(',')
                ry = wxry+fxry
                for name in ry:

                    if name in nameAll:
                        flag =1
                    else:
                        flag =0
                        break
                if flag ==1 :
                    hh.append(ii)
    print(hq)
    print(len(hq))
    print(dt)
    print(len(dt))
    print(hh)
    print(len(hh))
    return hq, dt, hh

def selectData():
    hq, dt, hh = chooseData2()
    new_hq = []
    new_hq_pool = []
    new_dt = []
    new_dt_pool = []
    new_hh = []
    new_hh_pool = []
    for i in range(10):
        flag = True
        while flag:
            num = random.randint(1,len(hq)-1)
            if num not in new_hq_pool:
                new_hq_pool.append(num)
                flag = False

    for i in range(10):
        flag = True
        while flag:
            num = random.randint(1, len(dt) - 1)
            if num not in new_dt_pool:
                new_dt_pool.append(num)
                flag = False
    for i in range(10):
        flag = True
        while flag:
            num = random.randint(1, len(hh) - 1)
            if num not in new_hh_pool:
                new_hh_pool.append(num)
                flag = False
    print(new_hq_pool)
    print(new_dt_pool)
    print(new_hh_pool)
    for i in new_hq_pool:
        new_hq.append(hq[i])
    for i in new_dt_pool:
        new_dt.append(dt[i])
    for i in new_hh_pool:
        new_hh.append(hh[i])
    print(new_hq)
    print(new_dt)
    print(new_hh)
    return new_hq, new_dt, new_hh




if __name__ == '__main__':
    import sys
    #sys.setrecursionlimit(1000000)
    AMRO = amro_progress.amro()
    AMRO.quitWeb()
    searchData(AMRO)
    #selectData()
    #l = loadData()
    #changedata(l)
    for i in range(5):

        time.sleep(10)
        searchData(AMRO)