import _ from 'lodash';
import { JOBS_LOADED, JOBS_REMOVED, JOBS_LOADING, JOBS_TYPES_LOADED, JOBS_STATUSES_LOADED, JOBS_HIDE_SEEN } from './types';
import { maintenanceActions, stringActions } from '../apis';
import DefaultStatusTypes from '../maintenanceStatus.json';
import { PlussCore } from '../feature.config';
import { values } from '../values.config';

const { Helper } = PlussCore;

export const jobsUpdate = (site, isdashboard) => {
  return (dispatch) => {
    if (isdashboard) dispatch({ type: JOBS_LOADING });

    maintenanceActions.getJobsRecursive(site).then((res) => {
      const currentSite = Helper.readStorageWithCookie('site');
      if (!_.isEmpty(res) && res[0].site === currentSite) {
        dispatch({
          type: JOBS_LOADED,
          payload: res,
        });
      } else {
        dispatch({
          type: JOBS_LOADED,
          payload: [],
        });
      }
    });
  };
};

export const jobsLoaded = (events) => {
  return {
    type: JOBS_LOADED,
    payload: events,
  };
};

export const removeJob = (id) => {
  return {
    type: JOBS_REMOVED,
    payload: id,
  };
};

export const jobTypesUpdate = (site) => {
  return (dispatch) => {
    maintenanceActions.getJobTypes(site).then((res) => {
      dispatch({
        type: JOBS_TYPES_LOADED,
        payload: res.data,
      });
    });
  };
};

export const jobTypesLoaded = (events) => {
  return {
    type: JOBS_TYPES_LOADED,
    payload: events,
  };
};

export const jobStatusesUpdate = (site, callback = null) => {
  return (dispatch) => {
    stringActions
      .getString(site, values.stringConfigJobStatus)
      .then((res) => {
        dispatch({
          type: JOBS_STATUSES_LOADED,
          payload: res.data,
        });
        if (callback) callback(res.data);
      })
      .catch((_error) => {
        dispatch({
          type: JOBS_STATUSES_LOADED,
          payload: DefaultStatusTypes,
        });
        if (callback) callback(DefaultStatusTypes);
      });
  };
};

export const jobStatusesLoaded = (statuses) => {
  return {
    type: JOBS_STATUSES_LOADED,
    payload: statuses,
  };
};

export const jobHideSeenUpdate = (site, callback = null) => {
  return (dispatch) => {
    stringActions
      .getString(site, values.stringConfigHideSeen)
      .then((res) => {
        dispatch({
          type: JOBS_HIDE_SEEN,
          payload: res.data,
        });
        if (callback) callback(res.data);
      })
      .catch((_error) => {
        dispatch({
          type: JOBS_HIDE_SEEN,
          payload: false,
        });
        if (callback) callback(false);
      });
  };
};

export const jobHideSeenLoaded = (hide) => {
  return {
    type: JOBS_HIDE_SEEN,
    payload: hide,
  };
};

// export const removeJobType = (id) => {
//   return {
//     type: JOBS_TYPES_REMOVED,
//     payload: id,
//   };
// };

// export const clearJobs = () => {
//   return {
//     type: JOBS_PURGE,
//   };
// };

// export const ticketsLoaded = (tickets) => {
//   return {
//     type: TICKETS_LOADED,
//     payload: tickets,
//   };
// };

// export const ticketsUpdate = (site) => {
//   return (dispatch) => {
//     maintenanceActions.getTickets(site).then((res) => {
//       if (res.data != null) {
//         dispatch({
//           type: TICKETS_LOADED,
//           payload: res.data,
//         });
//       }
//     });
//   };
// };
