const getDefaultState = () => ({
    user: [],
    dio: {
        di: [],
        do: [],
        diSt: {},
        doSt: {}
    },
    log: {
        list: [],
        page: {}
    },
    leone: {
        scanList: [],
        list: [],
        status: [],
        scanning: false
    },
    iogroup: {
        list: [],
        do: [],
        leone: []
    },
    schedule: {
        list: [],
        do: [],
        leone: [],
        iogroup: []
    },
    modbus: {
        list: []
    },
    link: {
        list: []
    },
    selectdev: []
})

const listsReducer = (state = getDefaultState(), action) => {
    switch (action.type) {
        case 'user_list':
            return {...state, user: action.user };
        case 'clear_user_list':
            return {...state, user: [] };
        case 'dio_list':
            return {...state,
                dio: {
                    ...state.dio,
                    di: action.di,
                    do: action.do
                }
            };
        case 'dio_status':
            return {...state,
                dio: {
                    ...state.dio,
                    diSt: action.diSt,
                    doSt: action.doSt
                }
            };
        case 'clear_dio':
            return {...state, dio: {...getDefaultState().dio } };
        case 'log_list':
            return {...state,
                log: {
                    list: action.list,
                    page: action.page
                }
            };
        case 'clear_log':
            return {...state, log: {...getDefaultState().log } };
        case 'leone_list':
            return {
                ...state,
                leone: {
                    ...state.leone,
                    list: action.list,
                    status: action.status
                }
            };
        case 'leone_scanning':
            return {
                ...state,
                leone: {
                    ...state.leone,
                    scanning: true
                }
            }
        case 'leone_scan':
            return {
                ...state,
                leone: {
                    ...state.leone,
                    scanList: action.list,
                    scanning: false
                }
            };
        case 'leone_clear_scan':
            return {
                ...state,
                leone: {
                    ...state.leone,
                    scanList: []
                }
            }
        case 'clear_leone':
            return {...state, leone: {...getDefaultState().leone } };
        case 'iogroup_list':
            return {
                ...state,
                iogroup: {
                    ...state.iogroup,
                    list: action.list,
                    do: action.do,
                    leone: action.leone
                }
            };
        case 'clear_iogroup':
            return {...state, iogroup: {...getDefaultState().iogroup } };
        case 'select_list':
            return {
                ...state,
                selectdev: action.list
            };
        case 'clear_select_list':
            return {
                ...state,
                selectdev: []
            };
        case 'schedule_list':
            return {
                ...state,
                schedule: {
                    list: action.list,
                    do: action.dos,
                    leone: action.les,
                    iogroup: action.ios
                }
            };
        case 'clear_schedule':
            return {...state,
                schedule: {
                    ...getDefaultState().schedule
                }
            }
        case 'modbus_list':
            return {...state, modbus:{
                ...state.modbus,
                list: action.list
            }}
        case 'mb_data':
            return {
                ...state,
                modbus: {
                    list: state.modbus.list.map(t=> mb_data(t, action))
                }
            }
        case 'mb_iostatus':
            return {
                ...state,
                modbus: {
                    list: state.modbus.list.map(t=> mb_data(t, action))
                }
            }
        case 'clear_modbus':
            return {...state, modbus: {...getDefaultState().modbus}};
        case 'link_list':
            return {
                ...state,
                link: {
                    list: action.list
                }
            }
        case 'clear_link':
            return {
                ...state,
                link:{
                    ...getDefaultState().link
                }
            }
        default:
            return state;
    }
}

function mb_data(state = {}, action){
    switch(action.type){
        case 'mb_data':
            if(action.id != state.uid) return state;
            return {
                ...state,
                iolist: action.iolist || [],
                aioset: action.aioset || []
            };
        case 'mb_iostatus':
            if(action.id != state.uid) return state;
            let json = {};
            json[action.iotype] = action.list;
            return {
                ...state,
                status: {
                    ...(state.status || {}),
                    ...json
                }
            };
        default: 
            return {
                ...state,
                iolist: state.iolist || []
            };
    }
}

export default listsReducer;