import { all, call, put, takeEvery } from "redux-saga/effects";
import { JWT_TOKEN } from "../../constants/localStorage";
import { addHeaderToken } from "../../request";
import {
  createComment,
  getCandidate,
  getAllAdsCandidates,
  deleteCandidate,
  getFilteredCandidates
} from "../../request/candidatesRequest";
import { authScopeStringGetHelper } from "../../util/helpers/authScopeHelpers";
import {
  ADS_CANDIDATES_FETCH,
  FILTER_CANDIDATES
} from "../actions/candidates/candidatesActionConstants";
import {
  fetchAdsCandidatesError,
  fetchAdsCandidatesSuccess,
  filterCandidatesSuccess,
  filterCandidatesError,
} from "../actions/candidates/candidatesActions";
import { rejectErrorCodeHelper } from "../../util/helpers/rejectErrorCodeHelper";
import {
  fetchCandidateError,
  fetchCandidateSuccess,
  createCandidateCommentSuccess,
  createCandidateCommentError,
  deleteCandidateSuccess,
  deleteCandidateError,
} from "../actions/candidate/candidateActions";
import {
  CANDIDATE_FETCH,
  CANDIDATE_COMMENTS_FETCH,
  DELETE_CANDIDATE,
} from "../actions/candidate/candidateActionConstants";
import { CANDIDATES_PAGE } from "../../constants/pages";
import history from "../utils/history";


export function* filterCandidates({ payload }) {
  try {
    const JwtToken = yield call(authScopeStringGetHelper, JWT_TOKEN);
    yield call(addHeaderToken, JwtToken);
    const {data} = yield call(getFilteredCandidates,payload);
    yield put(filterCandidatesSuccess(data));
    if(payload.handleApiResponseSuccess){
      yield call(payload.handleApiResponseSuccess)
    }
  } catch (error) {
    const errorMessage = yield call(rejectErrorCodeHelper, error);
    yield put(filterCandidatesError(errorMessage));
  }
}

export function* getSingleCandidate({ payload }) {
  try {
    const JwtToken = yield call(authScopeStringGetHelper, JWT_TOKEN);
    yield call(addHeaderToken, JwtToken);
    const { data } = yield call(getCandidate, payload.id);
    yield put(fetchCandidateSuccess(data));
  } catch (error) {
    const errorMessage = yield call(rejectErrorCodeHelper, error);
    yield put(fetchCandidateError(errorMessage));
  }
}

export function* addComment(data) {
  const { user, ...myObj } = data.payload;
  try {
    const JwtToken = yield call(authScopeStringGetHelper, JWT_TOKEN);
    yield call(addHeaderToken, JwtToken);
    yield call(createComment, myObj);
    yield put(createCandidateCommentSuccess({ user, myObj }));
  } catch (error) {
    const errorMessage = yield call(rejectErrorCodeHelper, error);
    yield put(createCandidateCommentError(errorMessage));
  }
}

export function* getAdsCandidates() {
  try {
    const JwtToken = yield call(authScopeStringGetHelper, JWT_TOKEN);
    yield call(addHeaderToken, JwtToken);
    const { data } = yield call(getAllAdsCandidates);
    yield put(fetchAdsCandidatesSuccess(data));
  } catch (error) {
    const errorMessage = yield call(rejectErrorCodeHelper, error);
    yield put(fetchAdsCandidatesError(errorMessage));
  }
}

export function* deleteSingleCandidate({ payload }) {
  try {
    yield call(deleteCandidate, payload.id);
    yield call(history.replace, CANDIDATES_PAGE);
    yield put(deleteCandidateSuccess());
  } catch (error) {
    const errorMessage = yield call(rejectErrorCodeHelper, error);
    yield put(deleteCandidateError(errorMessage));
  }
}

export default function* candidatesSaga() {
  yield all([takeEvery(CANDIDATE_FETCH, getSingleCandidate)]);
  yield all([takeEvery(ADS_CANDIDATES_FETCH, getAdsCandidates)]);
  yield all([takeEvery(CANDIDATE_COMMENTS_FETCH, addComment)]);
  yield all([takeEvery(DELETE_CANDIDATE, deleteSingleCandidate)]);
  yield all([takeEvery(FILTER_CANDIDATES, filterCandidates)]);
}
