import { call, put, takeLatest, takeEvery } from 'redux-saga/effects';
import * as types from '../actions/types';
import * as BookService from '../services/BookService';
import * as BookAction from '../actions/BookAction';
import * as GeneralAction from '../actions/GeneralAction';
import { Toast } from './../components';
import * as UserAction from '../actions/UserAction';
function* searchBook(action: any) {
  const { params } = action.payload || {};
  try {
    const res = yield call(BookService.searchBook, params);
    if (res.success) {
      yield put(BookAction.searchBookSuccess(res.data));
    } else {
      yield put(BookAction.searchBookFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.searchBookFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
    Toast.errorBottom('Token expired!');
  }
}

function* bookReserve(action: any) {
  const params = action.payload || {};
  const setCurrentTime = params.setCurrentTime;
  try {
    const res = yield call(BookService.reserveBook, params);
    if (res.success) {
      yield put(BookAction.bookReserveSuccess());
      yield put(BookAction.changeBookStatusRequest(params.id, 2));
      if (setCurrentTime) {
        setCurrentTime();
      }
      yield put(GeneralAction.goBackRequest());
      Toast.successBottom(res.message);
    } else {
      yield put(BookAction.bookReserveFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.bookReserveFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
    Toast.errorBottom('Token expired!');
  }
}

function* bookUnreserve(action: any) {
  const params = action.payload || {};

  const setCurrentTime = params.setCurrentTime;
  try {
    const res = yield call(BookService.unreserveBook, params);
    if (res.success) {
      yield put(BookAction.bookUnreserveSuccess());
      Toast.successBottom(res.message);
      if (params.from === 'historyList' || params.from === 'historyDetails') {
        yield put(BookAction.changeBookHistoryStatusRequest(params.id, 5));
        let filterParams = {
          search: '',
          search_by_book: 1
        };
        yield put(BookAction.bookHistoryRequest());
        yield put(BookAction.searchBookRequest(filterParams));
      } else {
        yield put(BookAction.changeBookStatusRequest(params.id, 1));
        yield put(GeneralAction.goBackRequest());
      }
      if (params.from === 'historyDetails') {
        yield put(GeneralAction.goBackRequest());
      }
      if (setCurrentTime) {
        setCurrentTime();
      }
      Toast.successBottom(res.message);
    } else {
      yield put(BookAction.bookUnreserveFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.bookUnreserveFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
     Toast.errorBottom('Token expired!');
  }
}

function* bookHistory(action: any) {
  const { params } = action.payload || {};
  try {
    const res = yield call(BookService.bookHistory, params);
    if (res.success) {
      yield put(BookAction.bookHistorySuccess(res.data));
    } else {
      yield put(BookAction.bookHistoryFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.bookHistoryFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
        Toast.errorBottom('Token expired!');
  }
}

function* bookHistoryDetails(action: any) {
  const { params } = action.payload || {};
  const { book_item_id } = action.payload || {};
  try {
    const res = yield call(BookService.bookHistoryDetails, params);
    if (res.success) {
      res.data.book_item_id = book_item_id;
      yield put(BookAction.bookHistoryDetailsSuccess(res.data));
    } else {
      yield put(BookAction.bookHistoryDetailsFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.bookHistoryDetailsFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
     Toast.errorBottom('Token expired!');
  }
}

function* bookRequestList(action: any) {
  const { params } = action.payload || {};
  try {
    const res = yield call(BookService.getBookRequestList, params);
    if (res.success) {
      yield put(BookAction.bookRequestListSuccess(res.data));
    } else {
      yield put(BookAction.bookRequestListFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.bookRequestListFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
    Toast.errorBottom('Token expired!');
  }
}

function* bookRequestDetails(action: any) {
  const params = action.payload || {};
  let { filterParams } = action.payload || {};
  try {
    const res = yield call(BookService.getBookRequestDetails, params);
    if (res.success) {
      if (params.shouldNavigate) {
        yield put(GeneralAction.navigateToBookRequestDetails(filterParams));
      }
      if (params.editModal) {
        yield put(GeneralAction.showEditModalRequest());
      }
      yield put(BookAction.bookRequestDetailsSuccess(res.data));
    } else {
      yield put(BookAction.bookRequestDetailsFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.bookRequestDetailsFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
    Toast.errorBottom('Token expired!');
  }
}

function* addBookRequest(action: any) {
  const { params } = action.payload || {};
  const { filterParams } = action.payload || {};
 try {
    const res = yield call(BookService.addBookRequest, params);
    if (res.success) {
      yield put(BookAction.addBookRequestSuccess());
      yield put(BookAction.bookRequestListRequest(filterParams));
      yield put(GeneralAction.goBackRequest());
      Toast.successBottom(res.message);
    } else {
      yield put(BookAction.addBookRequestFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.addBookRequestFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
     Toast.errorBottom('Something went wrong');
  }
}

function* editBookRequest(action: any) {
  const { params } = action.payload || {};
  const { filterParams } = action.payload || {};
  try {
    const res = yield call(BookService.putBookRequestDetails, params);
    if (res.success) {
      yield put(BookAction.editBookRequestSuccess());
      yield put(BookAction.bookRequestListRequest(filterParams));
      Toast.successBottom(res.message);
    } else {
      yield put(BookAction.editBookRequestFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.editBookRequestFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
    Toast.errorBottom('Token expired!');
  }
}

function* deleteBookRequest(action: any) {
  const { params } = action.payload || {};
  const { filterParams } = action.payload || {};
  try {
    const res = yield call(BookService.deleteBookRequest, params);
    if (res.success) {
      yield put(BookAction.deleteBookRequestSuccess());
      yield put(BookAction.bookRequestListRequest(filterParams));
      Toast.successBottom(res.message);
    } else {
      yield put(BookAction.deleteBookRequestFail(res.message));
      Toast.errorBottom(res.message);
    }
  } catch (error) {
    yield put(BookAction.deleteBookRequestFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
      Toast.errorBottom('Token expired!');
  }
}

export default function* watchAuthRequests() {
  yield takeLatest(types.SEARCH_BOOK_REQUEST, searchBook);
  yield takeEvery(types.BOOK_RESERVE_REQUEST, bookReserve);
  yield takeEvery(types.BOOK_UNRESERVE_REQUEST, bookUnreserve);
  yield takeLatest(types.BOOK_HISTORY_REQUEST, bookHistory);
  yield takeEvery(types.BOOK_HISTORY_DETAILS_REQUEST, bookHistoryDetails);
  yield takeLatest(types.BOOK_REQUEST_LIST_REQUEST, bookRequestList);
  yield takeEvery(types.BOOK_REQUEST_DETAILS_REQUEST, bookRequestDetails);
  yield takeEvery(types.ADD_BOOK_REQUEST_REQUEST, addBookRequest);
  yield takeEvery(types.EDIT_BOOK_REQUEST_REQUEST, editBookRequest);
  yield takeEvery(types.DELETE_BOOK_REQUEST_REQUEST, deleteBookRequest);
}
