import { call, put } from "redux-saga/effects";

import setAuthorizationHeader from "../data/setAuthorizationHeader";
import api from "../data/api";
import Router from 'next/router';
import _ from "lodash";

import {
  postAdAnswerRequestSuccess,
  postAdAnswerRequestFailure,
  fetchJobsAnsweredSuccess,
  fetchJobsAnsweredFailure,
  fetchAnswerSuccess,
  fetchAnswerFailure,
  postMessageAnswerSuccess,
  postMessageAnswerFailure,
  fetchAnswerMessagesRequest,
  fetchAnswerMessagesFailure,
  fetchAnswerMessagesSuccess,


} from "../actions/answer";

import {
  uploadMediaSuccess,
  uploadMediaFailure,
  uploadMediaPresSuccess,
  uploadMediaPresFailure,

} from "../actions/media";

import {
  uploadMediaCvSuccess,
  uploadMediaCvFailure
} from "../actions/mediaCv";
import AuthRouter from "../pages/AuthRouter";

export function* postAdAnswerRequestSaga(action) {
  try {

    const res = yield call(api.answers.postAnswer, action.payload);

    if (!!res.data) {
      yield put(postAdAnswerRequestSuccess(res));
      Router.push("/CandidatureValidation");
    }
    else
      yield put(postAdAnswerRequestFailure(res));

  } catch (err) {
    if (_.includes(err, "401")) {
      console.error("401")
      console.error(window.location.pathname)
      console.log("ERREUR 401")
      localStorage.setItem('loginRedirect', window.location.pathname)
      console.log("ERREUR 401")
      Router.push("/Login");
    }
    yield put(postAdAnswerRequestFailure(err.response));
  }
}

export function* uploadMediaSaga(action) {
  try {

    const res = yield call(api.medias.uploadMedia, action.payload);

    if (!!res.data)
      yield put(uploadMediaSuccess(res));
    else
      yield put(uploadMediaFailure(res));

  } catch (err) {
    if (_.includes(err, "401") || err.response.status === 401) {
    
      localStorage.setItem('loginRedirect', window.location.pathname)
      console.log("ERREUR 401")
      Router.push("/Login");
    }
    yield put(uploadMediaFailure(err.response));
  }
}

export function* uploadMediaPresSaga(action) {
  try {

    const res = yield call(api.medias.uploadMediaPres, action.payload);

    if (!!res.data)
      yield put(uploadMediaPresSuccess(res));
    else
      yield put(uploadMediaPresFailure(res));

  } catch (err) {
    if (_.includes(err, "401") || err.response.status === 401) {
    
      localStorage.setItem('loginRedirect', window.location.pathname)
      console.log("ERREUR 401")
      Router.push("/Login");
    }
    yield put(uploadMediaPresFailure(err.response));
  }
}

export function* uploadMediaCvSaga(action) {
  try {

    const res = yield call(api.cv.uploadMediaCv, action.payload);

    if (!!res.data)
      yield put(uploadMediaCvSuccess(res));
    else
      yield put(uploadMediaCvFailure(res));

  } catch (err) {
    if (_.includes(err, "401") || err.response.status === 401) {
    
      localStorage.setItem('loginRedirect', window.location.pathname)
      console.log("ERREUR 401")
      Router.push("/Login");
    }
    yield put(uploadMediaCvFailure(err.response));
  }
}

export function* fetchJobsAnsweredRequestSaga(action) {
  try {
    const res = yield call(api.answers.fetchJobsAnswered, action.payload);

    if (!!res.data)
      yield put(fetchJobsAnsweredSuccess(res));
    else
      yield put(fetchJobsAnsweredFailure(res));

  } catch (err) {
    if (_.includes(err, "401") || err.response.status === 401) {
      localStorage.setItem('loginRedirect', "/Candidatures")
      console.log("ERREUR 401")
      Router.push("/Login");

    }
    yield put(fetchJobsAnsweredFailure(err.response));
  }
}
// {
//   type: 'FETCH_ANSWER_REQUEST',
//   payload : 325
//   }
export function* fetchAnswerRequestSaga(action) {
  try {
    const res = yield call(api.answers.fetchAnswer, action.payload);

    if (!!res)
      yield put(fetchAnswerSuccess(res));
    else
      yield put(fetchAnswerFailure(res));

  } catch (err) {

    if (_.includes(err, "401") || err.response.status === 401) {
      localStorage.setItem('loginRedirect', "/Candidatures")
      console.log("ERREUR 401")
      Router.push("/Login");
    }
    yield put(fetchAnswerFailure(err.response));
  }
}

// 
export function* postAnswerMessageSaga(action) {
  try {

    const res = yield call(api.answers.postMessage, action.payload);

    if (!!res.data) {
      yield put(postMessageAnswerSuccess(res));
      yield put(fetchAnswerMessagesRequest(action.payload.answerId))

    }
    else
      yield put(postMessageAnswerFailure(res));

  } catch (err) {
    if (_.includes(err, "401")) {
      localStorage.setItem('loginRedirect', window.location.pathname)
      console.log("ERREUR 401")
      Router.push("/Login");
    }
    yield put(postMessageAnswerFailure(err.response));
  }
}

export function* fetchAnswerMessagesRequestSaga(action) {
  try {

    const res = yield call(api.answers.fetchAnswerMessages, action.payload);

    if (!!res)
      yield put(fetchAnswerMessagesSuccess(res));
    else
      yield put(fetchAnswerMessagesFailure(res));

  } catch (err) {
    if (_.includes(err, "401")) {
      localStorage.setItem('loginRedirect', "/Candidatures")
      console.log("ERREUR 401")
      Router.push("/Login");
    }
    yield put(fetchAnswerMessagesFailure(err.response));
  }
}