import time


def componentStage(aircondRunStages="", electroRunStages="",
                   heaterRunStages="", refrigRunStages=""):
    stageComponent = []
    stampDict = {}

    if len(aircondRunStages) > 1:
        aircondRunStages_stemp = [
            int(time.mktime(time.strptime(each, "%Y-%m-%d %H:%M:%S"))) for each
            in
            aircondRunStages]
        for each_i in range(len(aircondRunStages_stemp)):
            if each_i % 2 == 0:
                stampDict[aircondRunStages_stemp[each_i]] = "开启空调"
            else:
                stampDict[aircondRunStages_stemp[each_i]] = "关闭空调"

    if len(electroRunStages) > 1:
        electroRunStages_stemp = [
            int(time.mktime(time.strptime(each, "%Y-%m-%d %H:%M:%S"))) for each
            in
            electroRunStages]
        for each_i in range(len(electroRunStages_stemp)):
            if each_i % 2 == 0:
                stampDict[electroRunStages_stemp[each_i]] = "开启电磁炉"
            else:
                stampDict[electroRunStages_stemp[each_i]] = "关闭电磁炉"

    if len(heaterRunStages) > 1:
        heaterRunStages_stemp = [
            int(time.mktime(time.strptime(each, "%Y-%m-%d %H:%M:%S"))) for each
            in heaterRunStages]
        for each_i in range(len(heaterRunStages_stemp)):
            if each_i % 2 == 0:
                stampDict[heaterRunStages_stemp[each_i]] = "开启热水器"
            else:
                stampDict[heaterRunStages_stemp[each_i]] = "关闭热水器"

    if len(refrigRunStages) > 1:
        refrigRunStages_stemp = [
            int(time.mktime(time.strptime(each, "%Y-%m-%d %H:%M:%S"))) for each
            in refrigRunStages]
        for each_i in range(len(refrigRunStages_stemp)):
            if each_i % 2 == 0:
                stampDict[refrigRunStages_stemp[each_i]] = "开启电冰箱"
            else:
                stampDict[refrigRunStages_stemp[each_i]] = "关闭电冰箱"

    stampDict = sorted(stampDict.items(), key=lambda d: d[0])
    timeOrder = [each[0] for each in stampDict]
    actionOrder = [each[1] for each in stampDict]
    timeOrder = [time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(each)) for
                 each in timeOrder]
    timestage = []
    for each_i in range(len(actionOrder)):
        stageComponent.append([])
        if each_i == 0 and actionOrder[0][:2] == "开启":
            if stageComponent[-1] == []:
                stageComponent.pop(-1)
            continue
        else:
            if actionOrder[each_i - 1][:2] == "开启":
                stageComponent[-1].append(actionOrder[each_i - 1][2:] + "运行")
                timestage.append([timeOrder[each_i - 1], timeOrder[each_i]])
                if len(timestage) > 1 and timestage[-2] == timestage[-1]:
                    timestage.pop(-1)
            if actionOrder[:each_i].count("开启电冰箱") > actionOrder[
                                                     :each_i].count(
                    "关闭电冰箱") and actionOrder[
                each_i - 1] != "开启电冰箱" and "电冰箱运行" not in stageComponent:
                stageComponent[-1].append("电冰箱运行")
                timestage.append([timeOrder[each_i - 1], timeOrder[each_i]])
                if len(timestage) > 1 and timestage[-2] == timestage[-1]:
                    timestage.pop(-1)
            if actionOrder[:each_i].count("开启电磁炉") > actionOrder[
                                                     :each_i].count(
                    "关闭电磁炉") and actionOrder[
                each_i - 1] != "开启电磁炉" and "电磁炉运行" not in stageComponent:
                stageComponent[-1].append("电磁炉运行")
                timestage.append([timeOrder[each_i - 1], timeOrder[each_i]])
                if len(timestage) > 1 and timestage[-2] == timestage[-1]:
                    timestage.pop(-1)
            if actionOrder[:each_i].count("开启热水器") > actionOrder[
                                                     :each_i].count(
                    "关闭热水器") and actionOrder[
                each_i - 1] != "开启热水器" and "热水器运行" not in stageComponent:
                stageComponent[-1].append("热水器运行")
                timestage.append([timeOrder[each_i - 1], timeOrder[each_i]])
                if len(timestage) > 1 and timestage[-2] == timestage[-1]:
                    timestage.pop(-1)
            if actionOrder[:each_i].count("开启空调") > actionOrder[:each_i].count(
                    "关闭空调") and actionOrder[
                each_i - 1] != "开启空调" and "空调运行" not in stageComponent:
                stageComponent[-1].append("空调运行")
                timestage.append([timeOrder[each_i - 1], timeOrder[each_i]])
                if len(timestage) > 1 and timestage[-2] == timestage[-1]:
                    timestage.pop(-1)
        if stageComponent[-1] == []:
            stageComponent.pop(-1)

    return stageComponent, timestage, timeOrder, actionOrder
