"""
maintimeinzone.py
This file finds the number of bg data entries in the different bg zones.

MedicineX OpenAPS
2017-8-30
"""

from collections import namedtuple
from bgdata import get_bg_dataframe
from bgdata import get_bg_index
from bgdata import get_new_df_entries_every_5_minutes
from timeinzonedata import *


"""
MODIFY THE VARIABLES BELOW TO RUN DATA
"""
#Array of the ID to use. Put ID Number as a string (e.g. "00000001")
#["00000001", "00000003", "00000004", "00000007", "00000010", "00000011",
# "00000013", "00000015", "00000016", "00000017", "00000020", "00000021",
# "00000023", "00000024"]
ID_ARRAY = np.array(["00000001", "00000003", "00000004", "00000007", "00000010", "00000011",
"00000013", "00000015", "00000016", "00000017", "00000020", "00000021",
"00000023", "00000024"])

"""
END
"""




"""
Modify the plotting variable below if needed
"""



"""
Constants below
"""

#ID CONSTANTS
ID_LIST = [
    "00000001", "00000003", "00000004", "00000007", "00000010", "00000011",
    "00000013", "00000015", "00000016", "00000017", "00000020", "00000021",
    "00000023", "00000024"]
#They work, but they don't have enough days to make accurate data charts
WORK_BUT_SMALL_ID_LIST = ["00000000", "00000005", "00000008", "00000009", "00000012", "00000018", "00000019", "00000025"]
#THESE DON'T WORK!!!!! (e.g. they have loop algorithm only, no data at all)
BAD_ID_LIST = ["00000002", "00000006", "00000014", "00000022"]

#Start and end training set dates
START_TRAIN_DATE_DICT = {
    "00000000": "2017-02-03", "00000001": "2016-10-09", "00000002": "2017-02-03",
    "00000003": "2017-01-06", "00000004": "2017-01-06", "00000005": "2017-02-09",
    "00000006": "2017-01-10", "00000007": "2017-01-07", "00000008": "2017-01-25",
    "00000009": "2017-02-10", "00000010": "2017-01-06", "00000011": "2016-12-11",
    "00000012": "2017-01-08", "00000013": "2017-05-16", "00000015": "2017-04-02",
    "00000016": "2017-06-22", "00000017": "2017-05-19", "00000018": "2016-07-07",
    "00000019": "2017-05-27", "00000020": "2017-04-01", "00000021": "2017-06-13",
    "00000023": "2016-11-07", "00000024": "2017-04-20", "00000025": "2017-04-20"}

END_TRAIN_DATE_DICT = {
    "00000000": "2017-02-07", "00000001": "2016-10-18", "00000002": "2017-02-07",
    "00000003": "2017-01-15", "00000004": "2017-01-15", "00000005": "2017-02-13",
    "00000006": "2017-01-21", "00000007": "2017-01-16", "00000008": "2017-01-25",
    "00000009": "2017-02-15", "00000010": "2017-01-15", "00000011": "2016-12-20",
    "00000012": "2017-01-24", "00000013": "2017-05-25", "00000015": "2017-04-11",
    "00000016": "2017-07-01", "00000017": "2017-05-28", "00000018": "2016-07-16",
    "00000019": "2017-06-02", "00000020": "2017-04-10", "00000021": "2017-06-22",
    "00000023": "2016-11-16", "00000024": "2017-04-29", "00000025": "2017-04-27"}

#Start and end validation set dates
START_VALID_DATE_DICT = {
    "00000000": "2017-02-08", "00000001": "2016-10-19", "00000002": "2017-02-03",
    "00000003": "2017-01-16", "00000004": "2017-01-16", "00000005": "2017-02-09",
    "00000006": "2017-01-10", "00000007": "2017-01-17", "00000008": "2017-01-25",
    "00000009": "2017-02-10", "00000010": "2017-01-16", "00000011": "2016-12-21",
    "00000012": "2017-01-08", "00000013": "2017-05-26", "00000015": "2017-04-12",
    "00000016": "2017-07-02", "00000017": "2017-05-29", "00000018": "2016-07-17",
    "00000019": "2017-06-03", "00000020": "2017-04-11", "00000021": "2017-06-23",
    "00000023": "2016-11-17", "00000024": "2017-04-30", "00000025": "2017-04-28"}

