import {createAction, NavigationActions, Storage} from '../utils'
import SyncStorage from 'sync-storage';
import * as authService from '../services/user'
import {REFRESH_STATE} from "react-native-refresh-flatlist";
import {userfetch} from '../services/user'
import TurnPage from "../components/TurnPage";
import ResponseError from "../components/ResponseError";

let noticeData = [], receiveMessageData = [], sendMessageData = []
export default {
    namespace: 'auth',
    state: {
        login: false,
        loading: false,
        fetching: false,
        errorMessage: '',
        infoData: {},
        noticeData: [],
        noticeRState: "",
        sendMessageData: [],
        sendMessageRState: '',
        receiveMessageData: [],
        receiveMessageRState: ""
    },
    reducers: {
        updateState(state, {payload}) {
            return {...state, ...payload}
        },
        queryList(state, {payload}) {
            return {...state, ...payload}
        },
    },
    effects: {
        * authLogin({payload, callback, unLogin}, {call, put}) {
            const result = yield call(authService.authLogin, payload)
            if (result.success) {
                SyncStorage.set('credential', result.data);
                if (callback) {
                    callback()
                }
            } else {
                SyncStorage.remove('credential');
                unLogin()
            }
        },
        * login({payload, callback, unLogin}, {call, put}) {
            yield put(createAction('updateState')({fetching: true}))
            const result = yield call(authService.login, payload)
            if (result.success) {
                if (callback) {
                    callback()
                }
                SyncStorage.set('credential', result.data);
                yield put(NavigationActions.navigate({
                    routeName: "Main",
                    actions: [NavigationActions.navigate({routeName: 'SalesHome'})],
                }))
            } else {
                if (result.errors) {
                    unLogin(result.errors)
                }
            }
            yield put(createAction('updateState')({fetching: false}))
        },
        * logout({payload}, {call, put}) {
            yield call(authService.loginOut)
            SyncStorage.remove('credential');
            yield put(NavigationActions.navigate({
                routeName: "Login",
                actions: [NavigationActions.navigate({routeName: 'Login'})],
            }));
        },
        * fetchrnPermissions({payload}, {call, put}) {
            const response = yield call(authService.queryPermissions);
            if (response.success) {
                SyncStorage.set('premissions', response.data);
            }
        },
        * fetchRNInfo({payload}, {call, put}) {
            yield put(createAction('updateState')({infoData: {}, loading: true}))
            const response = yield call(authService.queryInfo);
            if (response.success) {
                yield put(createAction('updateState')({infoData: response.data, loading: false}))
            }else {
                ResponseError(response)
                yield put(createAction('updateState')({infoData: {}, loading: false}))
            }
        },
        //公告列表
        * fetchNoticeList({payload}, {call, put}) {
            if (payload.getParm.page > 1) {
                yield put(createAction('queryList')({noticeRState: REFRESH_STATE.FooterRefreshing,}));
            } else {
                yield put(createAction('queryList')({noticeRState: REFRESH_STATE.HeaderRefreshing,}));
            }
            const response = yield call(authService.queryNoticeList, payload);
            if (response.success) {
                const result = TurnPage(response, payload.getParm, noticeData)
                noticeData = result.data
                yield put(createAction('queryList')({
                    noticeData: noticeData, noticeRState: result.state,
                    total: response.total, page: payload.getParm.page
                }));
            } else {
                ResponseError(response)
                yield put(createAction('queryList')({noticeRState: REFRESH_STATE.Ready,}));
            }

        },
        //公告详情
        * fetchrNoticeDetail({payload, callback}, {call, put}) {
            const response = yield call(authService.queryNoticeDetail, payload);
            if (response.success) {
                if (callback) {
                    callback(response)
                }
            } else {
                ResponseError(response)
            }
        },
        //回复公告
        * fetchReplyNotice({payload, callback}, {call, put}) {
            yield put(createAction('queryList')({loading: true,}));
            const response = yield call(authService.replyNotice, payload);
            if (response.success) {
                if (callback) {
                    callback()
                }
            } else {
                ResponseError(response)
            }
            yield put(createAction('queryList')({loading: false,}));
        },
        //私信列表
        * fetchReceiveMessageList({payload}, {call, put}) {
            if (payload.getParm.page > 1) {
                yield put(createAction('queryList')({receiveMessageRState: REFRESH_STATE.FooterRefreshing,}));
            } else {
                yield put(createAction('queryList')({receiveMessageRState: REFRESH_STATE.HeaderRefreshing,}));
            }
            const response = yield call(authService.queryReceiveMessageList, payload);
            if (response.success) {
                const result = TurnPage(response, payload.getParm, receiveMessageData)
                receiveMessageData = result.data
                yield put(createAction('queryList')({
                    receiveMessageData: receiveMessageData, receiveMessageRState: result.state,
                    receiveTotal: response.total, receivePage: payload.getParm.page
                }));
            } else {
                ResponseError(response)
                yield put(createAction('queryList')({receiveMessageRState: REFRESH_STATE.Ready,}));
            }
        },
        //私信列表
        * fetchSendMessageList({payload}, {call, put}) {
            if (payload.getParm.page > 1) {
                yield put(createAction('queryList')({sendMessageRState: REFRESH_STATE.FooterRefreshing,}));
            } else {
                yield put(createAction('queryList')({sendMessageRState: REFRESH_STATE.HeaderRefreshing,}));
            }
            const response = yield call(authService.querySendMessageList, payload);
            if (response.success) {
                const result = TurnPage(response, payload.getParm, sendMessageData)
                sendMessageData = result.data
                yield put(createAction('queryList')({
                    sendMessageData: sendMessageData, sendMessageRState: result.state,
                    sendTotal: response.total, sendPage: payload.getParm.page
                }));
            } else {
                ResponseError(response)
                yield put(createAction('queryList')({sendMessageRState: REFRESH_STATE.Ready,}));
            }
        },
        //读私信
        * fetchReadReceiveMessage({payload, callback}, {call, put}) {
            const response = yield call(authService.readReceiveMessage, payload);
            if (response.success) {
                if (callback) {
                    callback(response)
                }
            } else {
                ResponseError(response)
            }
        },
        //回复私信
        * fetchReplyMessage({payload, callback}, {call, put}) {
            yield put(createAction('queryList')({loading: true,}));
            const response = yield call(authService.replyMessage, payload);
            if (response.success) {
                if (callback) {
                    callback()
                }
            } else {
                ResponseError(response)
            }
            yield put(createAction('queryList')({loading: false,}));
        },
        //修改密码
        * changePassword({payload, callback}, {call, put}) {
            yield put(createAction('queryList')({loading: true,}));
            const response = yield call(authService.changePassword, payload);
            if (response.success) {
                if (callback) {
                    callback()
                }
            } else {
                ResponseError(response)
            }
            yield put(createAction('queryList')({loading: false,}));
        },
        //验证登录地址
        * checkBaseURL({payload, callback}, {call, put}) {
            const response = yield call(authService.checkBaseURL, payload);
            if (response.success) {
                if (response.data === 'carwin') {
                    callback(true)
                } else {
                    callback(false)
                }
            } else {
                callback(false)
            }
        },
    },
    subscriptions: {
        setup({dispatch}) {
            //dispatch({ type: 'loadStorage' })
        },
    },
}
