import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
import Taro from '@tarojs/taro';
interface UserState {
    nickname: string | null;
    avatar: string | null;
    token: string | null;
    isLogin: boolean;
}


// const initialState: UserState = await getUserDataFromLocalStorage();
const initialState: UserState = {
    isLogin: false,
    nickname: null,
    avatar: null,
    token: null,
}

const userSlice = createSlice({
    name: 'user',
    initialState,
    reducers: {
        loginSuccess(state, action: PayloadAction<any>) {
            const { nickname, avatar, token } = action.payload;
            state.nickname = nickname;
            state.avatar = avatar;
            state.token = token;
            state.isLogin = true;
            global.token = token;
            Taro.setStorage({ key: 'userData', data: JSON.stringify(action.payload) });
        },
        registerSuccess(state, action: PayloadAction<any>) {
            const { nickname, avatar, token } = action.payload;
            state.nickname = nickname;
            state.avatar = avatar;
            state.token = token;
            state.isLogin = true;
            global.token = token;
            Taro.setStorage({ key: 'userData', data: JSON.stringify(action.payload) });
        },
        logoutSuccess(state) {
            state.nickname = null;
            state.avatar = null;
            state.token = null;
            state.isLogin = false;
            global.token = '';
            Taro.clearStorage();
        },
        clearSuccess(state) {
            state.nickname = null;
            state.avatar = null;
            state.token = null;
            state.isLogin = false;
            global.token = '';
            Taro.clearStorage();
        },
        getInfoSuccess(state, action: PayloadAction<any>) {
            const { nickname, avatar, token } = action.payload;
            state.nickname = nickname;
            state.avatar = avatar;
            state.token = token;
            state.isLogin = true;
            global.token = token;
            Taro.setStorage({ key: 'userData', data: JSON.stringify(action.payload) });
        },
        updateSuccess(state, action: PayloadAction<any>) {
            const { nickname, avatar, token } = action.payload;
            state.nickname = nickname;
            state.avatar = avatar;
            state.token = token;
            state.isLogin = true;
            Taro.setStorage({ key: 'userData', data: JSON.stringify(action.payload) });
        }
    },
});

export const { loginSuccess, registerSuccess, logoutSuccess, getInfoSuccess, updateSuccess, clearSuccess } = userSlice.actions;
export default userSlice.reducer;


// export const login = (username: string, password: string) => (dispatch: any) => {
//     request({
//         url: API_LOGIN, method: 'POST', data: { username, password }
//     }).then(res => {
//         dispatch(loginSuccess(res));
//     })
// }

// export const wxLogin = createAsyncThunk('user/wxLogin', async (payload: { code: string, encryptedData: string,iv:string }, { dispatch }) => {
//     console.log(payload.code)
//     request({
//         url: API_LOGIN, method: 'POST',
//         data: {
//             code: payload.code,
//             type: 'WX_MP',
//             app_version: '1',
//             client_version: '1',
//             client_type: 'WX_MP',
//             extra: {
//                 encryptedData: payload.encryptedData,
//                 iv: payload.iv
//             }
//         }
//     }).then(res => {
//         dispatch(loginSuccess(res));
//     })
// });

// /*
// export const wxLogin = (code: string) => (dispatch: any) => {
    
//     request({
//         url: API_LOGIN, method: 'POST', data: { code,type:'WX_MP',app_version:'1',client_version:'1',client_type:'WXP_MP' }
//     }).then(res => {
//         console.log(res);
//         dispatch(loginSuccess(res));
//     })
// }*/

// export const register = (name: string,email:string, password: string) => (dispatch: any) => {
//     request({
//         url: API_REGISTER, method: 'POST', data: { client_type:'IOS',username:name,email:email, password:password }
//     }).then(res => {
//         dispatch(registerSuccess(res));
//     })
// }

// export const logout = () => (dispatch: any) => {
//     request({
//         url: API_LOGOUT, method: 'GET', data: {}
//     }).then(res => {
//         dispatch(logoutSuccess());
//     })
// }

// export const clear = () => (dispatch: any) => {
//     request({
//         url: API_CLEAR_USER, method: 'POST', data: {}
//     }).then(res => {
//         dispatch(clearSuccess());
//     })
// }

// export const getInfo = () => (dispatch: any) => {
//     request({
//         url: API_USER_INFO, method: 'GET', data: {}
//     }).then(res => {
//         dispatch(getInfoSuccess(res));
//     })
// }


// export const update = () => (dispatch: any) => {
//     request({
//         url: API_USER_INFO, method: 'POST', data: {}
//     }).then(res => {
//         dispatch(updateSuccess(res));
//     })
// }

