import reduxList from 'lib/redux-list';
import createReducer, { safe } from 'lib/reducer';
import { listRequest, listResponse } from 'lib/list-backend';
import { settingsSelector } from 'utils/settings';

import backendRequest from 'lib/backend';
import { fromJS } from 'immutable';


let initialState = {
    periodParams: {
        periodType: 'last12Months'
    },
    list: {
        status: 'pending',
        paging: {},
        fresh: true,
        data: []
    },

    statusReport: {
        status: 'pending',
    },
    timelineReport: {
        status: 'pending',
    },
};

let reducer = createReducer(initialState);

//build API
let api = reduxList('incomeList', {
    reducer, 
	fetchRequest: listRequest(({ unitId }) => `/api/${unitId}/income`),
	fetchResponse: listResponse()
}); 

//export actions
export const fetchList = api.actions.fetchList;


export function incomeStatusReportAction(unitId, params) {
    return {
        type: '@@incomeStatusReport/REPORT',
        payload: backendRequest(`/api/${unitId}/income/report/status`, "GET", { params }),
        meta: {
            unitId: unitId,
            params
        }
    }
}


// report
reducer.handleAction("@@incomeStatusReport/REPORT_PENDING", (state, action) => state.setIn(["statusReport", "status"], "pending").setIn(["statusReport", "params"], action.meta.params));
reducer.handleAction("@@incomeStatusReport/REPORT_FULFILLED", (state, action) => {
    let { params, ...data } = action.payload.body;
    return state.setIn(["statusReport", "status"], "success").setIn(["statusReport", "params"], fromJS(params)).setIn(["statusReport", "data"], fromJS(data));
});
reducer.handleAction("@@incomeStatusReport/REPORT_REJECTED", (state, action) => state.setIn(["statusReport", "status"], "failure").deleteIn(["statusReport", "data"]) );


export function incomeTimelineReportAction(unitId, params) {
    return {
        type: '@@incomeTimelineReport/REPORT',
        payload: backendRequest(`/api/${unitId}/income/report/timeline`, "GET", { params }),
        meta: {
            unitId: unitId,
            params
        }
    }
}


// report
reducer.handleAction("@@incomeTimelineReport/REPORT_PENDING", (state, action) => state.setIn(["timelineReport", "status"], "pending").setIn(["timelineReport", "params"], action.meta.params));
reducer.handleAction("@@incomeTimelineReport/REPORT_FULFILLED", (state, action) => {
    let { params, ...data } = action.payload.body;
    return state.setIn(["timelineReport", "status"], "success").setIn(["timelineReport", "params"], fromJS(params)).setIn(["timelineReport", "data"], fromJS(data));
});
reducer.handleAction("@@incomeTimelineReport/REPORT_REJECTED", (state, action) => state.setIn(["timelineReport", "status"], "failure").deleteIn(["timelineReport", "data"]) );



export function incomeSummaryReportAction(unitId, params) {
    return {
        type: '@@incomeSummaryReport/REPORT',
        payload: backendRequest(`/api/${unitId}/income/report/summary`, "GET", { params }),
        meta: {
            unitId: unitId,
            params
        }
    }
}


// report
reducer.handleAction("@@incomeSummaryReport/REPORT_PENDING", (state, action) => state.setIn(["summaryReport", "status"], "pending").setIn(["summaryReport", "params"], action.meta.params));
reducer.handleAction("@@incomeSummaryReport/REPORT_FULFILLED", (state, action) => {
    let { params, ...data } = action.payload.body;
    return state.setIn(["summaryReport", "status"], "success").setIn(["summaryReport", "params"], fromJS(params)).setIn(["summaryReport", "data"], fromJS(data));
});
reducer.handleAction("@@incomeSummaryReport/REPORT_REJECTED", (state, action) => state.setIn(["summaryReport", "status"], "failure").deleteIn(["summaryReport", "data"]) );


//unit selector
export const selector = settingsSelector((state) => {
    if (!state || !state.get) {
        return {};
    }

    let income = safe(state.get("incomeList"));
    let timelineReport = safe(state.getIn(["incomeList", "timelineReport"]));
    let periodParams = safe(state.getIn(["params", "period"]));

    return { ...income, timelineReport, periodParams };
});


//export reducer
export default reducer.export();
