import service from '@/utils/apiService'
import qs from 'qs'
import { encryptRSA } from '@/utils/crypto'

const baseUrl = '/api/users'

export function getPublicKey() {
  return service({
    url: `/api/identity-server/clients/public-key`,
    method: 'post',
    data: {
      version: 1,
      clientId: process.env.VUE_APP_AUTH_CLIENT_ID
    }
  })
}

export function login(username, password, publicKey) {
  return service({
    url: '/connect/token',
    method: 'post',
    headers: {
      'Content-type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify({
      client_id: process.env.VUE_APP_AUTH_CLIENT_ID,
      client_secret: process.env.VUE_APP_AUTH_CLIENT_SECRET,
      // grant_type: 'password',
      grant_type: 'rsa_password',
      version: 1,
      username: username,
      password: encryptRSA(password, publicKey)
    })
  })
}

export function logout() {
  return new Promise(resolve => {
    resolve()
  })
}

export function getMe() {
  return service({
    url: `${baseUrl}/me`,
    method: 'get'
  })
}

export function getUser(id) {
  return service({
    url: `${baseUrl}/${id}`,
    method: 'get'
  })
}

export function getUserLatestEvents(id) {
  // if (token) {
  //   return service({
  //     url: `${baseUrl}/${id}/latest-events`,
  //     method: 'get'
  //   })
  // } else {
  //   return new Promise(resolve => {
  //     resolve({
  //       todoList: [],
  //       notifications: []
  //     })
  //   })
  // }
  return new Promise(resolve => {
    resolve({
      todoList: [],
      notifications: []
    })
  })
}

export function getUsers(params) {
  return service({
    url: `${baseUrl}`,
    method: 'get',
    params
  })
}

export function createUser(data) {
  return service({
    url: `${baseUrl}`,
    method: 'post',
    data
  })
}

export function updateUser(id, data) {
  return service({
    url: `${baseUrl}/${id}`,
    method: 'put',
    data
  })
}

export function deleteUser(id) {
  return service({
    url: `${baseUrl}/${id}`,
    method: 'delete'
  })
}

export function changePwd(id, data) {
  return service({
    url: `${baseUrl}/${id}/change-password`,
    method: 'post',
    data
  })
}

export function resetPwd(id) {
  return service({
    url: `${baseUrl}/${id}/reset-password`,
    method: 'post'
  })
}

export function changeOrganizations(id, data) {
  return service({
    url: `${baseUrl}/${id}/change-organizations`,
    method: 'post',
    data
  })
}

export function getGrantPermissions(id) {
  return service({
    url: `${baseUrl}/${id}/grant-permissions`,
    method: 'get'
  })
}

export function lookupAll() {
  return service({
    url: `${baseUrl}/lookup/all`,
    method: 'get'
  })
}

export function lookupByRoleName(roleName) {
  return service({
    url: `${baseUrl}/lookup/by-role-name/${roleName}`,
    method: 'get'
  })
}

export function lookupBySection(sectionId, unitCategory) {
  return service({
    url: `${baseUrl}/lookup/by-section/${sectionId}`,
    method: 'get',
    params: { unitCategory }
  })
}
