import csv
import os
from trunk.data import user_input

csv_file_of_daily_data = './data/results.csv'

'''
    Last row = newest data recorded.
'''

def readFileFromCsv(csv_file_name = csv_file_of_daily_data):
    try:
        file_read = open(csv_file_name)
    except (FileNotFoundError, TypeError):
        from data import user_input
        user_input.create_CSV_only_header()
        file_read = open(csv_file_name)
    return file_read

def lastline():
    number_of_last_row_index = sum(1 for row in readFileFromCsv()) - 1
    return number_of_last_row_index

def reader(csvFileName = csv_file_of_daily_data):
    csv_reader = csv.reader(readFileFromCsv(csvFileName), delimiter=",")
    return csv_reader

def lastRowInfo(index):
    try:
        csv_reader = reader()
        go_to_last_row = 0
        for row in csv_reader:
            if go_to_last_row == index:
                blood_pressure = float(row[0])
                body_mass_index = float(row[1])
                body_weight = float(row[2])
                time_spend_exercising_minutes = int(row[5])
                cigarettes_smoked_per_day = int(row[7])
                day_of_the_week = row[8]
            else:
                go_to_last_row += 1

        returning_list = [blood_pressure, body_mass_index, body_weight, time_spend_exercising_minutes, cigarettes_smoked_per_day, day_of_the_week]
        return(returning_list)
    except (ValueError):
        return [-1]

def semiLastRowWeightComparisson(index):
    go_to_wanted_row = 0
    csv_reader = reader()
    for row in csv_reader:
        if go_to_wanted_row == index:
            weight_wanted = float(row[2])
            break
        else:
            go_to_wanted_row += 1
    return weight_wanted



import csv
import os
from trunk.data import user_input

csv_file_of_daily_data = './data/results.csv'

'''
    Last row = newest data recorded.
'''

def readFileFromCsv(csv_file_name = csv_file_of_daily_data):
    try:
        file_read = open(csv_file_name)
    except (FileNotFoundError, TypeError):
        from data import user_input
        user_input.create_CSV_only_header()
        file_read = open(csv_file_name)
    return file_read

def lastline():
    number_of_last_row_index = sum(1 for row in readFileFromCsv()) - 1
    return number_of_last_row_index

def reader(csvFileName = csv_file_of_daily_data):
    csv_reader = csv.reader(readFileFromCsv(csvFileName), delimiter=",")
    return csv_reader

def lastRowInfo(index):
    try:
        csv_reader = reader()
        go_to_last_row = 0
        for row in csv_reader:
            if go_to_last_row == index:
                blood_pressure = float(row[0])
                body_mass_index = float(row[1])
                body_weight = float(row[2])
                time_spend_exercising_minutes = int(row[5])
                cigarettes_smoked_per_day = int(row[7])
                day_of_the_week = row[8]
            else:
                go_to_last_row += 1

        returning_list = [blood_pressure, body_mass_index, body_weight, time_spend_exercising_minutes, cigarettes_smoked_per_day, day_of_the_week]
        return(returning_list)
    except (ValueError):
        return [-1]

def semiLastRowWeightComparisson(index):
    go_to_wanted_row = 0
    csv_reader = reader()
    for row in csv_reader:
        if go_to_wanted_row == index:
            weight_wanted = float(row[2])
            break
        else:
            go_to_wanted_row += 1
    return weight_wanted



Last row = newest data recorded.