import { takeLatest, all, fork } from 'redux-saga/effects';
import * as types from '~/store/actionTypes';
import { config } from '~/config';
import { networkSaga } from 'react-native-offline';
import * as app from './app';
import * as auth from './auth';
import * as assistance from './assistance';
import * as network from './network';
import * as toaster from './toaster';
import * as imagePicker from './imagePicker';

export function* watchChangeAppRoot() {
	yield takeLatest(types.CHANGE_APP_ROOT, app.changeAppRoot);
}

export function* watchToast() {
	yield takeLatest(types.SHOW_TOAST, toaster.toast);
}

export function* watchToastError() {
	yield takeLatest(types.SHOW_TOAST_ERROR, toaster.toastError);
}

export function* watchWiFiSSID() {
	yield takeLatest(types.GET_WIFISSID, network.getWiFiSSID);
}

export function* watchNetwork() {
	yield fork(networkSaga, config.network);
}

export function* watchGetAssets() {
	yield takeLatest(types.GET_ASSETS, imagePicker.getAssets);
}

// API
export function* watchGetVerification() {
	yield takeLatest(types.REQUEST_VERIFICATION, auth.getVerification);
}

export function* watchVerifyCode() {
	yield takeLatest(types.VERIFY_CODE, auth.verifyCode);
}

export function* watchLogin() {
	yield takeLatest(types.REQUEST_LOGIN, auth.login);
}

export function* watchRegisterBeneficiary() {
	yield takeLatest(types.REGISTER_BENEFICIARY, auth.registerBeneficiary);
}

export function* watchValidateAssistance() {
	yield takeLatest(types.REQUEST_VALIDATE_ASSISTANCE, assistance.registerAssistance);
}

export function* watchVerifyBenefitRefID() {
	yield takeLatest(types.REQUEST_VERIFY_REFID, assistance.verifyRefID);
}

export default function* rootSaga() {
	yield all([
		watchChangeAppRoot(),
		watchToast(),
		watchToastError(),
		watchNetwork(),
		watchWiFiSSID(),
		watchGetAssets(),
		watchGetVerification(),
		watchVerifyCode(),
		watchLogin(),
		watchRegisterBeneficiary(),
		watchValidateAssistance(),
		watchVerifyBenefitRefID(),
	]);
}
