import { login, logout, getInfo } from '@/api/user';
import { getToken, setToken, removeToken } from '@/utils/auth';
import router, { resetRouter } from '@/router';

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: []
  };
};

const state = getDefaultState();

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  }
};

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { phone, code } = userInfo;
    // return new Promise((resolve, reject) => {
    //   const data = {
    //     token: 'admin-token'
    //   };
    //   commit('SET_TOKEN', data.token);
    //   setToken(data.token);
    //   resolve();
    // });
    return new Promise((resolve, reject) => {
      login({
        account: phone,
        signInType: 'MOBILE_NUMBER_SMS',
        smsCode: code
      })
        .then(response => {
          const { data } = response;

          if (data.token) {
            commit('SET_TOKEN', data.token);
            setToken(data.token);
            resolve();
          } else {
            reject(data.message);
          }
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // get user info
  getInfo({ commit, state, dispatch }) {
    // return new Promise((resolve, reject) => {
    //   const data = {
    //     roles: ['admin'],
    //     introduction: 'I am a super administrator',
    //     avatar: 'https://picsum.photos/id/237/200/300',
    //     name: 'Super Admin'
    //   };
    //   const { name, avatar, roles } = data;

    //   // const roles = [];

    //   commit('SET_ROLES', roles);
    //   commit('SET_NAME', name);
    //   commit('SET_AVATAR', avatar);

    //   // dispatch('permission/generateRoutes');
    //   resolve(data);
    // });
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then(async response => {
          console.log(response);
          const { data, success } = response;

          if (!success) {
            dispatch('logout');
            return reject('Verification failed, please Login again.');
          }

          const { nickname, avatar, roles } = data;

          // const roles = [];

          commit('SET_ROLES', roles);
          commit('SET_NAME', nickname);
          commit('SET_AVATAR', avatar);
          // dispatch('permission/generateRoutes');
          resolve(data);
        })
        .catch(error => {
          dispatch('logout');
          reject(error);
        });
    });
  },

  // user logout
  logout({ commit, state }) {
    // return new Promise((resolve, reject) => {
    // logout(state.token)
    // .then(() => {
    removeToken(); // must remove  token  first
    resetRouter();
    commit('RESET_STATE');
    commit('SET_ROLES', []);
    // resolve();
    //     })
    //     .catch(error => {
    //       reject(error);
    //     });
    // });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken(); // must remove  token  first
      commit('RESET_STATE');
      commit('SET_ROLES', []);
      resolve();
    });
  }
};

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