<commit_before>﻿#region

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using UlteriusServer.Api.Network.Messages;
using UlteriusServer.Api.Network.Models;
using UlteriusServer.Api.Services.LocalSystem;
using UlteriusServer.WebSocketAPI.Authentication;
using vtortola.WebSockets;

#endregion

namespace UlteriusServer.Api.Network.PacketHandlers
{
    public class CronJobPacketHandler : PacketHandler
    {
        private readonly CronJobService _cronJobService = UlteriusApiServer.CronJobService;
        private AuthClient _authClient;
        private MessageBuilder _builder;
        private WebSocket _client;
        private Packet _packet;


        public override void HandlePacket(Packet packet)
        {
            _client = packet.Client;
            _authClient = packet.AuthClient;
            _packet = packet;
            _builder = new MessageBuilder(_authClient, _client, _packet.EndPoint, _packet.SyncKey);
            switch (_packet.PacketType)
            {
                case PacketManager.PacketTypes.AddOrUpdateJob:
                    AddOrUpdateJob();
                    break;
                case PacketManager.PacketTypes.RemoveJob:
                    RemoveJob();
                    break;
                case PacketManager.PacketTypes.StopJobDaemon:
                    StopJobDaemon();
                    break;
                case PacketManager.PacketTypes.StartJobDaemon:
                    StartJobDaemon();
                    break;
                case PacketManager.PacketTypes.GetJobDaemonStatus:
                    GetJobDaemonStatus();
                    break;
                case PacketManager.PacketTypes.GetJobContents:
                    GetJobContents();
                    break;
                case PacketManager.PacketTypes.GetAllJobs:
                    GetAllJobs();
                    break;
            }
        }

        private void GetJobContents()
        {
            try
            {
                string contents = null;
                var exist = false;
                var existIndex = false;
                var localPath = string.Empty;
                var jobId = Guid.Parse(_packet.Args[0].ToString());
                if (_cronJobService.JobList.ContainsKey(jobId))
                {
                    existIndex = true;
                    var path = _cronJobService.JobList[jobId].Name;
                    localPath = path;
                    if (File.Exists(path))
                    {
                        exist = true;
                        contents = Convert.ToBase64String(File.ReadAllBytes(path));
                    } 
                }
                var response = new
                {
                    
                    Id = jobId.ToString(),
                    Base64ScriptContents = contents,
                    ScriptExist = exist,
                    IndexExist = existIndex,
                    LocalPath  = localPath
                };
                _builder.WriteMessage(response);
            }
            catch (Exception ex)
            {
                var exceptionResponse = new
                {

                    Message = ex.Message,
                    Exception = ex.StackTrace
                };
                _builder.WriteMessage(exceptionResponse);
            }
        }

        public void GetAllJobs()
        {
            _builder.WriteMessage(_cronJobService.JobList);
        }

        private void GetJobDaemonStatus()
        {
            var response = new
            {
                Online = _cronJobService.Status()
            };
            _builder.WriteMessage(response);
        }

        private void StopJobDaemon()
        {
            var response = new
            {
                ShutDown = true
            };
            _cronJobService.ShutDown();
            _builder.WriteMessage(response);
        }

        public void StartJobDaemon()
        {
            var response = new
            {
                Started = true
            };
            _cronJobService.Start();
            _builder.WriteMessage(response);
        }

        public void RemoveJob()
        {
            try
            {
                var removed = false;
                var jobId = Guid.Parse(_packet.Args[0].ToString());
                if (_cronJobService.JobList.ContainsKey(jobId))
                {
                    var oldPath = _cronJobService.JobList[jobId].Name;
                    removed = _cronJobService.RemoveJob(jobId);
                    if (removed)
                    {
                        if (File.Exists(oldPath))
                        {
                            File.Delete(oldPath);
                        }
                        _cronJobService.Save();
                    }
                }
                var response = new
                {
                    Id = jobId.ToString(),
                    JobRemoved = removed
                };
                _builder.WriteMessage(response);
            }
            catch (Exception ex)
            {
                var exceptionResponse = new
                {
                    Removed = false,
                    Message = ex.Message,
                    Exception = ex.StackTrace
                };
                _builder.WriteMessage(exceptionResponse);
            }
        }

