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


import { fromJS, toJS } from 'immutable';

import moment from 'moment';


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

	summary: {
		status: 'pending',
	},
	cashflow: {
		status: 'pending',
		fresh: true,
		data: {
			empty: true
		}
	},
	statuses: {
		status: 'pending',
		fresh: true,
		data: {
			income: {},
			outcome: {},
			data: {},
			empty: true
		}
	}
};

let reducer = createReducer(initialState);


let api = reduxList("home/dashboard", {
	reducer, 
	fetchRequest: listRequest(({ unitId }) => `/api/${unitId}/home/records`),
	fetchResponse: listResponse()
}); 

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

//TODO
export const selector = settingsSelector((state, props) => {
	let home = safe(state.get("home"));
	let periodParams = safe(state.getIn(["params", "period"]));
	let list = api.selector(state, props);
	return { ...home, periodParams };
});




export function fetchSummary(unitId, from, to, dateType) {
	return {
		type: 'home/FETCH_SUMMARY',
		payload: backendRequest('/api/' + unitId + '/home/dashboard', 'GET', { params: { from, to, dateType } })
	};
};

reducer.handleAction('home/FETCH_SUMMARY_PENDING', (state, action) => 
	state.setIn(["summary", "status"], "pending")
);
reducer.handleAction('home/FETCH_SUMMARY_FULFILLED', (state, action) => 
	state
		.setIn(["summary", "status"], "success")
		.setIn(["summary", "data"], action.payload.body)
		.deleteIn(["summary", "error"])
);
reducer.handleAction('home/FETCH_SUMMARY_REJECTED', (state, action) => 
	state.setIn(["summary", "status"], "failed").deleteIn(["summary", "data"]).setIn(["summary", "error"], true)
);


export function fetchCashflow(unitId, from, to, dateType) {
	return {
		type: 'home/FETCH_CASHFLOW',
		payload: backendRequest('/api/' + unitId + '/home/cashflow', 'GET', { params: { from, to, dateType } })
	};
};

reducer.handleAction('home/FETCH_CASHFLOW_PENDING', (state, action) => 
	state.setIn(["cashflow", "status"], "pending")
);
reducer.handleAction('home/FETCH_CASHFLOW_FULFILLED', (state, action) => 
	state
		.withMutations(s => {
			let b = action.payload.body;
			s = s.setIn(["cashflow", "data"], b.data);
			s = s.setIn(["cashflow", "labels"], b.labels);
			s = s.setIn(["cashflow", "params"], b.params);
			s = s.setIn(["cashflow", "currency"], b.currency);
			s = s.setIn(["cashflow", "currencySymbol"], b.currencySymbol);
			s = s.deleteIn(["cashflow", "error"]);
			return s;
		})
		.setIn(["cashflow", "status"], "success")
);
reducer.handleAction('home/FETCH_CASHFLOW_REJECTED', (state, action) => 
	state.setIn(["cashflow", "status"], "failed").setIn(["cashflow", "error"], true)
);



export function fetchStatuses(unitId, from, to, dateType) {
	return {
		type: 'home/FETCH_STATUSES',
		payload: backendRequest('/api/' + unitId + '/home/statuses', 'GET', { params: { from, to, dateType } })
	};
};

reducer.handleAction('home/FETCH_STATUSES_PENDING', (state, action) => 
	state.setIn(["statuses", "status"], "pending")
);
reducer.handleAction('home/FETCH_STATUSES_FULFILLED', (state, action) => 
	state
		.withMutations(s => {
			s = s.set("statuses", fromJS(action.payload.body));
			s = s.setIn(["statuses", "status"], "success");
			s = s.deleteIn(["statuses", "error"]);
			return s;
		})
);
		
reducer.handleAction('home/FETCH_STATUSES_REJECTED', (state, action) => 
	state.setIn(["statuses", "status"], "failed").setIn(["statuses", "error"], true)
);


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


//TODO transform summary response