library(readxl)
library(dplyr)
library(tidyverse)
library(openxlsx)
setwd("C:/Users/Marcel/Desktop/Bachelor Thesis/Xsens Data/participant_37/Excel") # <- CHANGE FOR CORRECT PARTICIPANT!!!!!!!!!!!
col_types_vector <- rep("guess", 32)
col_types_vector[31] <- "numeric"
col_types_vector[32] <- "text"

list_of_data_frames <- list()

# Assuming the files are named 'IDX_1.xlsx', 'IDX_2.xlsx', ..., 'IDX_10.xlsx'
for (i in 1:10) {
  file_name <- paste0("ID37_", i, ".xlsx") # <- UPDATE BASED ON ACTUAL PARTICIPANT!!!!!!!
  Xsensdf_block <- read_excel(file_name, col_types = col_types_vector)
  list_of_data_frames[[i]] <- Xsensdf_block
}

# Combine all data frames into one, and add a 'Block' column to each
Xsensdf <- bind_rows(list_of_data_frames, .id = "Block")
Xsensdf$Block <- as.numeric(Xsensdf$Block)

# Find indices where 'ns1:marker' equals 27
go_indices <- which(Xsensdf$`ns1:marker` == 27)

# Initialize Trial and Block columns
Xsensdf$Trial <- NA
Xsensdf$Block <- NA

current_trial <- 1
current_block <- 1

# Loop through each GO index
for (i in seq_along(go_indices)) {
    # Set the trial number starting from the GO signal
    Xsensdf$Trial[go_indices[i]] <- current_trial
    Xsensdf$Block[go_indices[i]] <- current_block
    
    # Increment trial number
    current_trial <- current_trial + 1
    
    # Check if it's time to reset the trial count and increment the block
    if (current_trial > 48) {
        current_trial <- 1
        current_block <- current_block + 1
    }
}

# Propagate trial and block values to all rows following each GO signal until the next GO signal
Xsensdf <- Xsensdf %>%
    fill(Trial, Block, .direction = "down")

# Remove rows where Trial is NA
Xsensdf <- Xsensdf %>%
  filter(!is.na(Trial))

# Identify relevant step markers
Xsensdf$is_step <- Xsensdf$`ns1:marker` %in% c(14, 15, 16, 17)

# Group data by Block and Trial to process each group
step_data <- Xsensdf %>%
  group_by(Block, Trial) %>%
  mutate(
    # Assign a row number only for step rows
    step_index = ifelse(is_step, row_number(), NA_integer_),
    # Get the index of the first step marker and the last step marker
    first_step = min(step_index, na.rm = TRUE),  # First step index
    last_step = max(step_index, na.rm = TRUE)    # Last step index
  ) %>%
  ungroup() %>%
  # Apply the buffer of 65 rows after the last step marker and from 'GO' signal
  group_by(Block, Trial) %>%
  filter(row_number() >= min(which(`ns1:marker` == 27)) & row_number() <= last_step + 65) %>%
  ungroup()

# Mutate to add 'Subject' and separate center of mass data into components
step_data <- step_data %>%
  mutate(Subject = 37) %>%
  separate(`ns1:centerOfMass`, 
           into = c("COM_displacement_X", "COM_displacement_Y", "COM_displacement_Z", 
                    "COM_velocity_X", "COM_velocity_Y", "COM_velocity_Z",
                    "COM_acceleration_X", "COM_acceleration_Y", "COM_acceleration_Z"),
           sep = " ", convert = TRUE) %>%
  select(Subject, Block, Trial, tc, `ns1:marker`,
         COM_displacement_X, COM_displacement_Y, COM_displacement_Z,
         COM_velocity_X, COM_velocity_Y, COM_velocity_Z,
         COM_acceleration_X, COM_acceleration_Y, COM_acceleration_Z)

write.xlsx(step_data, "C:/Users/Marcel/Desktop/Bachelor Thesis/Data Analysis/Relevant R Scripts/Movement Period Data (27 - Last-Step +65)/participant_37_movementperiod_go_until_last_step+65.xlsx") # <- CHANGE FOR CORRECT PARTICIPANT!!!!!!!!!!!