import { RESIZE_WINDOW } from './Actions';
import { isMobileDevice } from 'redhotmagma-visualization/Utils/isMobileDevice';

const getScreenObject = function() {
    const { innerWidth: width, innerHeight: height, matchMedia } = window;
    const orientation = matchMedia('(orientation: portrait)').matches ? 'portrait' : 'landscape';

    return {
        width,
        height,
        ratio: width / height,
        orientation,
        isMobileDevice: isMobileDevice(),
        isTabletDevice: isMobileDevice() && Math.min(width, height) > 760,
    };
};

const initialState = {
    screen: getScreenObject(),
};

export default function deviceReducer(state = initialState, action = {}) {
    switch (action.type) {
        case RESIZE_WINDOW:
            return {
                ...state,
                screen: getScreenObject(),
            };

        default:
            return state;
    }
}
