import numpy as np
import pandas as pd


data_paths = ["Data/WalkingStairs", "Data/Standing",
              "Data/Walking", "Data/Fahrstuhl"]


def read_data_files(path):
    accelerometer = pd.read_csv(
        path + "/Accelerometer.csv", sep=";", skiprows=1, header=0, names=["t", "ax", "ay", "az"])

    barometer = pd.read_csv(path + "/Barometer.csv", sep=";",
                            skiprows=1, header=0, names=["t", "bx", "by", "bz"])

    events = pd.read_csv(path + "/Events.txt", sep=";",
                         skiprows=0, header=None, names=["t", "event"])

    data = pd.merge(accelerometer, barometer, on="t", how="outer")
    data = pd.merge(data, events, on="t", how="outer")
    data = data.sort_values('t')

    # Fill all nan values in event with the last known event
    data['event'] = data['event'].fillna(method='pad')

    # Add additional magnitude column
    data['magnitude'] = np.sqrt(data['ax']**2 + data['ay']**2 + data['az']**2)

    return data


def increment_timestamps(data_frames):
    """
    data_frames: list
    """
    max_t = 0
    for frame in data_frames:
        frame['t'] += max_t
        max_t = frame['t'].iloc[-1]

    return data_frames


def merge_data_frames(data_frames):
    df = pd.DataFrame()

    for frame in data_frames:
        df = df.append(frame, ignore_index=True)

    return df


def load_all_data():
    print("Loading data files...")
    d = []
    for path in data_paths:
        data = read_data_files(path)
        d.append(data)

    d = increment_timestamps(d)
    data = merge_data_frames(d)

    return data