        private void AddOrUpdateJob()
        {
            try
            {
                var argumentObject = (JObject) _packet.Args[0];
                var jobId = Guid.Parse(argumentObject["Guid"].ToString());
                var base64ScriptContents = argumentObject["Base64ScriptContents"].ToString();<commit_msg>Fix response for job remove<commit_after>﻿#region

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using UlteriusServer.Api.Network.Messages;
using UlteriusServer.Api.Network.Models;
using UlteriusServer.Api.Services.LocalSystem;
using UlteriusServer.WebSocketAPI.Authentication;
using vtortola.WebSockets;

#endregion

namespace UlteriusServer.Api.Network.PacketHandlers
{
    public class CronJobPacketHandler : PacketHandler
    {
        private readonly CronJobService _cronJobService = UlteriusApiServer.CronJobService;
        private AuthClient _authClient;
        private MessageBuilder _builder;
        private WebSocket _client;
        private Packet _packet;


        public override void HandlePacket(Packet packet)
        {
            _client = packet.Client;
            _authClient = packet.AuthClient;
            _packet = packet;
            _builder = new MessageBuilder(_authClient, _client, _packet.EndPoint, _packet.SyncKey);
            switch (_packet.PacketType)
            {
                case PacketManager.PacketTypes.AddOrUpdateJob:
                    AddOrUpdateJob();
                    break;
                case PacketManager.PacketTypes.RemoveJob:
                    RemoveJob();
                    break;
                case PacketManager.PacketTypes.StopJobDaemon:
                    StopJobDaemon();
                    break;
                case PacketManager.PacketTypes.StartJobDaemon:
                    StartJobDaemon();
                    break;
                case PacketManager.PacketTypes.GetJobDaemonStatus:
                    GetJobDaemonStatus();
                    break;
                case PacketManager.PacketTypes.GetJobContents:
                    GetJobContents();
                    break;
                case PacketManager.PacketTypes.GetAllJobs:
                    GetAllJobs();
                    break;
            }
        }

        private void GetJobContents()
        {
            try
            {
                string contents = null;
                var exist = false;
                var existIndex = false;
                var localPath = string.Empty;
                var jobId = Guid.Parse(_packet.Args[0].ToString());
                if (_cronJobService.JobList.ContainsKey(jobId))
                {
                    existIndex = true;
                    var path = _cronJobService.JobList[jobId].Name;
                    localPath = path;
                    if (File.Exists(path))
                    {
                        exist = true;
                        contents = Convert.ToBase64String(File.ReadAllBytes(path));
                    } 
                }
                var response = new
                {
                    
                    Id = jobId.ToString(),
                    Base64ScriptContents = contents,
                    ScriptExist = exist,
                    IndexExist = existIndex,
                    LocalPath  = localPath
                };
                _builder.WriteMessage(response);
            }
            catch (Exception ex)
            {
                var exceptionResponse = new
                {

                    Message = ex.Message,
                    Exception = ex.StackTrace
                };
                _builder.WriteMessage(exceptionResponse);
            }
        }

        public void GetAllJobs()
        {
            _builder.WriteMessage(_cronJobService.JobList);
        }

        private void GetJobDaemonStatus()
        {
            var response = new
            {
                Online = _cronJobService.Status()
            };
            _builder.WriteMessage(response);
        }

        private void StopJobDaemon()
        {
            var response = new
            {
                ShutDown = true
            };
            _cronJobService.ShutDown();
            _builder.WriteMessage(response);
        }

        public void StartJobDaemon()
        {
            var response = new
            {
                Started = true
            };
            _cronJobService.Start();
            _builder.WriteMessage(response);
        }


        //Evan crys a lot
        public void RemoveJob()
        {
            try
            {
                
                var removed = false;
                var scriptRemoved = false;
                var jobId = Guid.Parse(_packet.Args[0].ToString());
                var jobExist = _cronJobService.JobList.ContainsKey(jobId);
                if (jobExist)
                {
                    var oldPath = _cronJobService.JobList[jobId].Name;
                    removed = _cronJobService.RemoveJob(jobId);
                    if (removed)
                    {
                        if (File.Exists(oldPath))
                        {
                            File.Delete(oldPath);
                            scriptRemoved = true;
                        }
                        _cronJobService.Save();
                    }
                }
                var response = new
                {
                    Id = jobId.ToString(),
                    JobRemoved = removed,
                    ScriptRemoved = scriptRemoved,
                     JobExisted = jobExist
                };
                _builder.WriteMessage(response);
            }
            catch (Exception ex)
            {
                var exceptionResponse = new
                {
                    Removed = false,
                    Message = ex.Message,
                    Exception = ex.StackTrace
                };
                _builder.WriteMessage(exceptionResponse);
            }
        }

        private void AddOrUpdateJob()
        {
            try
            {
                var argumentObject = (JObject) _packet.Args[0];
                var jobId = Guid.Parse(argumentObject["Guid"].ToString());
                var base64ScriptContents = argumentObject["Base64ScriptContents"].ToString();
{'fraction_non_alphanumeric': 0.06902371107923382, 'fraction_numerical': 0.0018563834275588558, 'mean_word_length': 1.1259192825112108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4940325', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2603, 'n_words': 507}