import { reactive } from 'vue'
import axios from 'axios'

const userInfo = reactive({
  isLogin: false,
  userId: '',
})

export const userApi = {
  loginProcess: async function (inputUsername, inputUserPassword) {
    const data = { username: inputUsername, password: inputUserPassword }
    const response = await axios.post('/public-api/loginProcess', data, { withCredentials: true })
    if (response.status == 200 && response.data && response.data.success == true) {
      userInfo.isLogin = true
      userInfo.userId = response.data.username
      return true
    } else {
      return false
    }
  },
  logoutProcess: async function () {
    const response = await axios.post('/public-api/logoutProcess')
    if (response.status == 200 && response.data && response.data.success == true) {
      userInfo.isLogin = false
      userInfo.userId = ''
      return true
    } else {
      return false
    }
  },
  loginCheck: async function () {
    const response = await axios.get('/public-api/sessionCheck', { withCredentials: true })
    if (response.status == 200 && response.data && response.data.success == true) {
      userInfo.isLogin = true
      userInfo.userId = response.data.username
      return true
    } else {
      userInfo.isLogin = false
      userInfo.userId = ''
      return false
    }
  },
  getEncryptText: async function (plainText) {
    const response = await axios.post('/public-api/passwordGenerator', { password: plainText })
    return response.data.password
  },
}
window.userApi = userApi
window.userInfo = userInfo
export default userInfo
