import configService from '../../shared/ConfigService';
import { close } from '../actions/parent';
import { toggleFavouriteInWindow } from '../../child/actions/favourites';
import { willBeInitialOpen } from '../../child/actions/initialOpen';

class ParentService {

    constructor(store) {
        this.store = store;
        this.parentClosing = false;
        this.onChildClosed = this.onChildClosed.bind(this);
        this.onCloseRequested = this.onCloseRequested.bind(this);

        fin.desktop.InterApplicationBus.subscribe(
            fin.desktop.Application.getCurrent().uuid,
            'createChildWindow',
            (config) => this.createChildWindow(config)
        );
    }

    getChildWindowCount() {
        return Object.keys(this.store.getState().childWindows).length;
    }

    getWindowDragOutCount() {
        return Object.keys(this.store.getState().dragOut).length;
    }

    onChildClosed({ name }) {
        if (this.parentClosing) {
            return;
        }

        const isFinalChildWindowClosing = this.getChildWindowCount() <= 1;

        if (isFinalChildWindowClosing && this.getWindowDragOutCount() === 0) {
            fin.desktop.Window.getCurrent().close();
        } else {
            this.store.dispatch(close(name, Date.now()));
        }
    }

    onCloseRequested() {
        this.parentClosing = true;
        fin.desktop.Window.getCurrent().close(true);
    }

    createChildWindowSuccess(childWindow, position, defaultStocks) {
        if (position) {
            childWindow.setBounds(position[0], position[1]);
        }

        if (defaultStocks) {
            const childWindowName = childWindow.name;
            this.store.dispatch(willBeInitialOpen(childWindowName));
            defaultStocks.forEach((code) => {
                this.store.dispatch(toggleFavouriteInWindow(code, childWindowName));
            });
        }

        childWindow.show();
        childWindow.addEventListener('closed', this.onChildClosed);
    }

    createChildWindow(config = {}) {
        const { windowName, position, intialState, defaultStocks } = config;
        const windowConfig = this.getChildWindowConfigOrDefault(windowName, intialState);
        const childWindow = new fin.desktop.Window(
            windowConfig,
            () => this.createChildWindowSuccess(childWindow, position, defaultStocks)
        );
    }

    getChildWindowConfigOrDefault(windowName, intialState) {
        let windowConfig;
        if (windowName && intialState) {
            if (intialState.isCompact) {
                windowConfig = configService.getCompactWindowConfig(windowName);
            } else if (intialState.isMaximized) {
                windowConfig = configService.getMaximizedWindowConfig(windowName);
            } else {
                windowConfig = configService.getWindowConfig(windowName);
            }
        } else if (windowName) {
            windowConfig = configService.getWindowConfig(windowName);
        } else {
            windowConfig = configService.getWindowConfig();
        }
        return windowConfig;
    }

    start() {
        fin.desktop.Window.getCurrent().contentWindow.store = this.store;

        fin.desktop.Window.getCurrent().addEventListener('close-requested', this.onCloseRequested);

        if (this.getChildWindowCount() === 0) {
            this.createChildWindow({ defaultStocks: configService.getDefaultStocks() });
        } else {
            const childWindows = this.store.getState().childWindows;
            Object.keys(childWindows).forEach((windowName) => {
                const newWindowName = windowName === 'undefined' ? null : windowName;
                this.createChildWindow({ windowName: newWindowName, initalState: childWindows[newWindowName] });
            });
        }
    }

}

export default ParentService;

@@ -2062,24 +2062,25 @@
 position, in
+i
 tialState, d
@@ -2174,32 +2174,33 @@
 t(windowName, in
+i
 tialState);%0A    
@@ -2425,16 +2425,17 @@
 Name, in
+i
 tialStat
@@ -2493,16 +2493,17 @@
 me && in
+i
 tialStat
@@ -2521,24 +2521,25 @@
       if (in
+i
 tialState.is
@@ -2536,16 +2536,28 @@
 alState.
+windowState.
 isCompac
@@ -2667,16 +2667,17 @@
 e if (in
+i
 tialStat
@@ -2678,16 +2678,28 @@
 alState.
+windowState.
 isMaximi
@@ -3758,16 +3758,17 @@
 me, init
+i
 alState:

fix child window config in parent service (#944)