import { PlussCore } from '../feature.config';
import { values } from '../values.config';
const { Helper, Session } = PlussCore;

export const maintenanceActions = {
  getJobType: (site, typeId) => {
    let url = Helper.getUrl(values.serviceKey, 'getjobtype');
    return Session.authedFunction({
      method: 'POST',
      url,
      data: { site, typeId },
    });
  },
  getJobTypes: (site, id) => {
    let url = Helper.getUrl(values.serviceKey, 'getjobtypes');
    return Session.authedFunction({
      method: 'POST',
      url,
      data: { site },
    });
  },
  getJob: (site, id) => {
    let url = Helper.getUrl(values.serviceKey, 'getJob');
    return Session.authedFunction({
      method: 'POST',
      url,
      data: { site, id },
    });
  },
  getJobByJobId: (site, jobId) => {
    let url = Helper.getUrl(values.serviceKey, 'getJob');
    return Session.authedFunction({
      method: 'POST',
      url,
      data: { site, jobId },
    });
  },
  getJobs: (site, status = '', type = '') => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'getJobs'),
      data: { site, status, type },
    });
  },
  getJobs2: (site, status, type, lastKey) => {
    const query = { site };
    if (status) {
      query.status = status;
    }
    if (type) {
      query.type = type;
    }
    if (lastKey) {
      query.lastKey = JSON.stringify(lastKey);
    }
    return Session.authedFunction({
      method: 'GET',
      url: Helper.getUrl(values.serviceKey, 'get/requests', query),
    });
  },
  getJobsRecursive: (site, status, type, lastKey, jobs = []) => {
    return new Promise((resolve) => {
      maintenanceActions.getJobs2(site, status, type, lastKey).then((jobRes) => {
        const newJobs = [...jobs, ...jobRes.data.Items];
        if (!jobRes.data.LastKey) {
          return resolve(newJobs);
        }
        return resolve(maintenanceActions.getJobsRecursive(site, status, type, jobRes.data.LastKey, newJobs));
      });
    });
  },
  createJob: (job) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'sendMaintenance'),
      data: {
        ...job,
      },
    });
  },
  editJob: (job, site) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'editJob'),
      data: { job, site },
    });
  },
  deleteJob: (site, id) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'requests/remove'),
      data: { site, id },
    });
  },
  editJobStatus: (id, status) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'editJobStatus'),
      data: { id, status },
    });
  },
  editJobPriority: (id, priority) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'update/priority'),
      data: { id, priority },
    });
  },
  assignJob: (jobId, userId) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'update/assign'),
      data: {
        id: jobId,
        userId,
      },
    });
  },
  getAssignees: (site) => {
    return Session.authedFunction({
      method: 'GET',
      url: Helper.getUrl(values.serviceKey, 'get/assignees', { site }),
    });
  },
  addNote: (jobId, note, attachments, images) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'requests/note'),
      data: {
        id: jobId,
        note,
        attachments,
        images,
        action: 'AddNote',
      },
    });
  },
  editNote: (jobId, noteId, note, attachments, images) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'requests/note'),
      data: {
        id: jobId,
        note,
        attachments,
        images,
        noteId,
        action: 'EditNote',
      },
    });
  },
  deleteNote: (jobId, noteId) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'requests/note'),
      data: {
        id: jobId,
        noteId,
        action: 'DeleteNote',
      },
    });
  },
  addJobType: (site, name, email, description, level, hasCustomFields, customFields) => {
    const data = { site, name, email, description, level, hasCustomFields, customFields: customFields || [] };
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'createJobType'),
      data,
    });
  },
  editJobType: (site, id, name, email, description, level, hasCustomFields, customFields) => {
    const data = { site, id, name, email, description, level, hasCustomFields, customFields: customFields || [] };
    if (hasCustomFields && customFields) data.customFields = customFields;
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'editJobType'),
      data,
    });
  },
  deleteJobType: (site, id) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'deleteJobType'),
      data: {
        site,
        id,
      },
    });
  },
  getExternalSync: (jobId) => {
    return Session.authedFunction({
      method: 'GET',
      url: Helper.getUrl(values.serviceKey, 'get/externalsync', { id: jobId }),
    });
  },
  retrySync: (jobId) => {
    return Session.authedFunction({
      method: 'POST',
      url: Helper.getUrl(values.serviceKey, 'update/retrysync'),
      data: { id: jobId },
    });
  },
};
