import { StateInterface } from './index';
import { ActionTree, MutationTree, GetterTree, Module } from 'vuex';
import { apiServer } from 'boot/axios';
import { jwtDecode } from 'jwt-decode';
import { LangType } from 'src/i18n';
export interface ExampleStateInterface {
  userInfo: object | null | string;
  loading: boolean;
  lang: LangType | string;
}

const userState = {
  userInfo: localStorage.getItem('accesstoken')
    ? jwtDecode(`${localStorage.getItem('accesstoken')}`)
    : null,
  loading: false,
  lang: localStorage.getItem('lang') || 'vn-VN',
};

const actions: ActionTree<ExampleStateInterface, StateInterface> = {
  async setUser({ commit }, params) {
    try {
      const res = await apiServer.post('/auth/login-google', params);
      commit('SET_USER', jwtDecode(res.data.data.access_token));
      localStorage.setItem('accesstoken', res.data.data.access_token);
      localStorage.setItem('refresh', res.data.data.refresh_token);
      localStorage.setItem('try', '0');
      return res.data.data;
    } catch (e) {
      console.log(e);
    }
  },
  logout({ commit }, params: any) {
    commit('SET_USER', null);
  },
  async login({ commit }, params) {
    const res = await apiServer.post('/auth/login', params);
    commit('SET_USER', jwtDecode(res.data.data.access_token));
    localStorage.setItem('accesstoken', res.data.data.access_token);
    localStorage.setItem('refresh', res.data.data.refresh_token);
    localStorage.setItem('try', '0');
    return res.data.data;
  },
  async refreshToken({ commit }, params) {
    try {
      const rs = await apiServer.post('auth/refresh-token', {
        refresh_token: localStorage.getItem('refresh'),
      });
      localStorage.setItem('accesstoken', rs.data.data.access_token);
      localStorage.setItem('refresh', rs.data.data.refresh_token);
      return rs.data.data;
    } catch (e) {
      console.log(e);

      localStorage.removeItem('accesstoken');
      localStorage.removeItem('refresh');
      commit('SET_USER', null);
      console.log(e);
    }
  },
  async signUp({ commit }, params) {
    const data = { ...params };
    // if (!/\S+@\S+\.\S+/.test(params.username)) {
    //   data['email'] = params.username
    // }
    try {
      const res = await apiServer.post('/auth/sign-up', data);
      return res.data;
    } catch (e) {
      console.log(e);
      throw e;
    }
  },
  addLoading({ commit }, params) {
    commit('SET_LOADING', params.loading);
  },
  async changePassword({ commit }, params) {
    try {
      const res = await apiServer.post('/auth/change-password', params);
      return res.data;
    } catch (e) {
      console.log(e);
      throw e;
    }
  },
};

const getters: GetterTree<ExampleStateInterface, StateInterface> = {
  getUser: (state) => {
    console.log(state.userInfo);
    return state.userInfo;
  },
  getLoading: (state) => state.loading,
  getLang: (state) => state.lang,
};

const mutations: MutationTree<ExampleStateInterface> = {
  SET_USER(state, userInfo) {
    state.userInfo = userInfo;
  },
  SET_LOADING(state, loading) {
    state.loading = loading;
  },
  SET_LANG(state, lang: LangType) {
    localStorage.setItem('lang', lang);
    state.lang = lang;
  },
};

const userModule: Module<ExampleStateInterface, StateInterface> = {
  namespaced: true,
  actions,
  getters,
  mutations,
  state: userState,
};

export default userModule;
