import json
import socket
import struct
from json import JSONDecoder
import threading

from dcs.tools import message_process as mp
from dcs.tools.message_process import parse_request, generate_response


class Crawl(threading.Thread):
    def __init__(self):
        super(Crawl, self).__init__()
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
        self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_socket.bind(('', 9000))

    @staticmethod
    def crawl() -> dict:
        result_map = {0: {'name': 'remote', 'college': 'remote', 'major': 'remote', 'paper': 'remote'}}
        return result_map

    def run(self) -> None:
        self.server_socket.listen()
        while True:
            client_socket, _ = self.server_socket.accept()
            request_map = parse_request(client_socket)
            if request_map['type'] == 'request':
                print("receiving help request:\n"+json.dumps(request_map, ensure_ascii=False))
                response_map = self.crawl()
                response_map.update({'cookie': request_map['cookie']})
                client_socket.sendall(generate_response(response_map))
            if request_map['type'] == 'response':
                print("receiving response:\n" + json.dumps(request_map, ensure_ascii=False))
            # break


crawl = Crawl()
crawl.start()
# crawl.join()

with socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) as socket_to_server:
    socket_to_server.bind(('127.0.0.1', 9000))
    socket_to_server.connect(('127.0.0.1', 7777))

    request = {'action': 'register', 'user': 'wufayuan', 'password': '113818'}
    socket_to_server.sendall(mp.generate_request(request))
    responseJson = JSONDecoder().decode(
        mp.read_bytes(socket_to_server, struct.unpack('!Q', socket_to_server.recv(8))[0]).decode(
            "utf-8"))
    print(responseJson)

    request = {'action': 'login', 'user': 'wufayuan', 'password': '113818'}
    socket_to_server.sendall(mp.generate_request(request))
    responseJson = JSONDecoder().decode(
        mp.read_bytes(socket_to_server, struct.unpack('!Q', socket_to_server.recv(8))[0]).decode(
            "utf-8"))
    cookie = responseJson['cookie']
    print(responseJson)

    request = {'action': 'report_free', 'cookie': cookie}
    socket_to_server.sendall(mp.generate_request(request))
    responseJson = JSONDecoder().decode(
        mp.read_bytes(socket_to_server, struct.unpack('!Q', socket_to_server.recv(8))[0]).decode(
            "utf-8"))
    print(responseJson)

    request = {'action': 'crawl zhiwang', 'word': 'computer', 'pages_start': 1, 'pages_end': 10,
               'cookie': cookie}
    socket_to_server.sendall(mp.generate_request(request))
    responseJson = JSONDecoder().decode(
        mp.read_bytes(socket_to_server, struct.unpack('!Q', socket_to_server.recv(8))[0]).decode(
            "utf-8"))
    print(responseJson)

    request = {'action': 'end'}
    socket_to_server.sendall(mp.generate_request(request))

crawl.join()
