import {createSlice, PayloadAction } from '@reduxjs/toolkit';
import Taro from '@tarojs/taro';
interface UserState {
    id:string|null;
    nickname: string | null;
    avatar: string | null;
    token: string | null;
    scenario_select_count: number |  0;
    isLogin: boolean;
    test_user:boolean;
    is_new_user:boolean;
}


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

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

            var obj = JSON.parse(JSON.stringify(action.payload))
            if (token){
                state.token = token;
                global.token = token;
            }
            else {
                obj.token = state.token;
            }
            Taro.setStorage({ key: 'userData', data: JSON.stringify(obj) });
        },
        updateSuccess(state, action: PayloadAction<any>) {
            const { nickname, avatar } = action.payload;
            if (nickname)
                state.nickname = nickname;
            if (avatar)
                state.avatar = avatar;
            state.isLogin = true;
            Taro.setStorage({ key: 'userData', data: JSON.stringify(state) });
        }
    },
});

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

