import { useAjax } from "@/Hook/useAjax";
import { editUser, modifyPasswordAjax } from "@/services/user";
import { isFull } from "@/utils/fullScreen";
import { useEffect, useReducer } from "react";
import { useModel } from "umi";
type State = {
    selectdUserId?: number,//当前选中的用户
    selectdName?: string,
    selectdUserIdYyb?: number,//当前选中的用户
    selectdNameYyb?: string,
    isFell?: boolean
}
type Action = {
    params?: any,
    type: 'setUserId' | 'initUserId' | 'setUserIdYyb' | 'initUserIdYyb' | 'setFell',
}
export function reducer(state: State, action: Action) {
    let { type, params } = action
    switch (type) {
        case 'setUserId':
            return { ...state, selectdUserId: params?.userId, selectdName: params?.name }
        case 'initUserId':
            return { ...state, selectdUserId: params?.userId, selectdName: params?.name }
        case 'setUserIdYyb':
            return { ...state, selectdUserIdYyb: params?.userId, selectdNameYyb: params?.name }
        case 'initUserIdYyb':
            return { ...state, selectdUserIdYyb: params?.userId, selectdNameYyb: params?.name }
        case 'setFell':
            return { ...state, isFell: params?.isFell }
        default:
            return state;
    }
}
export default function useUser() {
    const { currentUser }: any = useModel('@@initialState', model => ({ currentUser: model.initialState?.currentUser }))
    const { initialState, set }: any = useModel('@@initialState', model => ({ initialState: model.initialState, set: model.setInitialState }))
    const edit = useAjax((parmas: { oldPassword: string, password: string }) => editUser(parmas))
    const modifyPassword = useAjax((parmas: { userId: string, password: string, oldPassword: string }) => modifyPasswordAjax(parmas))
    const [state, dispatch] = useReducer(reducer, { selectdUserId: 0, selectdName: '', isFell: false })
    const initUserId = () => {
        dispatch({ type: 'initUserId', params: { userId: currentUser?.userId, name: currentUser?.name } })
    }
    const initUserIdYyb = () => {
        dispatch({ type: 'initUserIdYyb', params: { userId: currentUser?.userId, name: currentUser?.name } })
    }
    //起始选中的是当前登录的账号ID
    useEffect(() => {
        if (currentUser?.userId) {
            dispatch({ type: 'setUserId', params: { userId: currentUser?.userId, name: currentUser?.name } })
            dispatch({ type: 'setUserIdYyb', params: { userId: currentUser?.userId, name: currentUser?.name } })
        }
    }, [currentUser?.userId])
    //控制左侧菜单缩进
    useEffect(() => {
        let orignalSetItem: any = localStorage.setItem;
        localStorage.setItem = function (key, newValue) {
            let setItemEvent: any = new Event("setItemEvent");
            setItemEvent.key = key;
            setItemEvent.newValue = newValue;
            setItemEvent.oldValue = localStorage.getItem(key);
            window.dispatchEvent(setItemEvent);
            orignalSetItem.apply(this, arguments);
        }
        let fn = (e: any) => {
            if (e?.key === 'isFell') {
                dispatch({ type: 'setFell', params: { isFell: !!Number(e?.newValue) } })
            }
            set({ ...initialState, collapsed: e.newValue })
        }
        window.addEventListener("setItemEvent", fn);
        return () => {
            window.removeEventListener("setItemEvent", fn)
        }
    }, [initialState, set])
    // }, [initialState, set, history])
    useEffect(() => {
        function key() {
            if (!isFull()) {
                dispatch({ type: 'setFell', params: { isFell: false } })
            }
        }
        window.addEventListener('resize', key)
        return () => {
            window.removeEventListener("resize", key)
        }
    }, [])
    return {
        state,
        dispatch,
        initUserId,
        initUserIdYyb,
        edit,
        modifyPassword
    }
}