// 定义 PropertyChangedEventHandler 类型
type PropertyChangedEventHandler = (sender: any, args: { propertyName: string }) => void;
// 定义 EventHandler 类型
type EventHandler = (sender: any, args: any) => void;

// 模拟 ISampleGrabberCB 接口
interface ISampleGrabberCB {
    SampleCB(sampleTime: number, sample: ArrayBuffer): number;
    BufferCB(sampleTime: number, buffer: ArrayBuffer, bufferLen: number): number;
}

// 模拟 INotifyPropertyChanged 接口
interface INotifyPropertyChanged {
    PropertyChanged: PropertyChangedEventHandler | null;
}

class CapGrabber implements ISampleGrabberCB, INotifyPropertyChanged {
    private _height: number = 0;
    private _width: number = 0;
    public Map: ArrayBuffer | null = null;

    public PropertyChanged: PropertyChangedEventHandler | null = null;
    public NewFrameArrived: EventHandler | null = null;

    constructor() {
        // 构造函数逻辑
    }

    public SampleCB(sampleTime: number, sample: ArrayBuffer): number {
        return 0;
    }

    public BufferCB(sampleTime: number, buffer: ArrayBuffer, bufferLen: number): number {
        if (this.Map) {
            const target = new Uint8Array(this.Map);
            const source = new Uint8Array(buffer);
            target.set(source.subarray(0, bufferLen));
            this.onNewFrameArrived();
        }
        return 0;
    }

    private onNewFrameArrived() {
        if (this.NewFrameArrived) {
            this.NewFrameArrived(this, null);
        }
    }

    private onPropertyChanged(name: string) {
        if (this.PropertyChanged) {
            this.PropertyChanged(this, { propertyName: name });
        }
    }

    public get Width(): number {
        return this._width;
    }

    public set Width(value: number) {
        this._width = value;
        this.onPropertyChanged("Width");
    }

    public get Height(): number {
        return this._height;
    }

    public set Height(value: number) {
        this._height = value;
        this.onPropertyChanged("Height");
    }
}

export default CapGrabber;