import { createStore } from "vuex";
import user from "@/store/modules/user";
// import routes from "@/store/modules/routes";
import getter from "./getters";
export default createStore({
    state: {
        tagsList: [],
        collapse: true,
        //stationList
        stationList: [],
        stationListAll: [],
        booststationList: [],
        //实时报警
        warningList: [],
        //实时报警列表最大长度
        warningListLimitLength: 30,
        //是否存在实时报警轮询
        warnInterval: null,
        //是否启用报警弹窗
        enableWarn: false,
        //报警弹窗持续时间
        warnTime: 5,
        alarmResetFlg: 1,
        replaceRouteName: ["safehistorywaring"],
        alarmShowNumber: (localStorage.getItem("alarmShowNumber") ? parseInt(localStorage.getItem("alarmShowNumber")) : 6),
        columnNumber: (localStorage.getItem("columnNumber") ? parseInt(localStorage.getItem("columnNumber")) : 1)
    },
    mutations: {
        delTagsItem(state, data) {
            state.tagsList.splice(data.index, 1);
        },
        setTagsItem(state, data) {
            const needReplace = state.replaceRouteName.some(routeName => {
                return routeName === data.name;
            });
            if (needReplace) {
                let someIndex = 0;
                const someRes = state.tagsList.some((ele, index) => {
                    if (ele.name === data.name) {
                        someIndex = index;
                        return ele.name === data.name;
                    }
                });
                if (someRes) {
                    state.tagsList.splice(someIndex, 1, data);
                } else {
                    state.tagsList.push(data);
                }

            } else {
                state.tagsList.push(data);
            }
        },
        clearTags(state) {
            state.tagsList = [];
        },
        closeTagsOther(state, data) {
            state.tagsList = data;
        },
        closeCurrentTag(state, data) {
            for (let i = 0, len = state.tagsList.length; i < len; i++) {
                const item = state.tagsList[i];
                if (item.path === data.$route.fullPath) {
                    if (i < len - 1) {
                        data.$router.push(state.tagsList[i + 1].path);
                    } else if (i > 0) {
                        data.$router.push(state.tagsList[i - 1].path);
                    } else {
                        data.$router.push("/");
                    }
                    state.tagsList.splice(i, 1);
                    break;
                }
            }
        },
        // 侧边栏折叠
        hadndleCollapse(state, data) {
            state.collapse = data;
        },
        // stationList
        setStationList(state, data) {
            state.stationList = data;
        },
        setStationListAll(state, data) {
            state.stationListAll = data;
        },
        setBooststationList(state, data) {
            state.booststationList = data;
        },
        // --------------------------------------------------------------------
        //实时报警
        setWarning(state, data) {
            setTimeout(() => {
                state.warningList.unshift(data);
            }, 0);
        },

        removeWarning(state, data) {
            if (Array.isArray(data)) {
                data.forEach(ele => {
                    for (let i = 0; i < state.warningList.length; i++) {
                        if (`${ele.id}${ele.ts}` === `${state.warningList[i].id}${state.warningList[i].ts}`) {
                            setTimeout(() => {
                                state.warningList.splice(i, 1);
                            }, 0);
                            break;
                        }
                    }
                })
            } else {
                for (let i = 0; i < state.warningList.length; i++) {
                    if (`${state.warningList[i].id}${state.warningList[i].ts}` === `${data.id}${data.ts}`) {
                        state.warningList.splice(i, 1);
                        break;
                    }
                }
            }
        },

        emptyWarning(state) {
            state.warningList = [];
        },

        //是否存在实时报警轮询
        setWarnInterval(state, data) {
            state.warnInterval = data;
        },
        //是否启用报警弹窗
        setEnableWarn(state, data) {
            state.enableWarn = data;
        },
        setWarnTime(state, data) {
            state.warnTime = data;
        },
        changeAlarmResetFlg(state, data) {
            state.alarmResetFlg += 1;
        },
        changeAlarmShowNumber(state, data) {
            state.alarmShowNumber = data;
        },
        changeColumnNumber(state, data) {
            state.columnNumber = data;
        },
    },
    actions: {
        actionsWarning(context, newData) {
            context.commit("setWarning", newData);
        },
        removeWarning(context, newData) {
            context.commit("removeWarning", newData);
        },
        emptyWarning(context, newData) {
            context.commit("emptyWarning", newData);
        },
        //stationList
        actionsStationList(context, newData) {
            context.commit("setStationList", newData);
        },
        actionsStationListAll(context, newData) {
            context.commit("setStationListAll", newData);
        },
        actionsBooststationList(context, newData) {
            context.commit("setBooststationList", newData);
        },
        //是否存在实时报警轮询
        actionsWarnInterval(context, newData) {
            context.commit("setWarnInterval", newData);
        },
        changeAlarmResetFlg(context) {
            context.commit("changeAlarmResetFlg");
        },
        changeAlarmShowNumber(context, newData) {
            context.commit("changeAlarmShowNumber", newData);
        },
        changeColumnNumber(context, newData) {
            context.commit("changeColumnNumber", newData);
        },
    },
    modules: { user, },
    getters: getter,
});