import pandas as pd


def likesEquip(time):
    res = []
    for each in time:
        eachtime = str(each).split(" ")[1].split(":")[0]
        if int(eachtime) > 19 or int(eachtime) < 5:
            res.append("大功率设备疑似热水器")
        elif int(eachtime) > 18 and int(eachtime) < 19:
            res.append("大功率设备疑似洗衣机")
        elif int(eachtime) > 5 and int(eachtime) < 8:
            res.append("大功率设备疑似电吹风")
        else:
            res.append("大功率设备疑似电磁炉")
    return res


def bigPowerReg(eachdata="powerdata", key="a"):
    overload = 3500
    bigpower = 2500
    bigpowerone = [1000, 90]
    bigpowertwo = [1500, 120]
    # quanter = quancalcu(sid, key=key)
    # print(quanter)
    powerdata = eachdata["p"].values
    day_time = eachdata["time"].values.tolist()
    day_time_all = [pd.to_datetime(day_time[i], format='%Y-%m-%d %H:%M:%S') for
                    i in
                    range(len(day_time))]
    power_Series = pd.Series(powerdata, index=day_time_all)
    power_Series_diff = power_Series.diff(1)
    power_Series_diff_list = list(power_Series_diff[1:])
    flag1 = 0
    timeone = 0
    flag2 = 0
    timetwo = 0
    flag3 = 0
    timethr = 0
    res = []
    for i in range(len(power_Series_diff) - 10):

        if flag1 == 1 and power_Series_diff[i] * 1000 > -400:
            timeone = timeone + 1
            continue
        if flag1 == 1 and power_Series_diff[i] * 1000 < -1000 and powerdata[
            i] * 1000 < 2000 and max(powerdata[i + 1:i + 10]) * 1000 < 2000:
            res.append(day_time_all[i])
            timeone = 0
            flag1 = 0
        if flag2 == 1 and power_Series_diff[i] * 1000 < -500:
            if timetwo > 120 and max(powerdata[i + 1:i + 10]) * 1000 < 500:
                res.append(day_time_all[i])
            timetwo = 0
            flag2 = 0
        if flag2 == 1 and power_Series_diff[i] * 1000 > -400:
            timetwo = timetwo + 1
            continue
        if flag3 == 1 and power_Series_diff[i] * 1000 < -1500:
            if max(powerdata[i + 1:i + 10]) * 1000 < 500:
                res.append(day_time_all[i])
                print(4)
            timethr = 0
            flag3 = 0
        if flag3 == 1 and power_Series_diff[i] * 1000 > -400:
            timethr = timethr + 1
            continue
        # if powerdata[i] * 1000 > bigpowerone[0] and powerdata[
        #     i] * 1000 > quanter * 0.6 and flag1 == 0:
        #     timeone = timeone + 1
        #     flag1 = 1
        #     res.append(day_time_all[i])
        elif power_Series_diff[i] * 1000 > bigpowertwo[0] and \
                power_Series_diff[i] * 1000 < bigpower:
            res.append(day_time_all[i])
            timetwo = timetwo + 1
            flag2 = 1
        if powerdata[i] > overload:
            res.append(day_time_all[i])
            timethr = timethr + 1
            flag3 = 1
    equips = likesEquip(time=res[0::2])
    timer = [str(each) for each in res[0::2]]
    return timer, equips


