import json
import time

import scrapy

from AirplaneSpiders.items import NotamInfo
from AirplaneSpiders.utils import dateTranslateCsv


# 特殊用途空域（文件下载方式）采集引擎
class AirTrafficArchiveSpider(scrapy.Spider):
    name = 'sua_air_traffic'
    allowed_domains = ['sua.faa.gov']
    start_urls = ["https://sua.faa.gov/sua/download.app?colHead=<br><br>|Type<br><br>|Zoom<br><br>|SAA / NOTAM "
                  "ID<br><br>|Start Time<br><br>|End Time<br><br>|Center ID<br><br>|State<br><br>|Min Alt<br>(100s "
                  "ft)|Max Alt<br>(100s ft)|Group<br><br>&"]
    custom_settings = {
        'PROTO_MODULE_PATH': 'AirplaneSpiders.proto.Notam_pb2',
        'PROTO_CLASS_NAME': 'NotamInfoSets',
        'PROTO_FIELD_NAME': 'NotamInfo',
        'PROTO_SAVE_FILE_PATH': r'/usr/local/spider_data',
        'PROTO_SAVE_FILE_NAME': r'target_data_notam_',
        'IMAGES_STORE': r'/usr/local/temp_image/trafic',
        'IMAGES_URLS_FIELD': 'Image',
        'IMAGES_RESULT_FIELD': 'Image',
        'ZIP_FILE_NAME': 'image_data_notam_',
        'ITEM_PIPELINES': {
            'scrapy.pipelines.images.ImagesPipeline': 1,
            'AirplaneSpiders.pipelines.ProtobufSavePipeline': 300
        }
    }

    def __init__(self, params=None, *args, **kwargs):
        super(AirTrafficArchiveSpider, self).__init__(*args, **kwargs)
        json_params = json.loads(params)
        if 'job_id' in json_params:
            self.job_id = json_params['job_id']

    def parse(self, response):
        type_dict = {
            'AR': 'Aerial Refueling Routes/Anchors',
            'A': 'Air Traffic Control Assigned Airspaces',
            'L': 'Alert Areas',
            'M': 'Military Operations Areas',
            'O': 'Other',
            'P': 'Prohibited Areas',
            'R': 'Restricted Areas',
            'W': 'Warning Areas',
            'IR': 'IFR Military Training Routes',
            'SR': 'Slow Speed Low Altitude Training Routes',
            'VR': 'VFR Military Training Routes',
            'HAZ': 'Disaster/Hazard Areas',
            'HID': 'Disaster Areas in Hawaii',
            'EMERG': 'Emergency Air Traffic Rules',
            'VIP': 'Proximity of the President and Other Parties',
            'SPC': 'Proximity of Space Flight Operations',
            'HB': 'Flight Ops during Abnormally High Barometric Pressure Conditions',
            'EVT': 'Vicinity of Aerial Demonstrations and Major Sporting Events',
            'SEC': 'Special Security Instructions'
        }
        # 获取下载文件（表格形式）的文本格式，并按照回车符分割
        res_data_list = response.text.split('\n')
        # 逗号分割的所有标题，字符串
        title_str = res_data_list[0]
        # 按照逗号分割，返回标题列表
        title_name_list = title_str.split(',')
        # 从第2行开始遍历每行数据
        for data in res_data_list[1:]:
            if data:
                # 每行的数据列表
                value_list = data.split(',')
                body = {}
                # 将标题与数据一一对应赋值
                for i in range(len(title_name_list)):
                    title_name = title_name_list[i]
                    value = value_list[i]
                    if value.startswith('="'):
                        value = value[2:-1]
                    body[title_name] = value
                if body["Group"] == "TFR":
                    continue
                body['Start Time'] = int(1000 * dateTranslateCsv(body['Start Time']))
                body['End Time'] = int(1000 * dateTranslateCsv(body['End Time']))
                if body['Start Time'] <= 0:
                    body['Start Time'] = -1
                if body['End Time'] <= 0:
                    body['End Time'] = -1
                if body['Type'] in type_dict:
                    body['Type'] = type_dict[body['Type']]

                # 注入item
                item = NotamInfo()
                item["NotamNumber"] = body['SAA / NOTAM ID']
                item["IssueDate"] = body['Start Time']
                item["Location"] = 'Center ID: %s, State: %s' % (body['Center ID'], body['State'])
                item["BeginningDatetime"] = body['Start Time']
                item["EndingDateTime"] = body['End Time']
                item["Type"] = body['Type']
                item["AffectedAreas"] = '[]'
                item["AffectedAreaType"] = 3
                item["UpdateTime"] = str(int(time.time() * 1000))
                yield item
