import json
import logging
import time

import scrapy
import validators

from PhotoSpiders.items import PhotoSpidersItem
from PhotoSpiders.utils.date_utils import get_time_stamp
from PhotoSpiders.utils.http_utils import build_url
from PhotoSpiders.utils.string_utils import has_tags_in_use, OTHERS_TAG


class WebsiteInfoCnaSpider(scrapy.Spider):
    name = 'cna_images'

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

    def start_requests(self):
        for page in range(1, 18):
            yield scrapy.Request(f'https://www.cna.edu.tw/tw/Album.php?progId=SER017&clazzId=&groupId=&p={page}',
                                 callback=self.parse_list_page)
            # break

    def parse_list_page(self, response):
        detail_uris = response.xpath(
            "//div[@class='mainBlock']/div[@class='albumList']/span/a/@href").extract()
        for uri in detail_uris:
            full_url = build_url(response.url, [], uri)
            if not validators.url(full_url):
                continue
            yield scrapy.Request(full_url, callback=self.parse)

    def parse(self, response, **kwargs):
        page_info = {
            'current_url': response.url,
            'title': response.xpath("//div[@class='mainTitle']/text()").extract()[0],
            'description': response.xpath(
                "//div[@class='mainBlock']/div[@class='videoList']//div[@class='videoText']/text()").extract(),
            'image_url': response.xpath(
                "//div[@class='mainBlock']/div[@class='videoList']//img/@src").extract(),
            'date_taken': response.xpath(
                "//div[@class='mainBlock']/div[@id='bodyDetail']/dl[1]/dd/text()").extract()[0],
            'date_posted': response.xpath(
                "//div[@class='mainBlock']/div[@id='bodyDetail']/dl[1]/dd/text()").extract()[0],
            'tags': ['cna.edu.tw']
        }
        logging.debug(page_info)
        for i in range(len(page_info['image_url'])):
            page_item = PhotoSpidersItem()
            image_url = page_info['image_url'][i].replace("../", "https://www.cna.edu.tw/")
            if len(page_info['description']) == len(page_info['image_url']):
                description = page_info['description'][i]
            else:
                description = page_info['title']
            page_item['es_urlname'] = image_url
            page_item['es_srcname'] = page_info['current_url']
            page_item['es_urltitle'] = page_info['title']
            page_item['es_urlcontent'] = description
            page_item['es_urltime'] = get_time_stamp(page_info['date_taken'])
            page_item['es_lasttime'] = get_time_stamp(page_info['date_posted'])
            page_item['es_urlimage'] = [image_url]
            page_item['es_sitename'] = '海军军官学校（台湾）'
            page_item['es_loadtime'] = str(int(time.time() * 1000))
            page_item['es_carriertype'] = 'news'
            page_item['es_doclength'] = len(page_item['es_urlcontent'])
            page_item['es_catalog2'] = []
            if not has_tags_in_use(page_info['tags']):
                page_info['tags'].append(OTHERS_TAG)
            page_item['es_tags'] = ';'.join(page_info['tags'])
            response.request.meta['CUSTOM_FILTER_KEY'] = 'URL_Filter:PhotoSpiders:CNA_URL_Filter'
            response.request.meta['CUSTOM_FILTER_VAL'] = page_item['es_urlname']
            yield page_item
