import {
  call, put, takeLatest, all,
} from 'redux-saga/effects';
import * as Api from '../../services/api';
import {
  INAPP_TEMPLATE_CREATE_REQUEST,
  INAPP_TEMPLATE_CREATE_SUCCESS,
  INAPP_TEMPLATE_CREATE_FAILURE,
  UPLOAD_INAPP_ASSET_REQUEST,
  UPLOAD_INAPP_ASSET_SUCCESS,
  UPLOAD_INAPP_ASSET_FAILURE,
  INAPP_EDIT_TEMPLATE_SUCCESS,
  INAPP_EDIT_TEMPLATE_FAILURE,
  INAPP_EDIT_TEMPLATE_REQUEST,
  GET_INAPP_TEMPLATE_DETAILS_SUCCESS,
  GET_INAPP_TEMPLATE_DETAILS_FAILURE,
  GET_INAPP_TEMPLATE_DETAILS_REQUEST,
  GET_BEE_POPUP_BUILDER_TOKEN_REQUEST,
  GET_BEE_POPUP_BUILDER_TOKEN_SUCCESS,
  GET_BEE_POPUP_BUILDER_TOKEN_FAILURE,
} from './constants';
import { INAPP } from '../CreativesContainer/constants';

export function* uploadInAppAsset(params) {
  try {
    const result = yield call(Api.uploadFile, params);
    yield put({
      type: UPLOAD_INAPP_ASSET_SUCCESS,
      data: result?.response?.asset,
      statusCode: result?.status?.code || '',
      templateType: params.templateType,
    });
  } catch (error) {
    yield put({ type: UPLOAD_INAPP_ASSET_FAILURE, error });
  }
}
export function* watchUploadInAppAsset() {
  yield takeLatest(
    UPLOAD_INAPP_ASSET_REQUEST,
    uploadInAppAsset,
  );
}

export function* inAppTemplateCreate({ payload, callback }) {
  let errorMsg;
  try {
    const result = yield call(Api.createChannelWiseTemplate, { channel: INAPP, template: payload });
    if (result?.status?.code && result.status.code >= 400) {
      errorMsg = result?.message || result.status.code;
      throw errorMsg;
    }
    yield put({
      type: INAPP_TEMPLATE_CREATE_SUCCESS,
      data: result.response,
      statusCode: result.status ? result.status.code : '',
      errorMsg,
    });
    if (callback) {
      callback(result.response);
    }
  } catch (error) {
    yield put({
      type: INAPP_TEMPLATE_CREATE_FAILURE,
      error,
      errorMsg,
    });
    if (callback) {
      callback(null, errorMsg);
    }
  }
}

export function* watchCreateTemplate() {
  yield takeLatest(
    INAPP_TEMPLATE_CREATE_REQUEST,
    inAppTemplateCreate,
  );
}

// FOR EDIT
export function* editTemplate({ payload, callback }) {
  let errorMsg;
  try {
    const result = yield call(Api.createChannelWiseTemplate, { channel: INAPP, template: payload });
    if (result.status.code >= 400) {
      errorMsg = result.message || result.status.code;
      throw errorMsg;
    }
    yield put({
      type: INAPP_EDIT_TEMPLATE_SUCCESS,
      data: result.response,
      statusCode: result?.status?.code || '',
      errorMsg,
    });
    if (callback) {
      callback(result.response);
    }
  } catch (error) {
    yield put({ type: INAPP_EDIT_TEMPLATE_FAILURE, error, errorMsg });
    if (callback) {
      callback(null, errorMsg);
    }
  }
}
export function* watchEditTemplate() {
  yield takeLatest(INAPP_EDIT_TEMPLATE_REQUEST, editTemplate);
}

export function* getTemplateDetails({ id, callback }) {
  try {
    const result = yield call(Api.getTemplateDetails, {
      id,
      channel: INAPP,
    });
    yield put({
      type: GET_INAPP_TEMPLATE_DETAILS_SUCCESS,
      data: result.response,
    });
    if (callback) {
      callback(false);
    }
  } catch (error) {
    yield put({ type: GET_INAPP_TEMPLATE_DETAILS_FAILURE, error });
  }
}

export function* watchGetTemplateDetails() {
  yield takeLatest(
    GET_INAPP_TEMPLATE_DETAILS_REQUEST,
    getTemplateDetails,
  );
}

export function* getBeePopupBuilderToken() {
  try {
    const result = yield call(Api.getBeePopupBuilderToken);
    if (result.success) {
      yield put({
        type: GET_BEE_POPUP_BUILDER_TOKEN_SUCCESS,
        data: result.response,
      });
    } else {
      yield put({ type: GET_BEE_POPUP_BUILDER_TOKEN_FAILURE, error: result.message });
    }
  } catch (error) {
    yield put({ type: GET_BEE_POPUP_BUILDER_TOKEN_FAILURE, error });
  }
}

export function* watchGetBeePopupBuilderToken() {
  yield takeLatest(
    GET_BEE_POPUP_BUILDER_TOKEN_REQUEST,
    getBeePopupBuilderToken,
  );
}

export default [
  watchCreateTemplate,
  watchGetTemplateDetails,
  watchUploadInAppAsset,
  watchEditTemplate,
  watchGetBeePopupBuilderToken,
];

export function* v2InAppSagas() {
  yield all([
    watchCreateTemplate(),
    watchGetTemplateDetails(),
    watchUploadInAppAsset(),
    watchEditTemplate(),
    watchGetBeePopupBuilderToken(),
  ]);
}