def vioPowerReg(eachdata="powerdata", key="a"):
    viopowerone = [240, 150]
    viopowertwo = [120, 1000]
    viopowerthr = [3, 15, 700]

    powerdata = eachdata["p"].values
    day_time = eachdata["time"].values.tolist()
    flag1 = 0
    timeone = 0
    startimeone = ""
    flag2 = 0
    timetwo = 0
    startimetwo = ""
    flag3 = 0
    timethr = 0
    startimethr = ""
    totalthr = 0
    res = []
    equips = []
    for i in range(1, len(powerdata) - 1):

        if flag2 == 0 and powerdata[i] * 1000 > viopowertwo[1]:
            timetwo = timetwo + 1
            startimetwo = day_time[i]
            flag2 = 1
            continue
        if flag2 == 1 and powerdata[i] * 1000 < viopowertwo[1] and timetwo >= \
                viopowertwo[0]:
            if int(str(day_time[i]).split(" ")[1].split(":")[0]) > 5 and int(
                    str(day_time[i]).split(" ")[1].split(":")[0]) < 22:
                res.append(startimetwo)
                equips.append("违规电器疑似电磁炉")
            else:
                res.append(startimetwo)
                equips.append("违规电器疑似大功率充电器")
            flag2 = 0
            startimetwo = ""
            timetwo = 0
            continue
        if flag2 == 1 and powerdata[i] * 1000 < viopowertwo[1]:
            flag2 = 0
            startimetwo = ""
            timetwo = 0
        if flag2 == 1 and powerdata[i] * 1000 > viopowertwo[1]:
            timetwo = timetwo + 1
            continue
        if flag2 == 1 and powerdata[i] * 1000 < viopowertwo[1] and timeone < \
                viopowertwo[0]:
            flag2 = 0
            startimetwo = ""
            timetwo = 0
            continue
        if flag3 == 0 and abs(powerdata[i] - powerdata[i - 1]) * 1000 > \
                viopowerthr[2]:
            timethr = timethr + 1
            startimethr = day_time[i]
            totalthr = 1
            flag3 = 1
            continue
        if flag3 == 1 and timethr > viopowerthr[1] and totalthr < viopowerthr[
            0]:
            startimethr = ""
            totalthr = 0
            flag3 = 0
            timethr = 0
        if flag3 == 1 and abs(powerdata[i] - powerdata[i - 1]) * 1000 < \
                viopowerthr[2] and timethr < viopowerthr[1]:
            timethr = timethr + 1
        if flag3 == 1 and abs(powerdata[i] - powerdata[i - 1]) * 1000 > \
                viopowerthr[2] and timethr < viopowerthr[1]:
            totalthr = totalthr + 1
            timethr = timethr + 1
        if flag3 == 1 and timethr <= viopowerthr[1] and totalthr >= \
                viopowerthr[0] and powerdata[i] > 1.5:
            res.append(startimethr)
            equips.append("违规电器疑似电磁炉")
            startimethr = ""
            totalthr = 0
            flag3 = 0
            timethr = 0
            continue
        if flag1 == 0 and powerdata[i] * 1000 > viopowerone[1]:
            timeone = timeone + 1
            startimeone = day_time[i]
            flag1 = 1
            continue
        if flag1 == 1 and powerdata[i] * 1000 < viopowerone[1] and timeone >= \
                viopowerone[0]:
            res.append(startimeone)
            equips.append("违规电器疑似电动自行车")
            flag1 = 0
            startimeone = ""
            timeone = 0
            continue
        if flag1 == 1 and powerdata[i] * 1000 < viopowerone[1]:
            flag1 = 0
            startimeone = ""
            timeone = 0
        if flag1 == 1 and powerdata[i] * 1000 > viopowerone[1]:
            timeone = timeone + 1
            continue
    timer = [str(each) for each in res]
    return timer, equips

# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
#
# sidpath = "D:\gas\data\elect\struct\sort"
# keys = ["pa", "pb", "pc"]
# sids = os.listdir(sidpath)
# for sid in sids:
#     print(sid)
#     files = os.listdir(os.path.join(sidpath, sid))
#     for file in files:
#         print(file)
#         eachpath = os.path.join(sidpath, sid, file)
#         eachdata = pd.read_csv(eachpath, index_col=0)
#         for key in keys:
#             res, equips = bigPowerReg(eachdata=eachdata, key=key)
#             fig, ax = plt.subplots()
#             ax.plot(eachdata[key].values)
#             if len(res) % 2 != 0:
#                 res.pop(-1)
#             for each in range(len(res)):
#                 index = eachdata["time"].values.tolist().index(str(res[each]))
#                 # if each % 2==0:
#                 plt.annotate(equips[each],
#                              xy=(index, eachdata[key].values.tolist()[index]),
#                              xytext=(index, 0.5 + eachdata[key][index]),
#                              arrowprops=dict(arrowstyle='->'), fontsize=6)
#                 # else:
#                 # plt.annotate("关闭大功率设备", xy=(index, eachdata[key].values.tolist()[index]),
#                 #              xytext=(index ,  1+eachdata[key][index]), arrowprops=dict(arrowstyle='->'), fontsize=6)
#             res, equips = vioPowerReg(eachdata=eachdata, key=key)
#             for each in range(len(res)):
#                 index = eachdata["time"].values.tolist().index(str(res[each]))
#                 plt.annotate(equips[each],
#                              xy=(index, eachdata[key].values.tolist()[index]),
#                              xytext=(index, eachdata[key][index]),
#                              arrowprops=dict(arrowstyle='->'), fontsize=6)
#
#             plt.show()
#             print(res)