import { JsonDecoder } from 'ts.data.json';
import { Geometry } from 'dasf-web/lib/model/Features';
import { Properties, Gauge, Gauges } from './Gauge';

const GeometryDecoder = JsonDecoder.object<Geometry>(
    {
        coordinates: JsonDecoder.array<number>(JsonDecoder.number, 'number[]'),
        type: JsonDecoder.string
    },
    'Geometry'
);

const PropertiesDecoder = JsonDecoder.object<Properties>(
    {
        gid: JsonDecoder.number,
        name: JsonDecoder.string,
        river: JsonDecoder.failover("N/A", JsonDecoder.string)
    },
    'Properties'
);

const GaugeFeatureDecoder = JsonDecoder.object<Gauge>(
    {
        geometry: GeometryDecoder,
        properties: PropertiesDecoder,
        type: JsonDecoder.string,
        returnPeriod: JsonDecoder.succeed,
        discharge: JsonDecoder.succeed,
        floodEventDischarge: JsonDecoder.succeed,
        basin: JsonDecoder.succeed
    },
    'Gauge'
);

const GaugesDecoder = JsonDecoder.object<Gauges>(
    {
        features: JsonDecoder.array<Gauge>(GaugeFeatureDecoder, 'Gauge[]'),
        type: JsonDecoder.string
    },
    'Gauges'
);

export { GaugesDecoder };