import numpy as np
import re


def piececlassfy(current="eachnode_cur", power="eachnode_pow", timer="timer",
                 params="params"):
    month = timer[0].split(" ")[0].split("-")[1]
    day = timer[0].split(" ")[0].split("-")[2]
    hours = [each.split(" ")[1].split(":")[0] for each in timer]
    dataLen = len(current)
    his_p = []
    his_i = []
    now_p = []
    now_i = []
    operations = []
    runingEquip = []
    statesaircond = []
    statesrefrig = []
    stateselectro = []
    statesricecook = []
    statesheater = []
    timeaircond = []
    timerefrig = []
    timericecook = []
    timeelectro = []
    timeheater = []

    statesaircond_off = []
    statesrefrig_off = []
    stateselectro_off = []
    statesricecook_off = []
    statesheater_off = []
    timeaircond_off = []
    timerefrig_off = []
    timericecook_off = []
    timeelectro_off = []
    timeheater_off = []

    quannums = [.01, .03, .05, .1, .2, .3, .4, .5, .6, .7, .8, .9, .95, .98,
                .99]
    opernumstate = []
    for i in range(dataLen):
        if i == 0:
            now_p.append(power[i])
            now_i.append(current[i])
            timerefrig.append(timer[i])
            continue
        if i < 6:
            now_p.append(power[i])
            now_i.append(power[i])
            continue
        if i >= 6:
            now_p.append(power[i])
            now_i.append(power[i])

            his_i.append(now_i[0])
            his_p.append(now_p[0])
            now_p = now_p[-5:]
            now_i = now_i[-5:]
            if 1000 * (now_p[0] - his_p[-1]) > params[0] - 50 and 1000 * (
                    now_p[0] - his_p[-1]) < min(params[1:]) + 50:
                statesrefrig.append("电冰箱运行")
                timerefrig.append(timer[i])

            if 1000 * (now_p[0] - his_p[-1]) > params[1] - 50 and 1000 * (
                    now_p[0] - his_p[-1]) < min(params[2:]) + 50:
                statesaircond.append("空调运行")
                timeaircond.append(timer[i])
            if 1000 * (now_p[0] - his_p[-1]) > min(params[2:]) - 50 and (
                    int(hours[i]) >= 22 or int(hours[i]) < 5 or int(
                hours[i]) >= 19):
                statesheater.append("热水器启动")
                timeheater.append(timer[i])
            if 1000 * (now_p[0] - his_p[-1]) > min(
                    params[3:]) - 50 and 1000 * (now_p[0] - his_p[-1]) < max(
                    params[3:]) + 50 and (
                    int(hours[i]) >= 10 and int(hours[i]) < 14 or int(
                hours[i]) >= 17 and int(hours[i]) < 20 or int(
                hours[i]) >= 5 and int(hours[i]) < 10):
                statesricecook.append("电饭煲启动")
                timericecook.append(timer[i])
            if 1000 * (now_p[0] - his_p[-1]) > max(params[3:]) - 50 and (
                    int(hours[i]) >= 10 and int(hours[i]) < 14 or int(
                hours[i]) >= 17 and int(hours[i]) < 20 or int(
                hours[i]) >= 5 and int(hours[i]) < 10):
                stateselectro.append("电磁炉运行")
                timeelectro.append(timer[i])
            # return 0
            if 1000 * (his_p[-1] - now_p[0]) > params[0] - 50 and 1000 * (
                    his_p[-1] - now_p[0]) < min(params[1:]) + 50:
                statesrefrig_off.append("电冰箱关闭")
                timerefrig_off.append(timer[i])

            if 1000 * (his_p[-1] - now_p[0]) > params[1] - 50 and 1000 * (
                    his_p[-1] - now_p[0]) < min(params[2:]) + 50:
                statesaircond_off.append("空调关闭")
                timeaircond_off.append(timer[i])
            if 1000 * (his_p[-1] - now_p[0]) > min(params[2:]) - 50 and (
                    int(hours[i]) >= 22 or int(hours[i]) < 5 or int(
                hours[i]) >= 19):
                statesheater_off.append("热水器关闭")
                timeheater_off.append(timer[i])
            if 1000 * (his_p[-1] - now_p[0]) > min(
                    params[3:]) - 50 and 1000 * (his_p[-1] - now_p[0]) < max(
                    params[3:]) + 50 and (
                    int(hours[i]) >= 10 and int(hours[i]) < 14 or int(
                hours[i]) >= 17 and int(hours[i]) < 20 or int(
                hours[i]) >= 5 and int(hours[i]) < 10):
                statesricecook_off.append("电饭煲关闭")
                timericecook_off.append(timer[i])
            if 1000 * (his_p[-1] - now_p[0]) > max(params[3:]) - 50 and (
                    int(hours[i]) >= 10 and int(hours[i]) < 14 or int(
                hours[i]) >= 17 and int(hours[i]) < 20 or int(
                hours[i]) >= 5 and int(hours[i]) < 10):
                stateselectro_off.append("电磁炉关闭")
                timeelectro_off.append(timer[i])
    return statesaircond, statesrefrig, stateselectro, statesricecook, statesheater, timeaircond, timerefrig, timericecook, timeelectro, timeheater, statesaircond_off, statesrefrig_off, stateselectro_off, statesricecook_off, statesheater_off, timeaircond_off, timerefrig_off, timericecook_off, timeelectro_off, timeheater_off