#!/usr/bin/python

import pygame.font
import re

class Display_Activity:
    def __init__(self, window):
        self.screen = window.screen
        self.screen_rect = self.screen.get_rect()

        self.font_white = (250, 250, 250)
        self.font = pygame.font.SysFont(None, 20)

        self.font_med = pygame.font.SysFont(None, 20)

        self.font_red = (255, 0 , 0)

        self.network_traffic_dict = {}

    def prep_message(self, threaded_data):
        #print(threaded_data)
        self.network_traffic_dict = {}
        for data in threaded_data:
            if 'Adding' in data:
                pass
            else:
                data = data.replace('\n', '')
                split_data = data.split(' ')
                if len(split_data) > 2:
                    key = split_data[0]
                    self.network_traffic_dict[key] = [split_data[0], split_data[-1]]

        #images
        if len(self.network_traffic_dict) == 1:
            for traffic in self.network_traffic_dict.values():
                search = traffic[-1]
                something = re.search('(?<=\t).*', search)
                if something is not None:
                    something = something.group()
                    something = re.split('\t', something)
                    traffic[-1] = str(f'sent:{something[0]}, recevied:{something[1]}')
                self.string1 = self.font_med.render(f'{traffic[0]} {traffic[-1]}', True, self.font_white)
        elif len(self.network_traffic_dict) == 2:
            counter = 0
            for traffic in self.network_traffic_dict.values():
                counter += 1
                search = traffic[-1]
                something = re.search('(?<=\t).*', search)
                if something is not None:
                    something = something.group()
                    something = re.split('\t', something)
                    traffic[-1] = str(f'sent:{something[0]}, recevied:{something[1]}')
                if counter == 1:
                    self.string1 = self.font_med.render(f'{traffic[0]} {traffic[-1]}', True, self.font_white)
                elif counter == 2:
                    self.string2 = self.font_med.render(f'{traffic[0]} {traffic[-1]}', True, self.font_white)

        #rect
        if len(self.network_traffic_dict) == 1:
            self.string1_rect = self.string1.get_rect()

        #position
            self.string1_rect.midleft = self.screen_rect.midleft

        elif len(self.network_traffic_dict) == 2:
            self.string1_rect = self.string1.get_rect()
            self.string2_rect = self.string2.get_rect()

        #position
            self.string1_rect.midleft = self.screen_rect.midleft
            self.string2_rect.midtop = self.string1_rect.midbottom
            self.string2_rect.x = self.string1_rect.x

    def show_text(self):
        if len(self.network_traffic_dict) == 1:
            self.screen.blit(self.string1, self.string1_rect)
        elif len(self.network_traffic_dict) == 2:
            self.screen.blit(self.string1, self.string1_rect)
            self.screen.blit(self.string2, self.string2_rect)
