import { getMenuNode } from "@/utils/auth";
const state = {
    visitedViews: [
        [],
        [],
        [],
        [],
        [],
    ],
    cachedViews: [
        [],
        [],
        [],
        [],
        [],
    ],
    iframeViews: [
        [],
        [],
        [],
        [],
        [],
    ]
}

const mutations = {
    ADD_IFRAME_VIEW: (state, view) => {
        let url = getMenuNode();
        let Index = null
        if (url) {
            if (url.includes("Workbench")) {
                Index = 0
            } else if (url.includes("MasterDataCenter")) {
                Index = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                Index = 2
            } else if (url.includes("businessCenter")) {
                Index = 3
            } else if (url.includes("ApplicationCenter")) {
                Index = 4
            }
        }
        if (Index != null) {
            if (state.iframeViews[Index].some(v => v.path === view.path)) return
            state.iframeViews[Index].push(
                Object.assign({}, view, {
                    title: view.meta.title || 'no-name'
                })
            )
        }
    },
    ADD_VISITED_VIEW: (state, view) => {
        let url = getMenuNode();
        let Index = null
        if (url) {
            if (url.includes("Workbench")) {
                Index = 0
            } else if (url.includes("MasterDataCenter")) {
                Index = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                Index = 2
            } else if (url.includes("businessCenter")) {
                Index = 3
            } else if (url.includes("ApplicationCenter")) {
                Index = 4
            }
        }
        if (Index != null) {
            if (state.visitedViews[Index].some(v => v.path === view.path)) return
            if(view.meta.affix){
                state.visitedViews[Index].unshift(
                    Object.assign({}, view, {
                        title: view.meta.title || 'no-name'
                    })
                )
            }else{
                state.visitedViews[Index].push(
                    Object.assign({}, view, {
                        title: view.meta.title || 'no-name'
                    })
                )
            }
        }

    },
    ADD_CACHED_VIEW: (state, view) => {
        let url = getMenuNode();
        let Index = null
        if (url) {
            if (url.includes("Workbench")) {
                Index = 0
            } else if (url.includes("MasterDataCenter")) {
                Index = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                Index = 2
            } else if (url.includes("businessCenter")) {
                Index = 3
            } else if (url.includes("ApplicationCenter")) {
                Index = 4
            }
        }
        if (Index != null) {
            if (state.cachedViews[Index].includes(view.name)) return
            if (view.meta && !view.meta.noCache) {
                state.cachedViews[Index].push(view.name)
            }
        }
    },
    DEL_VISITED_VIEW: (state, view) => {
        let url = getMenuNode();
        let Index = null
        if (url) {
            if (url.includes("Workbench")) {
                Index = 0
            } else if (url.includes("MasterDataCenter")) {
                Index = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                Index = 2
            } else if (url.includes("businessCenter")) {
                Index = 3
            } else if (url.includes("ApplicationCenter")) {
                Index = 4
            }
        }
        if (Index != null) {
            for (const [i, v] of state.visitedViews[Index].entries()) {
                if (v.path === view.path) {
                    state.visitedViews[Index].splice(i, 1)
                    break
                }
            }
            state.iframeViews[Index] = state.iframeViews[Index].filter(item => item.path !== view.path)
        }
    },
    DEL_IFRAME_VIEW: (state, view) => {
        let url = getMenuNode();
        let Index = null
        if (url) {
            if (url.includes("Workbench")) {
                Index = 0
            } else if (url.includes("MasterDataCenter")) {
                Index = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                Index = 2
            } else if (url.includes("businessCenter")) {
                Index = 3
            } else if (url.includes("ApplicationCenter")) {
                Index = 4
            }
        }
        if (Index != null) {
            state.iframeViews[Index] = state.iframeViews[Index].filter(item => item.path !== view.path)

        }
    },
    DEL_CACHED_VIEW: (state, view) => {
        let url = getMenuNode();
        let Index = null
        if (url) {
            if (url.includes("Workbench")) {
                Index = 0
            } else if (url.includes("MasterDataCenter")) {
                Index = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                Index = 2
            } else if (url.includes("businessCenter")) {
                Index = 3
            } else if (url.includes("ApplicationCenter")) {
                Index = 4
            }
        }
        if (Index != null) {
            const index = state.cachedViews[Index].indexOf(view.name)
            index > -1 && state.cachedViews[Index].splice(index, 1)
        }
    },

    DEL_OTHERS_VISITED_VIEWS: (state, view) => {
        let url = getMenuNode();
        let Index = null
        if (url) {
            if (url.includes("Workbench")) {
                Index = 0
            } else if (url.includes("MasterDataCenter")) {
                Index = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                Index = 2
            } else if (url.includes("businessCenter")) {
                Index = 3
            } else if (url.includes("ApplicationCenter")) {
                Index = 4
            }
        }
        if (Index != null) {
            state.visitedViews[Index] = state.visitedViews[Index].filter(v => {
                return v.meta.affix || v.path === view.path
            })
            state.iframeViews[Index] = state.iframeViews[Index].filter(item => item.path === view.path)
        }
    },
    DEL_OTHERS_CACHED_VIEWS: (state, view) => {
        let url = getMenuNode();
        let INDEX = null
        if (url) {
            if (url.includes("Workbench")) {
                INDEX = 0
            } else if (url.includes("MasterDataCenter")) {
                INDEX = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                INDEX = 2
            } else if (url.includes("businessCenter")) {
                INDEX = 3
            } else if (url.includes("ApplicationCenter")) {
                INDEX = 4
            }
        }
        if (INDEX != null) {
            const index = state.cachedViews[INDEX].indexOf(view.name)
            if (index > -1) {
                state.cachedViews[INDEX] = state.cachedViews[INDEX].slice(index, index + 1)
            } else {
                state.cachedViews[INDEX] = []
            }
        }
    },
    DEL_ALL_VISITED_VIEWS: state => {
        // keep affix tags

        let url = getMenuNode();
        let INDEX = null
        if (url) {
            if (url.includes("Workbench")) {
                INDEX = 0
            } else if (url.includes("MasterDataCenter")) {
                INDEX = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                INDEX = 2
            } else if (url.includes("businessCenter")) {
                INDEX = 3
            } else if (url.includes("ApplicationCenter")) {
                INDEX = 4
            }
        }
        if (INDEX != null) {
            const affixTags = state.visitedViews[INDEX].filter(tag => tag.meta.affix)
            state.visitedViews[INDEX] = affixTags
            state.iframeViews[INDEX] = []
        }
    },
    DEL_ALL_CACHED_VIEWS: state => {
        let url = getMenuNode();
        let INDEX = null
        if (url) {
            if (url.includes("Workbench")) {
                INDEX = 0
            } else if (url.includes("MasterDataCenter")) {
                INDEX = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                INDEX = 2
            } else if (url.includes("businessCenter")) {
                INDEX = 3
            } else if (url.includes("ApplicationCenter")) {
                INDEX = 4
            }
        }
        if (INDEX != null) {
            state.cachedViews[INDEX] = []
        }
    },
    CLEAR_VISTEDVIEWS: state => {
        let url = getMenuNode();
        let INDEX = null
        if (url) {
            if (url.includes("Workbench")) {
                INDEX = 0
            } else if (url.includes("MasterDataCenter")) {
                INDEX = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                INDEX = 2
            } else if (url.includes("businessCenter")) {
                INDEX = 3
            } else if (url.includes("ApplicationCenter")) {
                INDEX = 4
            }
        }
        if (INDEX != null) {
            state.visitedViews[INDEX] = []
        }
    },
    UPDATE_VISITED_VIEW: (state, view) => {
        let url = getMenuNode();
        let INDEX = null
        if (url) {
            if (url.includes("Workbench")) {
                INDEX = 0
            } else if (url.includes("MasterDataCenter")) {
                INDEX = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                INDEX = 2
            } else if (url.includes("businessCenter")) {
                INDEX = 3
            } else if (url.includes("ApplicationCenter")) {
                INDEX = 4
            }
        }
        if (INDEX != null) {
            for (let v of state.visitedViews[INDEX]) {
                if (v.path === view.path) {
                    v = Object.assign(v, view)
                    break
                }
            }
        }
    },
    DEL_RIGHT_VIEWS: (state, view) => {
        let url = getMenuNode();
        let INDEX = null
        if (url) {
            if (url.includes("Workbench")) {
                INDEX = 0
            } else if (url.includes("MasterDataCenter")) {
                INDEX = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                INDEX = 2
            } else if (url.includes("businessCenter")) {
                INDEX = 3
            } else if (url.includes("ApplicationCenter")) {
                INDEX = 4
            }
        }
        if (INDEX != null) {
            const index = state.visitedViews[INDEX].findIndex(v => v.path === view.path)
            if (index === -1) {
                return
            }
            state.visitedViews[INDEX] = state.visitedViews[INDEX].filter((item, idx) => {
                if (idx <= index || (item.meta && item.meta.affix)) {
                    return true
                }
                const i = state.cachedViews[INDEX].indexOf(item.name)
                if (i > -1) {
                    state.cachedViews[INDEX].splice(i, 1)
                }
                if (item.meta && item.meta.link) {
                    const fi = state.iframeViews[INDEX].findIndex(v => v.path === item.path)
                    state.iframeViews[INDEX].splice(fi, 1)
                }
                return false
            })
        }

    },
    DEL_LEFT_VIEWS: (state, view) => {
        let url = getMenuNode();
        let INDEX = null
        if (url) {
            if (url.includes("Workbench")) {
                INDEX = 0
            } else if (url.includes("MasterDataCenter")) {
                INDEX = 1
            } else if (url.includes("IntegrationTaskCenter")) {
                INDEX = 2
            } else if (url.includes("businessCenter")) {
                INDEX = 3
            } else if (url.includes("ApplicationCenter")) {
                INDEX = 4
            }
        }
        if (INDEX != null) {
            const index = state.visitedViews[INDEX].findIndex(v => v.path === view.path)
            if (index === -1) {
                return
            }
            state.visitedViews[INDEX] = state.visitedViews[INDEX].filter((item, idx) => {
                if (idx >= index || (item.meta && item.meta.affix)) {
                    return true
                }
                const i = state.cachedViews[INDEX].indexOf(item.name)
                if (i > -1) {
                    state.cachedViews[INDEX].splice(i, 1)
                }
                if (item.meta && item.meta.link) {
                    const fi = state.iframeViews[INDEX].findIndex(v => v.path === item.path)
                    state.iframeViews[INDEX].splice(fi, 1)
                }
                return false
            })
        }

    }
}