END_VALID_DATE_DICT = {
    "00000000": "2017-02-09", "00000001": "2016-10-22", "00000002": "2017-02-07",
    "00000003": "2017-01-19", "00000004": "2017-01-19", "00000005": "2017-02-13",
    "00000006": "2017-01-21", "00000007": "2017-01-20", "00000008": "2017-01-25",
    "00000009": "2017-02-15", "00000010": "2017-01-19", "00000011": "2016-12-24",
    "00000012": "2017-01-24", "00000013": "2017-05-29", "00000015": "2017-04-15",
    "00000016": "2017-07-05", "00000017": "2017-06-01", "00000018": "2016-07-20",
    "00000019": "2017-06-04", "00000020": "2017-04-14", "00000021": "2017-06-26",
    "00000023": "2016-11-20", "00000024": "2017-05-03", "00000025": "2017-04-30"}

#Start and end test set dates
START_TEST_DATE_DICT = {
    "00000000": "2017-02-10", "00000001": "2016-10-23", "00000002": "2017-02-08",
    "00000003": "2017-01-20", "00000004": "2017-01-20", "00000005": "2017-02-14",
    "00000006": "2017-01-22", "00000007": "2017-01-21", "00000008": "2017-01-26",
    "00000009": "2017-02-16", "00000010": "2017-01-20", "00000011": "2016-12-25",
    "00000012": "2017-01-25", "00000013": "2017-05-30", "00000015": "2017-04-16",
    "00000016": "2017-07-06", "00000017": "2017-06-02", "00000018": "2016-07-21",
    "00000019": "2017-06-05", "00000020": "2017-04-15", "00000021": "2017-06-27",
    "00000023": "2016-11-21", "00000024": "2017-05-04", "00000025": "2017-04-31"}

END_TEST_DATE_DICT = {
    "00000000": "2017-02-12", "00000001": "2016-10-29", "00000002": "2017-02-09",
    "00000003": "2017-01-26", "00000004": "2017-01-26", "00000005": "2017-02-15",
    "00000006": "2017-01-11", "00000007": "2017-01-27", "00000008": "2017-01-26",
    "00000009": "2017-02-17", "00000010": "2017-01-26", "00000011": "2016-12-31",
    "00000012": "2017-01-26", "00000013": "2017-06-05", "00000015": "2017-04-22",
    "00000016": "2017-07-12", "00000017": "2017-06-08", "00000018": "2016-07-27",
    "00000019": "2017-06-07", "00000020": "2017-04-21", "00000021": "2017-07-03",
    "00000023": "2016-11-27", "00000024": "2017-05-10", "00000025": "2017-05-04"}


#Function to return the training, validaiton, and testing set dates given the string of the ID number
def _get_id_dates(id_str):
    return START_TRAIN_DATE_DICT[id_str], END_TRAIN_DATE_DICT[id_str], START_VALID_DATE_DICT[id_str], END_VALID_DATE_DICT[id_str], START_TEST_DATE_DICT[id_str], END_TEST_DATE_DICT[id_str]


#Main function
def main():
    for id_str in ID_ARRAY:
        print "\nID Number: " + id_str
        start_train_str, end_train_str, start_valid_str, end_valid_str, start_test_str, end_test_str = _get_id_dates(id_str)
        bg_df = get_bg_dataframe(id_str) #imports json file as a pandas dataframe

        #get start and stop indices
        bg_df, start_test_index, end_test_index = get_bg_index(bg_df, start_test_str, end_test_str, "Testing", True)

        test_bg_df, start_test_index, end_test_index = get_new_df_entries_every_5_minutes(bg_df, start_test_index, end_test_index, "New Testing")

        test_bg_array, test_bg_time_array = get_bg_array(test_bg_df, start_test_index, end_test_index)

        time_zone = get_time_in_zone(test_bg_array, test_bg_time_array)

#Run the main function
if __name__ ==  "__main__":
    main()
