import time


def findStart(timerefrig="timerefrig", timerefrig_last="", ahead=0):
    timerefrig_last_stemp = int(
        time.mktime(time.strptime(timerefrig_last, "%Y-%m-%d %H:%M:%S")))
    timerefrig_stemp = [
        int(time.mktime(time.strptime(each_time, "%Y-%m-%d %H:%M:%S"))) for
        each_time in timerefrig]
    if ahead < len(timerefrig) - 1:
        time_substruct = [1000000 if timerefrig_stemp[
                                         each_timerefrig_stemp] - timerefrig_last_stemp <= 0 or each_timerefrig_stemp < ahead else
                          timerefrig_stemp[
                              each_timerefrig_stemp] - timerefrig_last_stemp
                          for each_timerefrig_stemp
                          in range(len(timerefrig_stemp))]
    else:
        time_substruct = [1000000 if timerefrig_last_stemp - timerefrig_stemp[
            each_timerefrig_stemp] <= 0 or each_timerefrig_stemp < ahead else timerefrig_last_stemp -
                                                                              timerefrig_stemp[
                                                                                  each_timerefrig_stemp]
                          for
                          each_timerefrig_stemp in
                          range(len(timerefrig_stemp))]

    index = time_substruct.index(min(time_substruct))
    return index


def refrigAnalysis(statesrefrig="", statesrefrig_off="", timerefrig="",
                   timerefrig_off="", eachdata_pow="", eachdata_cur="",
                   timer=""):
    timerefrig_chang = [each.split(" ")[1][:-3].split(":") for each in
                        timerefrig]
    timerefrig_hour = [each[0] for each in timerefrig_chang]
    timerefrig_minute = [each[1] for each in timerefrig_chang]
    timerefrig_off_chang = [each.split(" ")[1][:-3].split(":") for each in
                            timerefrig_off]
    timerefrig_off_hour = [each[0] for each in timerefrig_off_chang]
    timerefrig_off_minute = [each[1] for each in timerefrig_off_chang]
    refrigRunStages = []
    if int(timerefrig_off_hour[-1]) >= 22 and int(timerefrig_off_hour[0]) <= 2:
        refrigRunStages.append(timerefrig[0])
        refrigRunStages.append(timerefrig_off[-1])
    else:
        refrigRunStages.append(timerefrig[0])

        for each_off in range(1, len(timerefrig_off)):
            # if each_off==len(timerefrig_off)-1:
            print("timerefrig_off_hour", timerefrig_off_hour)

            if len(timerefrig_off_hour) >= 3 and int(
                    timerefrig_off_hour[-1]) - int(
                    timerefrig_off_hour[-2]) <= 2 and int(
                    timerefrig_off_hour[-2]) - int(
                    timerefrig_off_hour[-3]) <= 2 and each_off == len(
                    timerefrig_off_hour) - 1:
                refrigRunStages.append(timerefrig_off[each_off])
            if len(timerefrig_off_hour) >= 2 and int(
                    timerefrig_off_hour[-1]) - int(
                    timerefrig_off_hour[-2]) <= 2 and each_off == len(
                    timerefrig_off_hour) - 1:
                refrigRunStages.append(timerefrig_off[each_off])
            if int(timerefrig_off_hour[each_off]) - int(
                    timerefrig_off_hour[each_off - 1]) <= 2 or int(
                    timerefrig_off_hour[each_off]) < 6 or int(
                    timerefrig_off_hour[each_off]) > 21:
                continue
            else:
                if each_off < len(timerefrig) - 1:
                    refrigRunStages.append(timerefrig_off[each_off - 1])
                    findStart1 = findStart(timerefrig=timerefrig,
                                           timerefrig_last=timerefrig_off[
                                               each_off - 1], ahead=each_off)
                    refrigRunStages.append(timerefrig[findStart1])
                else:
                    refrigRunStages.append(timerefrig_off[each_off])

    return refrigRunStages