const actions = {
    addView({ dispatch }, view) {
        dispatch('addVisitedView', view)
        dispatch('addCachedView', view)
    },
    clearVisted({ commit }) {
        commit('CLEAR_VISTEDVIEWS')

    },

    addIframeView({ commit }, view) {
        commit('ADD_IFRAME_VIEW', view)
    },
    addVisitedView({ commit }, view) {
        commit('ADD_VISITED_VIEW', view)
    },
    addCachedView({ commit }, view) {
        commit('ADD_CACHED_VIEW', view)
    },
    delView({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delVisitedView', view)
            dispatch('delCachedView', view)
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },
    delVisitedView({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_VISITED_VIEW', view)
            resolve([...state.visitedViews])
        })
    },
    delIframeView({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_IFRAME_VIEW', view)
            resolve([...state.iframeViews])
        })
    },
    delCachedView({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_CACHED_VIEW', view)
            resolve([...state.cachedViews])
        })
    },
    delOthersViews({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delOthersVisitedViews', view)
            dispatch('delOthersCachedViews', view)
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },
    delOthersVisitedViews({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_OTHERS_VISITED_VIEWS', view)
            resolve([...state.visitedViews])
        })
    },
    delOthersCachedViews({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_OTHERS_CACHED_VIEWS', view)
            resolve([...state.cachedViews])
        })
    },
    delAllViews({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delAllVisitedViews', view)
            dispatch('delAllCachedViews', view)
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },
    delAllVisitedViews({ commit, state }) {
        return new Promise(resolve => {
            commit('DEL_ALL_VISITED_VIEWS')
            resolve([...state.visitedViews])
        })
    },
    delAllCachedViews({ commit, state }) {
        return new Promise(resolve => {
            commit('DEL_ALL_CACHED_VIEWS')
            resolve([...state.cachedViews])
        })
    },
    updateVisitedView({ commit }, view) {
        commit('UPDATE_VISITED_VIEW', view)
    },
    delRightTags({ commit }, view) {
        return new Promise(resolve => {
            commit('DEL_RIGHT_VIEWS', view)
            resolve([...state.visitedViews])
        })
    },
    delLeftTags({ commit }, view) {
        return new Promise(resolve => {
            commit('DEL_LEFT_VIEWS', view)
            resolve([...state.visitedViews])
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
