import dataclasses
import json

from flandre.config import CONFIG_FOLDER


@dataclasses.dataclass
class ImagingConfig:
    focus_start: int = 0
    focus_end: int = 1500
    focus_max: int = 100
    focus_min: int = 0
    focus_mmax: int = 8192
    uafm: bool = False

    bscan_start: int = 0
    bscan_end: int = 1500
    bscan_max: int = 100
    bscan_min: int = 0
    bscan_mmax: int = 8192

    dct_start: int = 300
    dct_end: int = 550

    eid: int = 0
    beta: int = 1

    tgcl: int = 0

    changed_field: str = None
    changed_field_orig_value: str | int = None

    @staticmethod
    def from_file(input_format, focus_mode, folder=CONFIG_FOLDER):
        try:
            return ImagingConfig(**json.load((folder / f'icfg_{input_format}_{focus_mode}.json').open()))
        except Exception as e:
            return ImagingConfig()

    def save(self, input_format: str, focus_mode: str, folder=CONFIG_FOLDER):
        json.dump(self.__dict__, (folder / f'icfg_{input_format}_{focus_mode}.json').open('w'))


@dataclasses.dataclass
class DeviceConfig:
    second_per_y_pix: float = 2e-8
    meter_per_x_pix: float = 2e-4

    rows: int = 1500
    cols: int = 256
    v1: int = 1915
    v1x: int = 1915
    v2: int = 5900
    y1: float = 1e-3
