/* tslint:disable */
import * as _ from 'lodash';
import { IEditorInput } from '@workbench-stack/core';
import { EditorActions, FilesActions,
} from '@files-stack/client-state';
import { Store } from 'redux';
import { createSelector } from 'reselect';
import { Observable, combineLatest, BehaviorSubject } from 'rxjs';
import { pluck, map, shareReplay, tap } from 'rxjs/operators';
import { find, whereEq, propEq, prop, sortBy, compose, toLower } from 'ramda';

const editSelect = state => _.get(state, 'files_stack__edit');

const editorsSelect = createSelector(editSelect, edit => _.get(edit, 'editors', []));
const editingFilesSelect = createSelector(editSelect, edit => _.get(edit, 'editingFiles', []));
const openingFilesSelect = createSelector(editSelect, edit => _.get(edit, 'openingFiles', []));
const activeEditorIdSelect = createSelector(editSelect, edit => _.get(edit, 'activeEditorId', null));
const lastEditorIdSelect = createSelector(editorsSelect, editors => editors[editors.length - 1] || 0);

const contentSelect = createSelector(
    openingFilesSelect, editingFilesSelect, activeEditorIdSelect,
    (openingFiles, editingFiles, editorId) => getFileContent(openingFiles, editingFiles, editorId),
);

const fromStore = (store: Store<any>, selector: any) => () => {
    return selector(store.getState());
};

export const getFileContent = (openingFiles, editingFiles, editorId) => {
    const editingFile = find(propEq('editorId', editorId))(editingFiles);

    if (editingFile) {
        return find(whereEq({
            resource: editingFile.resource,
            name: editingFile.name,
            editorId,
        }))(openingFiles);
    } else {
        return null;
    }
};

export interface IEditorWindowsService {
    editors: () => any[];
    content: () => any;
    openingFiles: () => any[];
    editingFiles: () => any[];
    lastEditorId: () => number;
    activeEditorId: () => number;

    openEditorWindow(content: IEditorInput): void;
    closeEditorWindow(content: IEditorInput): void;
    selectEditorWindow(content: IEditorInput): void;
    updateEditorWindow(content: IEditorInput): void;
}

export const editorWindowsService = ({ store, reduxState$ }: { store: Store<any>, reduxState$: Observable<any> }): IEditorWindowsService => {
    return ({
        editors: fromStore(store, editorsSelect),
        content: fromStore(store, contentSelect),
        lastEditorId: fromStore(store, lastEditorIdSelect),
        openingFiles: fromStore(store, openingFilesSelect),
        editingFiles: fromStore(store, editingFilesSelect),
        activeEditorId: fromStore(store, activeEditorIdSelect),

        selectEditorWindow: (content: IEditorInput) => {
            // store.dispatch(EditorActions.selectEditAction(content as any));
            store.dispatch(FilesActions.selectFileAction(content as any));
        },

        openEditorWindow: (content: IEditorInput) => /** store.dispatch(EditorActions.openFileAction(content as any) */ {},
        closeEditorWindow: (content: IEditorInput) => /** store.dispatch(EditorActions.removeEditAction(content)) */ {},
        updateEditorWindow: (content: IEditorInput) => /** store.dispatch(EditorActions.updateEditorContentAction(content as any) */ {},
    });
};
