# SPDX-FileCopyrightText: © 2024-2025 Jimmy Fitzpatrick <jcfitzpatrick12@gmail.com>
# This file is part of SPECTRE
# SPDX-License-Identifier: GPL-3.0-or-later

from gnuradio import spectre
from gnuradio import soapy

import spectre_core.fields

from ._base import Base, BaseModel


class RTLSDRFixedCenterFrequencyModel(BaseModel):
    sample_rate: spectre_core.fields.Field.sample_rate = 1536000
    center_frequency: spectre_core.fields.Field.center_frequency = 95.8e6
    rf_gain: spectre_core.fields.Field.rf_gain = 30
    batch_size: spectre_core.fields.Field.batch_size = 3


class RTLSDRFixedCenterFrequency(Base[RTLSDRFixedCenterFrequencyModel]):
    def configure(self, tag: str, model: RTLSDRFixedCenterFrequencyModel) -> None:
        stream_args = ""
        tune_args = [""]
        settings = [""]
        self.soapy_rtlsdr_source = soapy.source(
            "driver=rtlsdr", "fc32", 1, "", stream_args, tune_args, settings
        )
        self.soapy_rtlsdr_source.set_sample_rate(0, model.sample_rate)
        self.soapy_rtlsdr_source.set_gain_mode(0, False)
        self.soapy_rtlsdr_source.set_frequency(0, model.center_frequency)
        self.soapy_rtlsdr_source.set_frequency_correction(0, 0)
        self.soapy_rtlsdr_source.set_gain(0, model.rf_gain)
        self.spectre_batched_file_sink = spectre.batched_file_sink(
            self._batches_dir_path,
            tag,
            model.batch_size,
            model.sample_rate,
            False,
            "rx_freq",
            0,
        )
        self.connect((self.soapy_rtlsdr_source, 0), (self.spectre_batched_file_sink, 0))
