import time


def findStart(timeelectro="timeelectro", timeelectro_last="", ahead=0):
    timeelectro_last_stemp = int(
        time.mktime(time.strptime(timeelectro_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 timeelectro]

    if ahead < len(timeelectro) - 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 electroAnalysis(stateselectro="stateselectro",
                    stateselectro_off="stateselectro_off"
                    , timeelectro="timeelectro",
                    timeelectro_off="timeelectro_off",
                    eachdata_pow="eachdata_pow"
                    , eachdata_cur="eachdata_cur", timer="timer"):
    electroRunStages = []

    timeelectro_chang = [each.split(" ")[1][:-3].split(":") for each in
                         timeelectro]
    timeelectro_hour = [each[0] for each in timeelectro_chang]
    timeelectro_minute = [each[1] for each in timeelectro_chang]
    timeelectro_off_chang = [each.split(" ")[1][:-3].split(":") for each in
                             timeelectro_off]
    timeelectro_off_hour = [each[0] for each in timeelectro_off_chang]
    timeelectro_off_minute = [each[1] for each in timeelectro_off_chang]

    flag = 0
    electroRunStages = []
    electroRunStages.append(timeelectro[0])
    for each_off in range(1, len(timeelectro_off)):
        # if each_off==len(timeaircond_off)-1:
        if int(timeelectro_off_hour[-1]) - int(
                timeelectro_off_hour[-2]) > 2 and each_off == len(
                timeelectro_off_hour) - 1:
            if flag == 1:
                index = findStart(timeelectro=timeelectro,
                                  timeelectro_last=timeelectro_off[each_off],
                                  ahead=each_off)
                electroRunStages.append(timeelectro[index])
            electroRunStages.append(timeelectro_off[each_off])
        elif int(timeelectro_off_hour[each_off]) - int(
                timeelectro_off_hour[each_off - 1]) <= 1:
            continue
        else:
            if each_off < len(timeelectro_off_hour) - 1:
                electroRunStages.append(timeelectro_off[each_off - 1])
                index = findStart(timeelectro=timeelectro,
                                  timeelectro_last=timeelectro_off[
                                      each_off - 1],
                                  ahead=each_off)
                electroRunStages.append(timeelectro[index])
                if int(timeelectro_off_hour[each_off]) - int(
                        timeelectro_hour[index]) <= 1 and int(
                        timeelectro_off_hour[each_off]) - int(
                    timeelectro_off_hour[each_off + 1]) < -1:
                    electroRunStages.append(timeelectro_off[each_off])
                    flag = 1

            else:
                electroRunStages.append(timeelectro_off[each_off])
                flag = 1

    return electroRunStages
