from yattag import Doc, indent


class PicoScopeExporter:
    """
    Генератор picoscope_params.xml в формате test1_full/srv_interp.py
    """

    def generate(
        self,
        adc_values,
        adc_starts,
        path: str,
        hw,
        sampling_freq: float = 8e6,
        num_channels: int = 3,
    ):
        adc_out_timings = [int(i * sampling_freq) for i in adc_values]

        doc, tag, text, line = Doc().ttl()
        with tag("root"):
            with tag("points"):
                with tag("title"):
                    text("Points")
                with tag("value"):
                    text(str(adc_out_timings))

            with tag("num_of_channels"):
                with tag("title"):
                    text("Number of Channels")
                with tag("value"):
                    text(num_channels)

            with tag("times"):
                with tag("title"):
                    text("Times")
                with tag("value"):
                    text(str([float(i) for i in adc_starts]))

            with tag("sample_freq"):
                with tag("title"):
                    text("Sample Frequency")
                with tag("value"):
                    text(sampling_freq)

        xml_string = indent(doc.getvalue(), indentation=" " * 4, newline="\r")
        with open(f"{path}/picoscope_params.xml", "w", encoding="utf-8") as f:
            f.write(xml_string)
