// Line Sagas

import {
  call, put, takeLatest, all,
} from 'redux-saga/effects';
import * as Api from '../../../services/api';
import * as types from './constants';

export function* createTemplate({ template, callback }) {
  let errorMsg;
  try {
    const result = yield call(Api.createLineTemplate, template);
    if (result.status.code >= 400) {
      errorMsg = result.message;
      throw errorMsg;
    }
    yield put({ type: types.CREATE_TEMPLATE_SUCCESS, data: result.response, statusCode: result.status ? result.status.code : '', errorMsg });
    if (callback) {
      callback(result.response);
    }
  } catch (error) {
    yield put({ type: types.CREATE_TEMPLATE_FAILURE, error, errorMsg });
    if (callback) {
      callback(null, errorMsg);
    }
  }
}
export function* uploadAsset(file, assetType, fileParams ) {
  try {
    const result = yield call(Api.uploadFile, file, assetType, fileParams);
    yield put({ type: types.UPLOAD_ASSET_SUCCESS, data: result.response.asset, statusCode: result.status ? result.status.code : '', templateType: file.templateType });
  } catch (error) {
    yield put({ type: types.UPLOAD_ASSET_FAILURE, error });
  }
}

function* watchCreateTemplate() {
  
  yield takeLatest(types.CREATE_TEMPLATE_REQUEST, createTemplate);
  
  
}
function* watchUploadAsset() {
  
  yield takeLatest(types.UPLOAD_ASSET_REQUEST, uploadAsset);
  
  
}

// edit

export function* editTemplate({ template, callback }) {
  let errorMsg;
  try {
    const result = yield call(Api.createLineTemplate, template);
    if (result.status.code >= 400) {
      errorMsg = result.message;
      throw errorMsg;
    }
    if (callback) {
      callback(result.response);
    }
    yield put({ type: types.EDIT_TEMPLATE_SUCCESS, data: result.response, statusCode: result.status ? result.status.code : '', errorMsg });
  } catch (error) {
    yield put({ type: types.EDIT_TEMPLATE_FAILURE, error, errorMsg });
    if (callback) {
      callback(null, errorMsg);
    }
  }
}

export function* getTemplateDetails(id) {
  try {
    
    const result = yield call(Api.getTemplateDetails, id);
    // const sidebar = result.response.sidebar;
    yield put({ type: types.GET_TEMPLATE_DETAILS_SUCCESS, data: result.response });
    if (id.callback) {
      id.callback(false);
    }
  } catch (error) {
    yield put({ type: types.GET_TEMPLATE_DETAILS_FAILURE, error });
  }
}

export function* getLineTemplatesList(params) {
  try {
    
    const result = yield call(Api.getAllTemplates, {channel: params.channel, queryParams: params.queryParams});
    yield put({ type: types.GET_LINE_TEMPLATES_LIST_SUCCESS, data: result.response.templates, templateData: result.response.mapped});
  } catch (error) {
    yield put({ type: types.GET_LINE_TEMPLATES_LIST_FAILURE, error });
  }
}

export function* getLineStickers() {
  try {
    const result = yield call(Api.getAllLineStickers);
    yield put({
      type: types.GET_STICKER_RESPONSE,
      result,
    });
  } catch (error) {
    yield put({
      type: types.GET_STICKER_RESPONSE,
      error,
    });
  }
}

function* watchEditTemplate() {
  yield takeLatest(types.EDIT_TEMPLATE_REQUEST, editTemplate);
  
  
}

function* watchGetTemplateDetails() {
  yield takeLatest(types.GET_TEMPLATE_DETAILS_REQUEST, getTemplateDetails);
  
  
}

function* watchLineTemplatesList() {
  yield takeLatest(types.GET_LINE_TEMPLATES_LIST_REQUEST, getLineTemplatesList);
  
  
}

function* watchGetLineStickers() {
  yield takeLatest(types.GET_STICKER_REQUEST, getLineStickers);
  
  
}

// All sagas to be loaded
export default [
  watchCreateTemplate,
  watchUploadAsset,
  watchEditTemplate,
  watchGetTemplateDetails,
  watchLineTemplatesList,
  watchGetLineStickers,
];

export function* v2LineContainerSagas() {
  yield all([
    watchCreateTemplate(),
    watchUploadAsset(),
    watchEditTemplate(),
    watchGetTemplateDetails(),
    watchLineTemplatesList(),
    watchGetLineStickers(),
  ]);
}