import fetch from 'isomorphic-fetch';
import actionTypes from '../actiontype/user'
import {saveHeaderInfo} from './initHeader'
import {addErrorInfo} from './uploadError'
import {saveCommonInfo, getUserinfo, emptyStoreInfo} from "./common";
const domain = ENV.domain;


const saveUserinfo = (data)=>{
    return{
        type:actionTypes.SAVEUSERINFO,
        data
    }
};

var myHeaders = new Headers({
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json; charset=UTF-8',
});



const getUserinfoOld = (info,headerInfo)=>{
    let employeeId = info.employeeId;
    let storeId = headerInfo.storeId;
    let bodyData = Object.assign({},{employeeId,storeId})
    return(dispatch)=>{
        dispatch(saveHeaderInfo(info));
        fetch(domain + 'machine_pad/get_alowed_task.action',{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers:myHeaders,
            body : JSON.stringify(bodyData)
        }).then((response)=>response.json())
            .then(json=>{
                if(json.meta.success && json.data){
                    dispatch(saveUserinfo(json.data));
                    if(!json.data.type){
                        dispatch(addErrorInfo('没有任务需要处理'))
                    }
                }
            }).catch(e=>{})
    }
}

const logoutUser = ()=>{
    return {
        type : actionTypes.LOGOUTUSER,
    }
}


let timer = null;

const pollingGetUserinfoAgain = (storeId)=>{
    return (dispatch)=>{
        if(!timer){
            timer = setTimeout(()=>{
                UTILPATH.myLogger.info("pollingGetUserinfoAgain storeId ",storeId)
                clearTimeout(timer);
                timer = null;
                dispatch(pollingGetUserinfo(storeId))
            },2*1000)
        }
    }
}


const pollingGetUserinfo = (storeId)=>{
    UTILPATH.myLogger.info("power_cut_find_user storeId ",storeId)
    return(dispatch)=>{
        fetch(domain + `machine_pad/power_cut_find_user/${storeId}`,{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers:myHeaders,
            body:JSON.stringify({})
        }).then((response)=>response.json())
            .then(json=>{
                UTILPATH.myLogger.info("power_cut_find_user response ",json)
                let {meta={},data={}} = json;
                if(meta.success && data.code){
                    let code = parseInt(data.code);
                    switch (code){
                        case 1:
                            //开门成功
                            let {employeeId,area} = data;
                            if(employeeId && area){
                                dispatch(saveCommonInfo({userInfo:{employeeId}}));
                                dispatch(saveCommonInfo(Object.assign({},{taskInfo:{area:area,taskId:0,currentTaskType:'OPEN'}})));
                                dispatch(getUserinfo(employeeId,storeId));
                            }else{
                                dispatch(pollingGetUserinfoAgain(storeId));
                            }
                            break;
                        case 2:
                            //开门失败
                            dispatch(emptyStoreInfo());
                            break;
                        case 3:
                            //暂无信息，继续调用
                            dispatch(pollingGetUserinfoAgain(storeId));
                            break;
                    }
                }else{
                    dispatch(pollingGetUserinfoAgain(storeId));
                }
            }).catch(e=>{
            UTILPATH.myLogger.error(`power_cut_find_user error ${e}`);
            dispatch(pollingGetUserinfoAgain(storeId));
        })
        // dispatch(tempGetUserInfo(storeId));
    }
};



export {pollingGetUserinfo}

