import json
from datetime import datetime, timedelta

import records as r

def get_records() -> list[r.Collection]:
    """Returns the trial data as a list of `records.Collection` objects."""

    json_data = []
    with open('data.json', 'r') as file:
        json_data = json.load(file)

    def incremented(time: datetime) -> datetime:
        if time.hour == 22:
            time += timedelta(hours=12)
        else:
            time += timedelta(hours=3)
        return time

    records: list[r.Collection] = []
    time = datetime.fromisoformat("2023-08-14T10:00:00")

    for item in json_data:
        p1 = item['p1']
        home_environment = item['home']

        if p1:
            mews = None
            blood_pressure = None
            body_temp = None
            heart_rate = None
            respiration_score = None
            spo2 = None
            s1 = None
            b1 = None
            t1 = None
            p2 = None
            s2 = None
            b2 = None
            t2 = None
            uplink = None
            downlink = None
            rtt = None
        else:
            blood_pressure = r.BloodPressure(
                time=datetime.fromisoformat(item['blood_pressure']['time']),
                value_systolic=item['blood_pressure']['value_systolic'],
                value_diastolic=item['blood_pressure']['value_systolic']
            )
            body_temp = r.BodyTemp(
                time=datetime.fromisoformat(item['body_temp']['time']),
                value=item['body_temp']['value']
            )
            heart_rate = r.HeartRate(
                time=datetime.fromisoformat(item['heart_rate']['time']),
                value=item['heart_rate']['value']
            )
            respiration_score = r.RespirationScore(
                time=datetime.fromisoformat(item['respiration_score']['time']),
                value=item['respiration_score']['value']
            )
            spo2 = r.Spo2(
                time=datetime.fromisoformat(item['spo2']['time']),
                value=item['spo2']['value']
            )
            s1 = item['s1']
            b1 = item['b1']
            t1 = item['t1']
            p2 = item['p2']
            s2 = item['s2']
            b2 = item['b2']
            t2 = item['t2']
            uplink = item['uplink']
            downlink = item['downlink']
            rtt = item['rtt']
            if s2 or b2 or t2:
                mews = None
            else:
                mews = r.Mews(
                    time=datetime.fromisoformat(item['mews']['time']),
                    value=item['mews']['value']
                )

        records.append(r.Collection(
            notification_time=time,
            mews=mews,
            blood_pressure=blood_pressure,
            body_temp=body_temp,
            heart_rate=heart_rate,
            respiration_score=respiration_score,
            spo2=spo2,
            s1=s1,
            b1=b1,
            t1=t1,
            s2=s2,
            b2=b2,
            t2=t2,
            p1=p1,
            p2=p2,
            home_environment=home_environment,
            uplink=uplink,
            downlink=downlink,
            rtt=rtt
        ))

        time = incremented(time)

    return records
