import socket
import threading
from dcs.tests.spider_task import Spider_task


class Spider(threading.Thread):
    def __init__(self):
        super(Spider, self).__init__()
        # self.tasks: list[tuple[socket.socket, dict]] = []
        self.tasks: list[Spider_task] = []
        self.daemon = True

    def add_task(self, request_map: dict, client_socket: socket.socket):
        # distribute tasks, 5 pages as a task
        # [pages_start, pages_end), like [1,3) means 1,2 page
        # pages_start = request_map['pages_start']
        # pages_end = request_map['pages_end']
        # while pages_start < pages_end:
        #     tmp = request_map.copy()
        #     tmp['pages_start'] = pages_start
        #     if pages_start + const_page <= pages_end:
        #         pages_start += const_page
        #     else:
        #         pages_start = pages_end
        #     tmp['pages_end'] = pages_start
        #     self.tasks.append((client_socket, tmp))
        self.tasks.append(Spider_task(client_socket, request_map))

    def run(self) -> None:
        while True:
            # free_remote_nodes = get_free_sockets()
            # for task in self.tasks:
            #     logger.info(f'processing crawl task...')
            #     for f_node in free_remote_nodes:
            #         address = f_node.getpeername()
            #         global_var.requester.get(address, task[1])
            #     global_var.responser.add_response('crawling state', task[0], {'crawling state': 'starting, please wait...'})
            #     spider_task = Spider_task(task[0], task[1])
            #     spider_task.start()
            #     self.tasks.remove(task)
            for task in self.tasks:
                task.start()
                self.tasks.remove(task)
        pass
