<commit_before>﻿using ConsoleScraper.Enums;
using ConsoleScraper.Models;
using System;
using System.Collections.Concurrent;
using System.Configuration;
using System.IO;

namespace ConsoleScraper
{
	public interface IJsonLogger
	{
		/// <summary>
		/// Creates a JSON file in the local wiki using info
		/// scraped from the wiki entry
		/// </summary>
		/// <param name="localFileChanges">The dictionary that holds all of the local changes</param>
		/// <param name="vehicleName">Name of the vehicle the page is for</param>
		/// <param name="fileName">What the file should be called</param>
		/// <param name="filePath">Where the file should be stored</param>
		void CreateJsonFile(ConcurrentDictionary<string, string> localFileChanges, string vehicleName, IVehicle vehicle, string fileName, string filePath);
	}

	public class JsonLogger : IJsonLogger
	{
		IFilePerVehicleLogger _filePerVehicleLogger;
		IConsoleManager _consoleManager;

		public JsonLogger(IFilePerVehicleLogger filePerVehicleLogger, IConsoleManager consoleManager)
		{
			_filePerVehicleLogger = filePerVehicleLogger;
			_consoleManager = consoleManager;
		}

		public void CreateJsonFile(ConcurrentDictionary<string, string> localFileChanges, string vehicleName, IVehicle vehicle, string fileName, string filePath)
		{
			LocalWikiFileTypeEnum fileType = LocalWikiFileTypeEnum.HTML;
			string fileExtension = fileType.ToString();

			GroundVehicle groundVehicle = (GroundVehicle)vehicle;
			string vehicleJson = Newtonsoft.Json.JsonConvert.SerializeObject(groundVehicle, Newtonsoft.Json.Formatting.Indented);

			if (!File.Exists(filePath))
			{
				// Add new item
				File.WriteAllText(filePath, vehicleJson);
				_filePerVehicleLogger.RecordAddFileToLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
			}
			else
			{
				string existingFileText = File.ReadAllText(filePath);
				GroundVehicle existingVehicle = Newtonsoft.Json.JsonConvert.DeserializeObject<GroundVehicle>(existingFileText);

				// Get out the last modified times for comparison
				string newLastModSection = groundVehicle.LastModified;
				string oldLastModSection = existingVehicle?.LastModified;

				// If both files have a last modified time
				if (newLastModSection != null && oldLastModSection != null)
				{
					if (!_filePerVehicleLogger.AreLastModifiedTimesTheSame(oldLastModSection, newLastModSection))
					{
						// Update existing
						File.WriteAllText(filePath, vehicleJson);
						_filePerVehicleLogger.RecordUpdateFileInLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
					}<commit_msg>Fixing copy-pasta error with filetype<commit_after>﻿using ConsoleScraper.Enums;
using ConsoleScraper.Models;
using System;
using System.Collections.Concurrent;
using System.Configuration;
using System.IO;

namespace ConsoleScraper
{
	public interface IJsonLogger
	{
		/// <summary>
		/// Creates a JSON file in the local wiki using info
		/// scraped from the wiki entry
		/// </summary>
		/// <param name="localFileChanges">The dictionary that holds all of the local changes</param>
		/// <param name="vehicleName">Name of the vehicle the page is for</param>
		/// <param name="fileName">What the file should be called</param>
		/// <param name="filePath">Where the file should be stored</param>
		void CreateJsonFile(ConcurrentDictionary<string, string> localFileChanges, string vehicleName, IVehicle vehicle, string fileName, string filePath);
	}

	public class JsonLogger : IJsonLogger
	{
		IFilePerVehicleLogger _filePerVehicleLogger;
		IConsoleManager _consoleManager;

		public JsonLogger(IFilePerVehicleLogger filePerVehicleLogger, IConsoleManager consoleManager)
		{
			_filePerVehicleLogger = filePerVehicleLogger;
			_consoleManager = consoleManager;
		}

		public void CreateJsonFile(ConcurrentDictionary<string, string> localFileChanges, string vehicleName, IVehicle vehicle, string fileName, string filePath)
		{
			LocalWikiFileTypeEnum fileType = LocalWikiFileTypeEnum.JSON;
			string fileExtension = fileType.ToString();

			GroundVehicle groundVehicle = (GroundVehicle)vehicle;
			string vehicleJson = Newtonsoft.Json.JsonConvert.SerializeObject(groundVehicle, Newtonsoft.Json.Formatting.Indented);

			if (!File.Exists(filePath))
			{
				// Add new item
				File.WriteAllText(filePath, vehicleJson);
				_filePerVehicleLogger.RecordAddFileToLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
			}
			else
			{
				string existingFileText = File.ReadAllText(filePath);
				GroundVehicle existingVehicle = Newtonsoft.Json.JsonConvert.DeserializeObject<GroundVehicle>(existingFileText);

				// Get out the last modified times for comparison
				string newLastModSection = groundVehicle.LastModified;
				string oldLastModSection = existingVehicle?.LastModified;

				// If both files have a last modified time
				if (newLastModSection != null && oldLastModSection != null)
				{
					if (!_filePerVehicleLogger.AreLastModifiedTimesTheSame(oldLastModSection, newLastModSection))
					{
						// Update existing
						File.WriteAllText(filePath, vehicleJson);
						_filePerVehicleLogger.RecordUpdateFileInLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
					}
{'fraction_non_alphanumeric': 0.08326923076923078, 'fraction_numerical': 0.0, 'mean_word_length': 4.950800915331808, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '4322442', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1470, 'n_words': 360}