//private void ParseFile(ProjectFileViewModel fileViewModelToParse)
        //{
        //    var fileDto = ProjectMVVMConverters.FromViewModelToModel(fileViewModelToParse);

        //    MockWebServiceUtility.ParseFile(fileDto, response =>
        //    {
        //        if (response.Parse == null)
        //            return;

        //        var errors = response.Parse.Errors;
        //        //response.Parse.Errors
        //        if (errors != null)
        //        {
        //            Logger.AppendLine("{0} error(s) detected.", errors.Length);
        //            if (errors.Length > 0)
        //                foreach (var err in errors)
        //                    Logger.AppendLine("{0}: In file {1} ({2}, {3}): {4}", err.Type, err.FileName, err.Line, err.Column, err.Message);
        //        }

        //    });
        //}

        //private static FileOperationResponse DoAutoComplete(FileOperationRequest request)
        //{
        //    return AutocompleteServiceUtil.DoAutoComplete(request);
        //}


        private void AnalyzeProject2(bool ctrlSpace)
        {
            //ctrlSpace = true;
            var sw = Stopwatch.StartNew();

            var projectVm = (ProjectViewModel)DataContext;
            var fileVm = projectVm.SelectedProjectItem as ProjectFileViewModel;
            if (fileVm == null)
            {
                Logger.AppendLine("A file must be selected");
                return;
            }

            fileVm.CaretIndex = SelectedFileContent.CaretIndex;
            // Synchronize the view's content with the viewmodel (since by default this is only done AFTER the textbox loses focus)
            fileVm.Content = SelectedFileContent.Text;

            // update file content in local data store if not already done
            var projectModelRepo = EndpointHost.AppHost.TryResolve<IProjectModelRepository>();
            projectModelRepo.SaveFileContent(478, fileVm.ProjectId, fileVm.Id, fileVm.Content);


            var fileRequest = new FileOperationRequest()
            {
                UserId = 478, //projectVm..UserId,
                ProjectId = projectVm.ProjectId,
                FileId = fileVm.Id,
                CompleteCode = new FileCodeCompletionRequest()
                {
                    AutoComplete = true,
                    Offset = fileVm.CaretIndex,
                    LineNumber = fileVm.CaretLine,
                    ColumnNumber = fileVm.CaretColumn,
                    CtrlSpace = ctrlSpace
                },
                Parse = new FileParseRequest()
            };


            var fileResponse = AutocompleteServiceUtil.DoAutoComplete(fileRequest);

#if false

            var projectDto = ProjectMVVMConverters.FromViewModelToModel(projectVm);

            var projectModel = ProjectModelConverters.FromDtoToModel(projectDto);

            var analysisRequest = new ProjectAnalysisRequest()
            {
                ProjectModel = projectModel,
                CodeCompletionParameters = new ProjectAnalysisCodeCompletionParameters()
                {
                    CtrlSpace = ctrlSpace,
                    FileId = fileVm.Id,
                    Offset = fileVm.CaretIndex
                    //Line = request.CompleteCode.LineNumber,
                    //Column = request.CompleteCode.ColumnNumber,
                    //CtrlSpace = true // always true for now
                }
            };

            ProjectAnalysisResult analysisResult = NRefactoryUtils.RunFullProjectAnalysis(analysisRequest);

            //StatelessProjectResponse response = MockWebServiceUtility.Server_HandleStatelessCodeCompletionRequest(request);

            FileOperationResponse response = new FileOperationResponse();
            // Convert analysis result model to file operation response DTO
            if (analysisResult.CompletionOptions != null)
            {
                response.CodeCompletion = new FileCodeCompletionResponse();
                response.CodeCompletion.CompletionOptions = analysisResult.CompletionOptions
                    .Select(ProjectModelConverters.FromICompletionDataToFileCodeCompletionResult).ToArray();
                for (int i = 0, len = response.CodeCompletion.CompletionOptions.Length; i < len; i++)
                    response.CodeCompletion.CompletionOptions[i].Id = i;
                response.CodeCompletion.CompletionWord = analysisResult.CompletionWord;
                if (analysisResult.BestMatchToCompletionWord != null)
                    response.CodeCompletion.BestMatchToCompletionWord = response.CodeCompletion.CompletionOptions.FirstOrDefault(x => x.CompletionText == analysisResult.BestMatchToCompletionWord.CompletionText);
            }
            var allErrors = new List<FileParseResult>();
            foreach (var fileModel in analysisRequest.ProjectModel.GetFileDescendants())
            {
                allErrors.AddRange(fileModel.Parser.ErrorsAndWarnings
                                       .Select(x => new FileParseResult()
                                       {
                                           FileId = fileModel.Id,
                                           FileName = fileModel.Name,
                                           Line = x.Region.BeginLine,
                                           Column = x.Region.BeginColumn,
                                           Type = x.ErrorType,
                                           Message = x.Message
                                       }).ToArray());
            }
            response.ParseResults = allErrors.ToArray();
#endif

            //var jsonResponse = JsonConvert.SerializeObject(fileResponse, Formatting.Indented);
            //Logger.AppendLine(jsonResponse);

            // Summarize results...
            Logger.AppendLine("=========================================================");
            Logger.AppendLine("Project analysis completed in {0} ms", sw.ElapsedMilliseconds);

            if (fileResponse.CodeCompletion == null)
            {
                Logger.AppendLine("No Completion Results.");
                Logger.SetCodeCompletionOptions(null, null);
            }
            else
            {
                var codeCompletion = fileResponse.CodeCompletion;

                Logger.SetCodeCompletionOptions(codeCompletion.CompletionOptions, codeCompletion.BestMatchToCompletionWord);

                Logger.AppendLine("Completion Results...");
                Logger.AppendLine("  Input:  Line:{0}  Col:{1}  Offset:{2}", codeCompletion.Line, codeCompletion.Column, codeCompletion.Offset);
                Logger.AppendLine("  Context: \"{0}\" <cursor> \"{1}\"", codeCompletion.TextBeforeCursor, codeCompletion.TextAfterCursor);
                Logger.AppendLine("  {0} code completion option(s) generated.", codeCompletion.CompletionOptions.Length);

                // Try to find closest matching completion result
                if (string.IsNullOrWhiteSpace(codeCompletion.CompletionWord))
                {
                    Logger.AppendLine("  No code completion word detected.");
                }
                else
                {
                    if (codeCompletion.BestMatchToCompletionWord != null)
                    {
                        Logger.AppendLine("  Detected code completion word, \"{0}\", most closely matches completion option \"{1}\".",
                                          codeCompletion.CompletionWord, codeCompletion.BestMatchToCompletionWord.CompletionText);
                    }
                    else
                    {
                        Logger.AppendLine("  Detected code completion word: {0}", codeCompletion.CompletionWord);
                    }
                }
            }


            if (fileResponse.ParseResults != null)
            {
                Logger.AppendLine("{0} error(s) detected.", fileResponse.ParseResults.Length);
                if (fileResponse.ParseResults.Length > 0)
                    foreach (var err in fileResponse.ParseResults)
                        Logger.AppendLine("{0}: In file {1} ({2}, {3}): {4}", err.Type, err.FileName, err.Line, err.Column, err.Message);
            }

        }
        public static FileOperationResponse DoAutoComplete(FileOperationRequest request)
        {
            FileOperationResponse response = new FileOperationResponse();
            ProjectModel projectModel = null;
            Stopwatch sw = Stopwatch.StartNew();

            try
            {
                var projectModelRepo = EndpointHost.AppHost.TryResolve<IProjectModelRepository>();

                projectModel = projectModelRepo.GetProject(request.UserId, request.ProjectId);

                var analysisRequest = new ProjectAnalysisRequest()
                {
                    ProjectModel = projectModel,
                    CodeCompletionParameters = new ProjectAnalysisCodeCompletionParameters()
                    {
                        FileId = request.FileId,
                        Line = request.CompleteCode.LineNumber,
                        Column = request.CompleteCode.ColumnNumber,
                        Offset = request.CompleteCode.Offset,
                        CtrlSpace = request.CompleteCode.CtrlSpace
                    }
                };


                // Analyze!
                ProjectAnalysisResult analysisResult = NRefactoryUtils.RunFullProjectAnalysis(analysisRequest);


                // Convert analysis result model to file operation response DTO
                if (analysisResult.CompletionOptions != null)
                {
                    var codeCompletion = response.CodeCompletion = new FileCodeCompletionResponse();
                    codeCompletion.CompletionOptions = analysisResult.CompletionOptions
                        .Select(CodeCompletionResultUtility.FromICompletionDataToFileCodeCompletionResult).ToArray();
                    for (int i = 0, len = codeCompletion.CompletionOptions.Length; i < len; i++)
                        codeCompletion.CompletionOptions[i].Id = i;
                    codeCompletion.CompletionWord = analysisResult.CompletionWord;
                    if (analysisResult.BestMatchToCompletionWord != null)
                        codeCompletion.BestMatchToCompletionWord = codeCompletion.CompletionOptions.FirstOrDefault(x => x.CompletionText == analysisResult.BestMatchToCompletionWord.CompletionText);
                    // Record text around cursor
                    codeCompletion.TextBeforeCursor = analysisResult.CompletionContextBefore;
                    codeCompletion.TextAfterCursor = analysisResult.CompletionContextAfter;
                    // Record inputs
                    codeCompletion.Line = analysisResult.Line;
                    codeCompletion.Column = analysisResult.Column;
                    codeCompletion.Offset = analysisResult.Offset;
                }
                var allErrors = new List<FileParseResult>();
                foreach (var fileModel in analysisRequest.ProjectModel.GetFileDescendants())
                {
                    allErrors.AddRange(fileModel.Parser.ErrorsAndWarnings
                                           .Select(x => new FileParseResult()
                                           {
                                               FileId = fileModel.Id,
                                               FileName = fileModel.Name,
                                               Line = x.Region.BeginLine,
                                               Column = x.Region.BeginColumn,
                                               Type = x.ErrorType,
                                               Message = x.Message
                                           }).ToArray());
                }
                response.ParseResults = allErrors.ToArray();


                /*
                StatelessProjectResponse response = new StatelessProjectResponse();
                if (analysisResult.CompletionOptions != null)
                {
                    response.CompletionOptions = analysisResult.CompletionOptions
                        .Select(x => new FileCodeCompletionResult()
                        {
                            CompletionText = x.CompletionText,
                            DisplayText = x.DisplayText,
                            Description = x.Description,
                            OverloadCount = x.OverloadedData.Count(),
                            CompletionCategoryDisplayText = (x.CompletionCategory == null ? "" : x.CompletionCategory.DisplayText)
                        }).ToArray();
                    response.CompletionWord = analysisResult.CompletionWord;
                    if (analysisResult.BestMatchToCompletionWord != null)
                        response.BestMatchToCompletionWord = response.CompletionOptions.FirstOrDefault(x => x.CompletionText == analysisResult.BestMatchToCompletionWord.CompletionText);
                }
                var allErrors = new List<FileParseResult>();
                foreach (var fileModel in analysisRequest.ProjectModel.GetFileDescendants())
                {
                    allErrors.AddRange(fileModel.Parser.ErrorsAndWarnings
                                           .Select(x => new FileParseResult()
                                           {
                                               FileId = fileModel.Id,
                                               FileName = fileModel.Name,
                                               Line = x.Region.BeginLine,
                                               Column = x.Region.BeginColumn,
                                               Type = x.ErrorType,
                                               Message = x.Message
                                           }).ToArray());
                }
                response.Errors = allErrors.ToArray();
                response.MsElapsed = analysisResult.TimeElapsed.TotalMilliseconds; // string.Format("{0} ms", analysisResult.TimeElapsed.TotalMilliseconds);
                */
                response.Status.Success = true;
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString(), ex);
                response.Status.SetError(ex);
            }
            finally
            {
                //response.CodeCompletion.MsElapsed = analysisResult.TimeElapsed.TotalMilliseconds; // string.Format("{0} ms", analysisResult.TimeElapsed.TotalMilliseconds);
                response.Status.MsElapsed = sw.ElapsedMilliseconds;
            }

            return response;
        }
            public object Any(AutoCompleteRequest request)
            {
                try
                {
                    var sb = new StringBuilder("autocomplete args: ");
                    sb.AppendFormat(" [UserId:{0}]", request.UserId);
                    sb.AppendFormat(" [ProjectId:{0}]", request.ProjectId);
                    sb.AppendFormat(" [FileId:{0}]", request.FileId);
                    sb.AppendFormat(" [Line:{0}]", request.Line);
                    sb.AppendFormat(" [Column:{0}]", request.Column);
                    log.Info(sb.ToString());

                    var fileRequest = new FileOperationRequest()
                    {
                        UserId = request.UserId,
                        ProjectId = request.ProjectId,
                        FileId = request.FileId,
                        CompleteCode = new FileCodeCompletionRequest()
                        {
                            AutoComplete = true,
                            LineNumber = request.Line,
                            ColumnNumber = request.Column,
                            CtrlSpace = true  // always true for now
                        },
                        Parse = new FileParseRequest()
                    };

                    FileOperationResponse response = AutocompleteServiceUtil.DoAutoComplete(fileRequest);

                    return response;
                }
                catch (Exception ex)
                {
                    log.Error(ex.ToString(), ex);
                    
                    return new ApiError()
                    {
                        FullName = ex.GetType().FullName,
                        Message = ex.Message,
                        StackTrace = ex.StackTrace
                    };
                }
            }
        /// <summary>
        /// Needs to accept args in the following format:
        ///    {$iUserID} {$iProjectID} {$iFileID} {$iRow} {$iColumn}
        /// Example PHP command:
        ///    $sCommand = "mono /QuantConnect.Server.Autocomplete.worker/bin/QuantConnect.Server.Autocomplete.worker.exe {$iUserID} {$iProjectID} {$iFileID} {$iRow} {$iColumn}";
        /// 
        /// Output should be a JSON-formatted object in the following form:
        ///    { autocomplete: [ ... ] }
        /// 
        /// Input format:
        ///    UserId  ProjectId  SessionId  FileId  RowNumber  ColumnNumber
        /// Note:  RowNumber and ColumnNumber apply to the location within the file specified by FileId.
        /// Input Example:
        ///    478 125 MyArbitrarySessionIdThatCouldBeHelpfulForCachingSerializedProjectInfo 263 9 1
        ///  > Interpretation of above example:
        ///    User=Paul(478)  Project=paul-project(125)  SessionId  File=Main.cs(263)  Row=9  Column=1
        /// 
        /// An example url:
        ///    http://autocomplete.quantconnect.com/api.php?sAction=autocomplete&iUserID=478&iProjectID=125&iFileID=263&iRow=10&iColumn=123
        /// </summary>
        /// <param name="args"></param>
        static void CommandLineAutocompleteMain(string[] args)
        {
            var sb = new StringBuilder("QuantConnect.Server.Autocomplete.worker args: ");
            for (int i = 0; i < args.Length; i++)
                sb.AppendFormat("{0} ", args[i]);
            Log.Trace(sb.ToString());


            int userId = 0, projectId = 0, fileId = 0, line = 0, column = 0;
            string sessionId = "";
            if (args.Length < 6 ||
                !Int32.TryParse(args[0].Trim('\''), out userId) ||
                !Int32.TryParse(args[1].Trim('\''), out projectId) ||
                // session ID doesn't need processing
                !Int32.TryParse(args[3].Trim('\''), out fileId) ||
                !Int32.TryParse(args[4].Trim('\''), out line) ||
                !Int32.TryParse(args[5].Trim('\''), out column))
            {
                WriteResponse(NO_RESULTS_RESPONSE);
                return;
            }

            try
            {
                var request = new FileOperationRequest()
                {
                    UserId = userId,
                    ProjectId = projectId,
                    FileId = fileId,
                    CompleteCode = new FileCodeCompletionRequest()
                    {
                        AutoComplete = true,
                        LineNumber = line,
                        ColumnNumber = column,
                        CtrlSpace = true  // always true for now
                    },
                    Parse = new FileParseRequest()
                    //SyncContent = new FileSyncContentRequest()
                };

                FileOperationResponse response = AutocompleteServiceUtil.DoAutoComplete(request);

                string completionJson = JsonConvert.SerializeObject(response);
                //string jsonResponse = string.Format("{{ \"autocomplete\": {0} }}", completionJson);
                WriteResponse(completionJson);
            }
            catch (Exception ex)
            {
                WriteResponse(ex.ToString());
                Log.Error(ex.ToString());
            }
        }