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

from .base import FrameTypeInterface


class Ch10PCMStats(FrameTypeInterface):
    """Chapter 10 PCM (Pulse Code Modulation) Data Statistics"""
    def __init__(self):
        super().__init__()
        self.name = "Chapter 10 PCM"
        self.count = 0
        self.bytes = 0
        self.first_time = None
        self.last_time = None
        self.channel_ids = set()
        self.frame_sync_errors = 0
        self.bit_slip_events = 0
        self.minor_frame_count = 0
        self.major_frame_count = 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
        
        # In real implementation, decode PCM frame structure
    
    def get_summary_dict(self) -> Dict[str, Any]:
        duration = (self.last_time or 0) - (self.first_time or 0)
        return {
            'Pkts': self.count,
            'Bytes': self.bytes,
            'Duration': round(duration, 3),
            'Channels': len(self.channel_ids),
            'Sync Errors': self.frame_sync_errors,
            'Bit Slips': self.bit_slip_events,
            'Minor Frames': self.minor_frame_count,
            'Major Frames': self.major_frame_count
        }
    
    def get_column_definitions(self) -> List[tuple]:
        return [
            ('Pkts', 'd'),
            ('Bytes', 'd'),
            ('Duration', '.3f'),
            ('Channels', 'd'),
            ('Sync Errors', 'd'),
            ('Bit Slips', 'd'),
            ('Minor Frames', 'd'),
            ('Major Frames', 'd')
        ]