import time


def findStart(timeheater="timeheater", timeheater_last="", ahead=0):
    timeelectro_last_stemp = int(
        time.mktime(time.strptime(timeheater_last, "%Y-%m-%d %H:%M:%S")))
    timeelectro_stemp = [
        int(time.mktime(time.strptime(each_time, "%Y-%m-%d %H:%M:%S"))) for
        each_time in timeheater]
    if ahead < len(timeheater) - 1:
        time_substruct = [1000000 if timeelectro_stemp[
                                         each_timeelectro_stemp] - timeelectro_last_stemp <= 0 or each_timeelectro_stemp < ahead else
                          timeelectro_stemp[
                              each_timeelectro_stemp] - timeelectro_last_stemp
                          for each_timeelectro_stemp
                          in range(len(timeelectro_stemp))]
    else:
        time_substruct = [
            1000000 if timeelectro_last_stemp - timeelectro_stemp[
                each_timeelectro_stemp] <= 0 or each_timeelectro_stemp < ahead else timeelectro_last_stemp -
                                                                                    timeelectro_stemp[
                                                                                        each_timeelectro_stemp]
            for
            each_timeelectro_stemp in range(len(timeelectro_stemp))]

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


def heaterAnalysis(statesheater="statesheater", statesheater_off="",
                   timeheater="timeheater",
                   timeheater_off="timeheater_off",
                   eachdata_pow="eachdata_pow", eachdata_cur="eachdata_cur",
                   timer="timer"):
    heaterRunStages = []

    timeheater_chang = [each.split(" ")[1][:-3].split(":") for each in
                        timeheater]
    timeheater_hour = [each[0] for each in timeheater_chang]
    timeheater_minute = [each[1] for each in timeheater_chang]
    timeheater_off_chang = [each.split(" ")[1][:-3].split(":") for each in
                            timeheater_off]
    timeheater_off_hour = [each[0] for each in timeheater_off_chang]
    timeheater_off_minute = [each[1] for each in timeheater_off_chang]

    flag = 0
    heaterRunStages = []
    if len(timeheater) > 1:
        heaterRunStages.append(timeheater[0])

        for each_off in range(1, len(timeheater_off)):
            # if each_off==len(timeaircond_off)-1:
            if int(timeheater_off_hour[-1]) - int(
                    timeheater_off_hour[-2]) > 2 and each_off == len(
                    timeheater_off_hour) - 1:
                if flag == 1:
                    index = findStart(timeheater=timeheater,
                                      timeheater_last=timeheater_off[each_off],
                                      ahead=each_off)
                    heaterRunStages.append(timeheater[index])
                heaterRunStages.append(timeheater_off[each_off])
            elif int(timeheater_off_hour[each_off]) - int(
                    timeheater_off_hour[each_off - 1]) <= 1:
                continue
            else:
                if each_off < len(timeheater) - 1:
                    heaterRunStages.append(timeheater_off[each_off - 1])
                    index = findStart(timeheater=timeheater,
                                      timeheater_last=timeheater_off[
                                          each_off - 1],
                                      ahead=each_off)
                    heaterRunStages.append(timeheater[index])
                    if int(timeheater_off_hour[each_off]) - int(
                            timeheater_hour[index]) <= 1 and int(
                            timeheater_off_hour[each_off]) - int(
                        timeheater_off_hour[each_off + 1]) < -1:
                        heaterRunStages.append(timeheater_off[each_off])
                        flag = 1

                else:
                    heaterRunStages.append(timeheater_off[each_off])
                    flag = 1
    else:
        heaterRunStages.append(timeheater[0])
        heaterRunStages.append(timeheater_off[0])

    return heaterRunStages
