import apiClient from "@/infrastructure/apiClient";
import apiPath from "@/infrastructure/apiPath";
import { getDeviceId } from "@/infrastructure/utilities/fingerprintService";
import { getDeviceInfo } from "@/infrastructure/utilities/deviceInfoService";

export default {
  async login(loginModel) {
    const deviceId = await getDeviceId();
    const deviceInfo = getDeviceInfo();

    const params = JSON.parse(
      JSON.stringify({
        ...loginModel,
        DeviceId: deviceId,
        DeviceInfo: JSON.stringify(deviceInfo),
      })
    );

    var loginResult = await apiClient
      .post(apiPath.user.login, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        throw e.message;
      });
    return loginResult;
  },

  async register(registerModel) {
    const params = JSON.parse(JSON.stringify(registerModel));
    var result = await apiClient
      .post(apiPath.user.register, params)
      .then((r) => r.data)
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async refreshToken(refreshToken) {
    const params = JSON.parse(JSON.stringify({ refreshToken: refreshToken }));
    var loginResult = await apiClient
      .post(apiPath.user.refresh, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return loginResult;
  },
  async listStaff(searchModel) {
    const params = JSON.parse(JSON.stringify(searchModel));

    var searchResult = await apiClient
      .post(apiPath.user.listStaff, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async createStaff(createOrUpdateModel) {
    const params = JSON.parse(JSON.stringify(createOrUpdateModel));

    var searchResult = await apiClient
      .post(apiPath.user.createStaff, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getStaff(userId) {
    var roleResult = await apiClient
      .get(apiPath.user.getStaffById + userId)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async updateStaff(staffModel) {
    const params = JSON.parse(JSON.stringify(staffModel));

    var roleResult = await apiClient
      .put(apiPath.user.updateStaff, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async lock(userId) {
    const params = JSON.parse(JSON.stringify({ Id: userId }));

    var roleResult = await apiClient
      .put(apiPath.user.lock, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        throw new Error(e);
      });
    return roleResult;
  },
  async unlock(userId) {
    const params = JSON.parse(JSON.stringify({ Id: userId }));

    var roleResult = await apiClient
      .put(apiPath.user.unlock, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async setPassword(setPasswordModel) {
    const params = JSON.parse(JSON.stringify(setPasswordModel));

    var roleResult = await apiClient
      .put(apiPath.user.setPassword, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async setRole(setRoleModel) {
    const params = JSON.parse(JSON.stringify(setRoleModel));

    var roleResult = await apiClient
      .put(apiPath.user.setRole, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async sugStaff(model) {
    var roleResult = await apiClient
      .get(apiPath.user.sugStaff + "?keyword=" + model)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async sugDriver(keyword) {
    var roleResult = await apiClient
      .get(apiPath.user.sugDriver + keyword)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async autoComplete(model) {
    var roleResult = await apiClient
      .get(apiPath.user.autoComplete + model)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async autoCompleteByagency(model) {
    var roleResult = await apiClient
      .get(apiPath.user.autoCompleteByAgency + model)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async changePassword(model) {
    const params = JSON.parse(JSON.stringify(model));
    var result = await apiClient
      .put(apiPath.user.changePassword, params)
      .then((r) => r.data)
      .catch((e) => {
        console.log(e);
        throw e;
      });
    return result;
  },
};
