import { put, call} from 'redux-saga/effects';
import ACTION from '../actions/actiontsTypes';
import {
  getGoodsInGroup,
  getGoodsById,
  appendFavouriteGoods,
  getFavouriteGoods,
    deleteFavouriteGoods,
    issueOrder,
    dynamicSearchGoods,
    getFilterElements
} from '../api/rest/restContoller';

export function* getGoodsByGroup({ id, query, isRandom = true, isFetching = true}) {
  yield put({ type: ACTION.GOODS_BY_GROUP_REQUEST, isFetching: isFetching});
  try{

    const {data} = yield call(getGoodsInGroup, id, query);
    yield put({type: ACTION.GOODS_BY_GROUP_RESPONSE, data: data, isRandom: isRandom});
  } catch (e) {
    yield put({ type: ACTION.GOODS_BY_GROUP_ERROR, error: e })
  }
}

export function* getElementsForFilter({ groupId }) {
    yield put({ type: ACTION.FILTER_ELEMENTS_REQUEST});
    try{

        const {data} = yield call(getFilterElements, groupId);
        yield put({type: ACTION.FILTER_ELEMENTS_RESPONSE, data: data});
    } catch (e) {
        yield put({ type: ACTION.FILTER_ELEMENTS_ERROR, error: e })
    }
}

export function* getSimpleGoodById({ id }) {
  yield put({type: ACTION.SIMPLE_GOODS_BY_ID_REQUEST});
  try {
    const {data} = yield call(getGoodsById, id);
    yield put({type: ACTION.SIMPLE_GOODS_BY_ID_RESPONSE, data: data});
  } catch (e) {
    yield put({type: ACTION.SIMPLE_GOODS_BY_ID_ERROR, error: e})
  }
}

export function* appendFavouriteGood({ goodsId }) {
  yield put({type: ACTION.FAVOURITE_GOODS_REQUEST});
  try {
    const {data} = yield call(appendFavouriteGoods, goodsId, window.localStorage.getItem('token'));
    yield put({
      type: ACTION.FAVOURITE_GOODS_RESPONSE,
      data: data
    });
  }catch (e) {
    yield put({type: ACTION.FAVOURITE_GOODS_ERROR, error: e});
  }
}

export function* getFavouritesGoods() {
  yield put({type: ACTION.FAVOURITE_GOODS_REQUEST});
  try {
    const {data} = yield call(getFavouriteGoods, window.localStorage.getItem('token'));
    yield put({
      type: ACTION.FAVOURITE_GOODS_RESPONSE,
      data: data
    })
  }catch (e) {
    yield put({type: ACTION.FAVOURITE_GOODS_ERROR, error: e});
  }
}

export function* deleteFavouriteGood({ goodsId }) {
  yield put({type: ACTION.FAVOURITE_GOODS_REQUEST});
  try {
    const {data} = yield call(deleteFavouriteGoods, goodsId, window.localStorage.getItem('token'));
    yield put({
      type: ACTION.FAVOURITE_GOODS_RESPONSE,
      data: data
    });
  }catch (e) {
    yield put({type: ACTION.FAVOURITE_GOODS_ERROR, error: e});
  }
}

export function* updateBasketGoodsData({ basketData }) {
    yield put({
      type: ACTION.GOODS_BASKET_UPDATE_DATA,
      data: basketData
    });
    window.localStorage.setItem('goodsData', JSON.stringify(basketData));
}

export function* toIssueOrder({ orderData }) {
    yield put({type: ACTION.ISSUE_ORDER_REQUEST});
    try {
        const {data} = yield call(issueOrder, orderData, window.localStorage.getItem("token"));
        yield put({
            type: ACTION.ISSUE_ORDER_RESPONSE,
            data: data
        })
    }catch (e) {
        yield put({type: ACTION.ISSUE_ORDER_ERROR, error: e});
    }
}

export function* dynamicSearchGoodsSaga({ searchString }) {
    yield put({type: ACTION.DYNAMIC_SEARCH_GOODS_REQUEST});
    try {
        const {data} = yield call(
            dynamicSearchGoods,
            searchString,
            window.localStorage.getItem("token")
        );
        yield put({
            type: ACTION.DYNAMIC_SEARCH_GOODS_RESPONSE,
            data: data
        })
    }catch (e) {
        yield put({type: ACTION.DYNAMIC_SEARCH_GOODS_ERROR, error: e});
    }
}