import { put, call} from 'redux-saga/effects';
import ACTION from '../actions/actiontsTypes';
import {
    sendSignUpFormData,
    sendAuthFormData,
    checkAuth,
    getPersonalUserData,
    updatePersonalData,
    getHistoryOrders,
    deleteOrder
} from "../api/rest/restContoller";
import { toast } from 'react-toastify';
import {errorsUpdatePersonalUserData} from "../constants/errors/updatePersonalUserData";



export function* signUpSaga({ signUpData }) {
    yield put({type: ACTION.USER_DATA_REQUEST});
    try {
        const {data} = yield call(sendSignUpFormData, signUpData);

        if(data) {
            window.localStorage.setItem("token", data.token);
        }
        yield put(
            {
                type: ACTION.USER_DATA_RESPONSE,
                role: data.role,
                token: data.token,
                userName: data.userName
            }
        );
    } catch (e) {
        yield put({type: ACTION.USER_DATA_ERROR, error: e})
    }
}

export function* authSaga({ authData }) {
    yield put({type: ACTION.USER_DATA_REQUEST});

    try {
        const {data} = yield call(sendAuthFormData, authData);

        if(data) {
            window.localStorage.setItem("token", data.token);
        }
        authData.adminPrivateKey && window.localStorage.setItem("adminPrivateKey", authData.adminPrivateKey);
        yield put(
            {
                type: ACTION.USER_DATA_RESPONSE,
                role: data.role,
                token: data.token,
                userName: data.userName
            }
        );
    } catch (e) {
        yield put({type: ACTION.USER_DATA_ERROR, error: e})
    }
}

export function* checkAuthUser() {
    if( window.localStorage.getItem("token") ) {
        yield put({type: ACTION.USER_DATA_REQUEST});

        try {
            const {data} = yield call(checkAuth, window.localStorage.getItem('token'));

            if(data) {
                window.localStorage.setItem("token", data.token);
            }
            yield put(
                {
                    type: ACTION.USER_DATA_RESPONSE,
                    role: data.role,
                    token: data.token,
                    userName: data.userName
                }
            );
        } catch (e) {
            window.localStorage.removeItem("token");
            yield put({type: ACTION.USER_DATA_ERROR, error: e});
        }
    }
}

export function* logout({ history }) {
    yield put({type: ACTION.USER_DATA_REQUEST});

    try {
        yield window.localStorage.removeItem("token");
        window.localStorage.getItem('adminPrivateKey')
            && window.localStorage.removeItem("adminPrivateKey");
        yield put({
            type: ACTION.USER_DATA_RESPONSE,
            role: null,
            token: null,
            userName: null
        });
        yield history.push("/");
    } catch (e) {
        yield put({type: ACTION.USER_DATA_ERROR, error: e});
    }
}

export function* setPageForCabinet({ page, changed }) {

    yield put({
        type: ACTION.PAGE_FOR_CABINET_APPEND,
        page: page,
        changed: changed
    })
}

export function* getPersonalData() {
    yield put({type: ACTION.PERSONAL_DATA_REQUEST});

    try{
        const {data} = yield call(getPersonalUserData, window.localStorage.getItem("token"));

        yield put(
            {
                type: ACTION.PERSONAL_DATA_RESPONSE,
                data: data
            }
        )
    }catch (e) {
        yield put({type: ACTION.PERSONAL_DATA_ERROR, error: e});
    }
}

export function* updatePersonalDataUser({ personalData }) {
    yield put({type: ACTION.PERSONAL_DATA_REQUEST});

    try{
        const {data} = yield call(updatePersonalData, personalData, window.localStorage.getItem("token"));
        yield put(
            {
                type: ACTION.PERSONAL_DATA_RESPONSE,
                data: data
            }
        );
        yield toast.success("Данные успешно сохранены");
    }catch (e) {
        yield put({type: ACTION.PERSONAL_DATA_ERROR, error: e});
        yield toast.error(errorsUpdatePersonalUserData[e.response.data.status]);
    }
}

export function* getUserOrders() {
    yield put({type: ACTION.HISTORY_ORDERS_REQUEST});

    try{
        const {data} = yield call(getHistoryOrders, window.localStorage.getItem("token"));
        yield put(
            {
                type: ACTION.HISTORY_ORDERS_RESPONSE,
                data: data
            }
        );
    }catch (e) {
        yield put({type: ACTION.HISTORY_ORDERS_ERROR, error: e});
    }
}

export function* deleteOrders({ orderId }) {
    yield put({type: ACTION.HISTORY_ORDERS_REQUEST});

    try{
        const {data} = yield call(deleteOrder, orderId, window.localStorage.getItem("token"));
        yield put(
            {
                type: ACTION.HISTORY_ORDERS_RESPONSE,
                data: data
            }
        );
    }catch (e) {
        yield put({type: ACTION.HISTORY_ORDERS_ERROR, error: e});
    }
}