import {
    IController, ICompletionParams, IReferencesParams,
    IDocumentFormattingEditsParams,
} from '@workbench-stack/extension-api';
import { PubSub } from 'graphql-subscriptions';
import * as Logger from 'bunyan';
import { ITextDocumentPositionParams, IResolvers } from '@workbench-stack/core';
const __typename = 'LangaugeFeature';

type EditorContext = {
    cache: any,
    controller: IController,
};

export const resolvers: (pubsub: PubSub, logger?: Logger) => IResolvers<EditorContext> = (pubsub, logger) =>  ({
    Query: {
        getViews: (_, { containerId }, { cache, controller }) => controller.services.treeViews.getViews(containerId),
        registerProvider: (_, args, context) => {
            // const entries = getCacheKey({ __typename  });
            // const updateEntries = [...entries.value.filter(e => !args.entries.includes(e))];
            // cache.writeQuery({
            //     query: REGISTER_PROVIDERS_QUERY,
            //     data: { updateEntries },
            // });

            // return { ...updateEntries, __typename };
            console.log('Called registerProvider');
            return true;
        },
    },
    Mutation: {
        openLanguageDocument: async (_, { params }, { cache, controller }) => {
            console.log('not implemented yet should open document');
            return [];
        },
        hoverProviderDefinition: async (_, { params }, { cache, controller }) => {
            let result = await controller.services.textDocumentHover.getHover(params);
            return result;
        },
        codeLenseProviderDefinition: async (_, { params }, { cache, controller }) => {
            let result = await controller.services.textDocumentCodeLenth.getCodeLenth(params);
            console.log('codeLenseProviderDefinition : >', result);
            return [];
        },
        completionProviderDefinition: async (_, { params }: { params: ICompletionParams }, { cache, controller }) => {
            let result = await controller.services.textDocumentСompletion.getCompletion(params);
            return result;
        },
        documentFormattingProviderDefinition: async (_, { params }: { params: IDocumentFormattingEditsParams }, { cache, controller }) => {
            let result = await controller.services.textDocumentFormatting.getFormatting(params);
            return result;
        },
        documentSymbolDefinition: async (_, { params }, { cache, controller }) => {
            let result = await controller.services.textDocumentSymbolDefinition.getSymbolDefinition(params);
            return result;
        },
        referenceProviderDefinition: async (_, { params }: { params: IReferencesParams }, { cache, controller }) => {
            const result = await controller.services.textDocumentReferences.getLocations(params);
            return result;
        },
        definitionDefinition: async (_, { params }: { params: any }, { cache, controller }) => {//ReferenceParams}, { cache, controller }) => {
            const result = await controller.services.textDocumentDefinition.getLocations(params);
            return result;
        },
        documentHighLightProviderDefinition: async (_, { params }, { cache, controller }) => {
            let result = await controller.services.textDocumentHightLight.getHightLight(params);
            return result;
        },
        renameDefinition: async (_, { params }, { cache, controller }) => {
            let result = await controller.services.textDocumentRename.getRename(params);
            return result;
        },
        updateModel: (_, { model }, context) => {
            console.log('model----------------------->', model);
            // context.controller.services.model.model.next(model);
            return true;
        },
    },
});
