import { 
	SORT_BY_TAG,
	SORT_BY_TIME,
	TAGGED_ANSWERS_SUCCESS,
	LOADING,
	BADGE_USERS_SUCCESS,
	BADGE_USERS_FAILED,
	SORT_BY_BADGE,
	SHOW_POPOVER,
	GET_USER,
	LOAD_MORE_USERS_SUCCESS,
} from '../actions/stackOverflow';
import { badgeMenuItems } from '../containers/StackOverflowBadges/menuItems';
import randomArrayItem from '../helpers/randomArrayItem';

const randomBadgesArrayItem = randomArrayItem(badgeMenuItems);

const initialState = {
	sortedTag: 'javascript',
	sortedTime: 'month',
	sortedTagLabel: 'Tag (Javascript)',
	sortedTimeLabel: 'Time Period (Month)',	
	loading: false,
	sortedBadge: randomBadgesArrayItem.menuItem,
	sortedBadgeLabel: randomBadgesArrayItem.menuItemLabel,
	badgeUsers: [],
	popoverId: '',
	userId: '',
	responseEnd: false,
};

const stackOverflowReducer = (state = initialState, action) => {
	switch (action.type) {
		case SORT_BY_TAG:
			return {
				...state,
				sortedTag: action.tag,				
				sortedTagLabel: action.label,				
			}

		case SORT_BY_TIME:
			return {
				...state,
				sortedTime: action.timePeriod,				
				sortedTimeLabel: action.label
			}
		case LOADING:
			return {
				...state,
				loading: true
			}
		case TAGGED_ANSWERS_SUCCESS:
			return {
				...state,
				taggedAnswers: action.response,
				loading: false
			}
		case SORT_BY_BADGE:
			return {
				...state,
				badgeUsers: [],
				sortedBadge: action.badge,
				sortedBadgeLabel: action.label,
				responseEnd: false,
			}
		case BADGE_USERS_SUCCESS:			
			return {
				...state,
				badgeUsers: action.response,
				loading: false,
			}
		case LOAD_MORE_USERS_SUCCESS:			
			return {
				...state,
				badgeUsers: [...state.badgeUsers, ...action.response],
				loading: false,
				responseEnd: action.responseEnd,
			}
		case BADGE_USERS_FAILED:
			return {
				...state,
				loading: false,
			}
		case SHOW_POPOVER:			
			return {
				...state,
				popoverId: action.id,
			}
		case GET_USER:
			return {
				...state,
				userId: action.userId,
			}
		default:
			return state
	}
}

export {
	stackOverflowReducer
}