#!/usr/bin/python3
# -*- coding: UTF-8 -*-
from urllib.parse import quote
from argument_parser import ArgumentParser
import sys
from html_downloader import HtmlDownloader


class UrlManager(object):
    def __init__(self):
        args = ArgumentParser()
        index_start = 1
        try:
            with open(args.index_end_path, 'r', encoding='utf-8') as f:
                index_end = int(f.readline().strip('\n'))
        except Exception as e:
            print(e)
            sys.exit(-1)
        self.new_urls = set()
        print("Adding all urls ...")
        for index in range(index_start, index_end):
            url = "https://baike.baidu.com/view/" + str(index)
            self.new_urls.add(url)
        print("Done.")
        self.old_urls = set()
        self.fail_urls = set()
        self.fail_url_mark = True
        self.downloader = HtmlDownloader()
        # self.update_new_url(index_end, args)

    def update_new_url(self, index_end, args):
        err_cnt = 0
        start = index_end
        end = start
        while True:
            if err_cnt > 10:
                break
            url = "https://baike.baidu.com/view/" + str(start)
            response, response_url = self.downloader.download_update(url)
            if not response_url or response_url == 'https://baike.baidu.com/error.html':
                err_cnt += 1
            else:
                err_cnt = 0
                self.new_urls.add(url)
                end = start
            start += 1
        with open(args.index_end_path, 'w', encoding='utf-8') as f:
            f.write(str(end))

    def add_new_url(self, url):
        if url is None:
            return
        if url not in self.new_urls and url not in self.old_urls:
            self.new_urls.add(url)

    def add_old_url(self, url):
        if url is None:
            return
        if url not in self.new_urls:
            self.old_urls.add(url)

    def add_fail_url(self, url):
        if url is None:
            return
        if url not in self.new_urls and url not in self.old_urls:
            self.fail_urls.add(url)

    def add_new_urls(self, urls):
        if urls is None or len(urls) == 0:
            return
        for url in urls:
            if url not in self.old_urls:
                self.new_urls.add(url)

    def has_new_url(self):
        if len(self.new_urls) != 0:
            return True
        elif self.fail_url_mark:
            self.new_urls = self.fail_urls.copy()
            self.fail_urls.clear()
            self.fail_url_mark = False
            return True
        else:
            return False

    def get_new_url(self):
        new_url = self.new_urls.pop()
        return new_url