from typing import Dict, List, Any
from scapy.all import Packet

from .base import FrameTypeInterface


class PTPStats(FrameTypeInterface):
    """General PTP Statistics"""
    def __init__(self):
        super().__init__()
        self.name = "PTP"
        self.count = 0
        self.bytes = 0
        self.first_time = None
        self.last_time = None
        self.sync_messages = 0
        self.follow_up_messages = 0
        self.delay_req_messages = 0
        self.delay_resp_messages = 0
        self.announce_messages = 0
    
    def add(self, timestamp: float, size: int, packet: Packet):
        self.count += 1
        self.bytes += size
        if self.first_time is None:
            self.first_time = timestamp
        self.last_time = timestamp
        
        # Simulate PTP message type counting
        if packet:
            # Example: decode PTP message type
            # msg_type = extract_ptp_message_type(packet)
            pass
    
    def get_summary_dict(self) -> Dict[str, Any]:
        duration = (self.last_time or 0) - (self.first_time or 0)
        return {
            'Pkts': self.count,
            'Duration': round(duration, 3),
            'Sync': self.sync_messages,
            'Follow Up': self.follow_up_messages,
            'Delay Req': self.delay_req_messages,
            'Delay Resp': self.delay_resp_messages,
            'Announce': self.announce_messages
        }
    
    def get_column_definitions(self) -> List[tuple]:
        return [
            ('Pkts', 'd'),
            ('Duration', '.3f'),
            ('Sync', 'd'),
            ('Follow Up', 'd'),
            ('Delay Req', 'd'),
            ('Delay Resp', 'd'),
            ('Announce', 'd')
        ]