using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using CarManagerV3.Classes; using CarManagerV3.Manager; namespace CarManagerV3 { // Could be made non-static / non-singleton if multiple collections are needed in the future. Not likely though. /// /// The StateManager class is responsible for managing the state of the car collection, including adding, removing, updating, and retrieving cars. /// Fully static / singleton at the moment, as only one collection is needed. /// internal class StateManager { // Initialize global static list of cars static List cars = new List(); // Initialize default file path for car data. static string filePath = ""; static bool hasConfirmedMigration = false; /// /// Gets a car by its identifier. /// /// The identifier. /// /// A object if found; otherwise, null. /// public static Car GetCarById(string id) { cars = SafeManager.ReadCars(filePath); return cars.FirstOrDefault(c => c.Id == id); } /// /// Public getter and setter for the cars list. Used to have better control over the list. /// /// /// The cars. /// public static List Cars { get { return cars; } set { cars = value; } } /// /// Adds a car to the collection. /// /// The to add. public static void AddCar(Car car) { cars = SafeManager.ReadCars(filePath); cars.Add(car); SafeManager.SaveCars(filePath, cars); } /// /// Removes a car from the collection. /// /// The to remove. public static void RemoveCar(Car car) { cars = SafeManager.ReadCars(filePath); Car existingCar = GetCarById(car.Id); if (existingCar == null) return; cars.Remove(existingCar); SafeManager.SaveCars(filePath, cars); } /// /// Updates a car in the collection. Identifies itself by its Id. /// /// /// If the car's Id has changed during editing, this will not work correctly. Keep Id immutable! /// /// The to update. public static void UpdateCar(Car car) { Car existingCar = GetCarById(car.Id); if (existingCar != null) { int index = cars.IndexOf(existingCar); cars[index] = car; Console.WriteLine("Updated car: " + existingCar.Id); SafeManager.SaveCars(filePath, cars); } } /// /// Creates a new car and adds it to the collection. /// /// The make. /// The model. /// The year. /// The color. /// The mileage. /// The price. /// /// The newly created object. /// public static Car CreateCar(string make, string model, int year, string color, int mileage, decimal price) { cars = SafeManager.ReadCars(filePath); int newOrder = cars.Count > 0 ? cars.Max(c => c.Order) + 1 : 1; Car newCar = new Car("", make, model, year, color, mileage, price, newOrder); AddCar(newCar); return newCar; } /// /// Sets the file path used for saving and loading car data. /// Called when user selects a new file path. /// /// The path. public static void setFilePath(string path) { // Reset migration confirmation when changing file path, as the new file may also require migration. hasConfirmedMigration = false; filePath = path; } /// /// Normalizes the orders of the cars in the collection to be sequential starting from 1, while keeping the relative order the same. /// /// The list of s. /// A normalized List of s public static List normalizeOrders(List cars) { // Normalize the Order field of all cars to be sequential starting from 1, while keeping the relative order the same. var orderedCars = cars.OrderBy(c => c.Order).ToList(); for (int i = 0; i < orderedCars.Count; i++) { orderedCars[i].Order = i + 1; } return orderedCars; } /// /// Prompts the user to confirm migration if they haven't already confirmed it for the current session. /// This is to prevent multiple annoying popups if the user tries to open multiple files that require migration. /// /// True if the user has accepted to migrate the file, otherwise False public static bool askForMigration(FileMeta meta = null) { if (hasConfirmedMigration) { return true; } string promptMessage = "The file you are trying to open is in an older format. Do you want to attempt to migrate it to the new format? If you choose not to migrate, the file will not be opened."; if(meta != null) { int nUpdatesNeeded = FileUpdate.GetRequiredUpdates(meta.Version).Count; promptMessage = $"The file you are trying to open is in an older format ({meta.Version}). It requires {nUpdatesNeeded} update(s) to be migrated to your Version ({Updater.GetCurrentVersion(true)}).\nDo you want to attempt to migrate it? If you choose not to migrate, the file will not be opened."; } DialogResult result = MessageBox.Show(promptMessage, "Migration Needed", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); hasConfirmedMigration = result == DialogResult.Yes; return hasConfirmedMigration; } } }