from unify_api.modules.shidianu.service.aircondAnalysis import aircondAnalysis
from unify_api.modules.shidianu.service.electroAnalysis import electroAnalysis
from unify_api.modules.shidianu.service.heaterAnalysis import heaterAnalysis
from unify_api.modules.shidianu.service.pieceClassify import piececlassfy
from unify_api.modules.shidianu.service.refrigAnalysis import refrigAnalysis
import pandas as pd
import os


def pieceWise(eachdata, params="params"):
    # dataPath = "/home/ubuntu/data/code/unify_api/unify_api/modules/shidianu/service/params"
    from unify_api.modules.shidianu.service.IntergratePipe import SDU_DIR
    # dataPath = os.path.join(SDU_DIR, "service/params")
    # paramsData = pd.read_csv(os.path.join(dataPath,sid,node+".csv"))
    timer = eachdata["time"].values
    eachdata_cur = eachdata["i"].values
    eachdata_pow = eachdata["p"].values
    # params = paramsData[paramsData["node"] == node].values[0][1:]
    # time, operations, startime, endtime
    refrigRunStages = []
    aircondRunStages = []
    electroRunStages = []
    heaterRunStages = []
    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 = piececlassfy(
        current=eachdata_cur, power=eachdata_pow, timer=timer, params=params)
    if len(statesaircond_off) > 0:
        aircondRunStages = aircondAnalysis(statesaircond=statesaircond,
                                           statesaircond_off=statesaircond_off,
                                           timeaircond=timeaircond,
                                           timeaircond_off=timeaircond_off,
                                           eachdata_pow=eachdata_pow,
                                           eachdata_cur=eachdata_cur,
                                           timer=timer)
    #
    if len(stateselectro) > 0:
        electroRunStages = electroAnalysis(stateselectro=stateselectro,
                                           stateselectro_off=stateselectro_off,
                                           timeelectro=timeelectro,
                                           timeelectro_off=timeelectro_off,
                                           eachdata_pow=eachdata_pow,
                                           eachdata_cur=eachdata_cur,
                                           timer=timer)

    if len(statesheater) > 0:
        heaterRunStages = heaterAnalysis(statesheater=statesheater,
                                         statesheater_off=statesheater_off,
                                         timeheater=timeheater,
                                         timeheater_off=timeheater_off,
                                         eachdata_pow=eachdata_pow,
                                         eachdata_cur=eachdata_cur,
                                         timer=timer)

    if len(statesrefrig) > 0:
        refrigRunStages = refrigAnalysis(statesrefrig=statesrefrig,
                                         statesrefrig_off=statesrefrig_off,
                                         timerefrig=timerefrig,
                                         timerefrig_off=timerefrig_off,
                                         eachdata_pow=eachdata_pow,
                                         eachdata_cur=eachdata_cur,
                                         timer=timer)

    return aircondRunStages, electroRunStages, heaterRunStages, refrigRunStages
