377 lines
12 KiB
C#
377 lines
12 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Security.AccessControl;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using System.Xml.Linq;
|
||
using GtaVUsersInfo.Helpers;
|
||
using GtaVUsersInfo.Controls;
|
||
using Newtonsoft.Json;
|
||
using System.IO;
|
||
using System.Data.Common;
|
||
using GtaVUsersInfo.Forms;
|
||
using System.Collections;
|
||
using System.Security.Cryptography;
|
||
|
||
namespace GtaVUsersInfo
|
||
{
|
||
public partial class MainForm : Form
|
||
{
|
||
private List<Control> debugControlsList;
|
||
|
||
private List<Car> carList;
|
||
|
||
private List<ParameterControl> parameters;
|
||
private List<CarControl> cars;
|
||
|
||
public MainForm()
|
||
{
|
||
InitializeComponent();
|
||
|
||
debugControlsList = new List<Control> { parserButton, readJsonButton };
|
||
|
||
this.visible(debugControlsList, false);
|
||
|
||
carList = LoadCarsFromJson(Resources.carJsonPath);
|
||
|
||
cars = new List<CarControl>();
|
||
parameters = new List<ParameterControl> { new ParameterControl("Имя"), new ParameterControl("Деньги") };
|
||
|
||
carsItems.ContextMenuStrip = new CarPanelMenuStrip(addNewCar).Menu;
|
||
parametersPanel.ContextMenuStrip = new ParameterPanelMenuStrip(addNewParameter).Menu;
|
||
}
|
||
|
||
private void MainForm_Load(object sender, EventArgs e)
|
||
{
|
||
AllPanelCreate();
|
||
}
|
||
|
||
private void AllPanelCreate()
|
||
{
|
||
ParameterPanelCreate();
|
||
CarPanelCreate();
|
||
}
|
||
|
||
private void ParameterPanelCreate()
|
||
{
|
||
parametersPanel.Controls.Clear();
|
||
|
||
foreach (Control param in parameters)
|
||
{
|
||
param.Dock = DockStyle.Top;
|
||
(param as ParameterControl).MenuStrip = new ParameterItemMenuStrip(addNewParameter, deleteParameter, param as ParameterControl).Menu;
|
||
parametersPanel.Controls.Add(param);
|
||
parametersPanel.Controls.SetChildIndex(param, 0);
|
||
}
|
||
}
|
||
|
||
private void CarPanelCreate()
|
||
{
|
||
carsItems.Controls.Clear();
|
||
|
||
foreach (CarControl car in cars)
|
||
{
|
||
carsItems.Controls.Add(car);
|
||
}
|
||
}
|
||
|
||
public static List<Car> LoadCarsFromJson(string filePath)
|
||
{
|
||
try
|
||
{
|
||
string json = File.ReadAllText(filePath);
|
||
List<Car> cars = JsonConvert.DeserializeObject<List<Car>>(json);
|
||
|
||
return cars;
|
||
}
|
||
catch (FileNotFoundException)
|
||
{
|
||
string message = $"Файл с автомобилями не найден: {filePath}";
|
||
Console.WriteLine(message);
|
||
MessageBox.Show(message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return new List<Car>();
|
||
}
|
||
catch (JsonException ex)
|
||
{
|
||
ErrorsShow.ShowErrorMessage($"Ошибка при десериализации JSON автомобилей: {ex.Message}");
|
||
return new List<Car>();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ErrorsShow.ShowErrorMessage($"Произошла неожиданная ошибка при открытии файла с автомобилями: {ex.Message}");
|
||
return new List<Car>();
|
||
}
|
||
}
|
||
|
||
#region Methods
|
||
|
||
private void visible(List<Control> controls, bool isVisible)
|
||
{
|
||
controls.ForEach(control => { control.Visible = isVisible; control.Enabled = isVisible; });
|
||
}
|
||
|
||
private void readJsonButton_Click(object sender, EventArgs e)
|
||
{
|
||
carList = LoadCarsFromJson(Resources.carJsonPath);
|
||
}
|
||
|
||
private void debugCheck_CheckedChanged(object sender, EventArgs e)
|
||
{
|
||
this.visible(debugControlsList, debugCheck.Checked);
|
||
}
|
||
|
||
#endregion
|
||
|
||
async private void parserButton_Click(object sender, EventArgs e)
|
||
{
|
||
using (var parser = new CarParser())
|
||
{
|
||
var cars = await parser.ParseCarsFromPages(25);
|
||
parser.SaveToJson(cars, Resources.carJsonPath);
|
||
ErrorsShow.ShowInfoMessage($"Данные сохранены: {Resources.carJsonPath}");
|
||
}
|
||
|
||
carList = LoadCarsFromJson(Resources.carJsonPath);
|
||
}
|
||
|
||
#region Методы автомобильной панели
|
||
|
||
private void addCar_Click(object sender, EventArgs e)
|
||
{
|
||
if (carList.Count <= 0)
|
||
{
|
||
ErrorsShow.ShowErrorMessage($"Список автомобилей пуст, ошибка чтения файла: {Resources.carJsonPath}");
|
||
return;
|
||
}
|
||
|
||
Car car = new Car();
|
||
|
||
using (AddCars form = new AddCars(carList, car))
|
||
{
|
||
form.ShowDialog();
|
||
}
|
||
|
||
if (car.Name != null)
|
||
{
|
||
CarControl item = new CarControl(car);
|
||
item.MenuStrip = new CarItemMenuStrip(addNewCar, editCar, deleteCar, item).Menu;
|
||
cars.Add(item);
|
||
CarPanelCreate();
|
||
}
|
||
}
|
||
|
||
private void addNewCar(object sender, EventArgs e)
|
||
{
|
||
addCar_Click(sender, e);
|
||
}
|
||
|
||
private void editCar(object sender, EventArgs e)
|
||
{
|
||
if (carList.Count <= 0)
|
||
{
|
||
ErrorsShow.ShowErrorMessage($"Список автомобилей пуст, ошибка чтения файла: {Resources.carJsonPath}");
|
||
return;
|
||
}
|
||
|
||
ToolStripMenuItem clickedMenuItem = sender as ToolStripMenuItem;
|
||
if (clickedMenuItem == null)
|
||
return;
|
||
|
||
CarItemMenuStrip menu = clickedMenuItem.Tag as CarItemMenuStrip;
|
||
if (menu == null)
|
||
return;
|
||
|
||
CarControl carItem = menu.Car;
|
||
if (carItem == null)
|
||
return;
|
||
|
||
Car car = new Car();
|
||
|
||
using (AddCars form = new AddCars(carList, car))
|
||
{
|
||
form.ShowDialog();
|
||
}
|
||
|
||
if (car.Name != null)
|
||
{
|
||
carItem.EditCar(car);
|
||
}
|
||
}
|
||
|
||
private void deleteCar(object sender, EventArgs e)
|
||
{
|
||
if (carList.Count <= 0)
|
||
{
|
||
ErrorsShow.ShowErrorMessage($"Список автомобилей пуст, ошибка чтения файла: {Resources.carJsonPath}");
|
||
return;
|
||
}
|
||
|
||
ToolStripMenuItem clickedMenuItem = sender as ToolStripMenuItem;
|
||
if (clickedMenuItem == null)
|
||
return;
|
||
|
||
CarItemMenuStrip menu = clickedMenuItem.Tag as CarItemMenuStrip;
|
||
if (menu == null)
|
||
return;
|
||
|
||
CarControl carItem = menu.Car;
|
||
if (carItem == null)
|
||
return;
|
||
|
||
if (cars.Contains(carItem))
|
||
{
|
||
if (
|
||
ErrorsShow.ShowQuestMessage(
|
||
$"Вы точно хотите удалить автомобиль: {carItem.Car.Name} - {carItem.Car.Price}?"
|
||
) == DialogResult.Yes)
|
||
{
|
||
cars.Remove(carItem);
|
||
CarPanelCreate();
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Методы панели параметров
|
||
|
||
private void addParameter_Click(object sender, EventArgs e)
|
||
{
|
||
ParameterControl parameter = new ParameterControl();
|
||
|
||
using (AddParameter form = new AddParameter(parameters, parameter))
|
||
{
|
||
form.ShowDialog();
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(parameter.ParamName))
|
||
{
|
||
parameter.Dock = DockStyle.Top;
|
||
parameters.Add(parameter);
|
||
ParameterPanelCreate();
|
||
}
|
||
}
|
||
|
||
private void addNewParameter(object sender, EventArgs e)
|
||
{
|
||
addParameter_Click(sender, e);
|
||
}
|
||
|
||
private void deleteParameter(object sender, EventArgs e)
|
||
{
|
||
if (parameters.Count <= 0)
|
||
{
|
||
ErrorsShow.ShowErrorMessage($"Список параметров пуст");
|
||
return;
|
||
}
|
||
|
||
ToolStripMenuItem clickedMenuItem = sender as ToolStripMenuItem;
|
||
if (clickedMenuItem == null)
|
||
return;
|
||
|
||
ParameterItemMenuStrip menu = clickedMenuItem.Tag as ParameterItemMenuStrip;
|
||
if (menu == null)
|
||
return;
|
||
|
||
ParameterControl parameterControl = menu.Parameter;
|
||
if (parameterControl == null)
|
||
return;
|
||
|
||
if (parameters.Contains(parameterControl))
|
||
{
|
||
if (
|
||
ErrorsShow.ShowQuestMessage(
|
||
$"Вы точно хотите удалить элемент: {parameterControl.ParamName}?"
|
||
) == DialogResult.Yes)
|
||
{
|
||
parameters.Remove(parameterControl);
|
||
ParameterPanelCreate();
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
SaveDataToFile();
|
||
AllPanelCreate();
|
||
}
|
||
|
||
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
Cars_Parameters lists = LoadDataFromFile();
|
||
if (lists != null)
|
||
{
|
||
cars = lists.cars.Select(car => new CarControl(car)).ToList();
|
||
parameters = lists.parameters.Select(parameter => new ParameterControl(parameter)).ToList();
|
||
}
|
||
|
||
AllPanelCreate();
|
||
}
|
||
|
||
public void SaveDataToFile()
|
||
{
|
||
Cars_Parameters data = new Cars_Parameters(Car.ConvertToCarList(cars), Parameter.ConvertToParameterList(parameters));
|
||
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
||
saveFileDialog.Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*";
|
||
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
// Сериализуем данные в JSON
|
||
string jsonData = JsonConvert.SerializeObject(data, Formatting.Indented);
|
||
|
||
// Записываем данные в файл
|
||
File.WriteAllText(saveFileDialog.FileName, jsonData);
|
||
}
|
||
}
|
||
|
||
public Cars_Parameters LoadDataFromFile()
|
||
{
|
||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||
openFileDialog.Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*";
|
||
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
// Читаем данные из файла
|
||
string jsonData = File.ReadAllText(openFileDialog.FileName);
|
||
|
||
// Десериализуем данные в объект ListData
|
||
Cars_Parameters data = JsonConvert.DeserializeObject<Cars_Parameters>(jsonData);
|
||
return data;
|
||
}
|
||
return null; // Если пользователь отменил выбор файла
|
||
}
|
||
|
||
private void newToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (ErrorsShow.ShowQuestMessage("Хотите сохранить текущего персонажа?") == DialogResult.Yes)
|
||
{
|
||
SaveDataToFile();
|
||
}
|
||
|
||
cars = new List<CarControl>();
|
||
parameters = new List<ParameterControl> { new ParameterControl("Имя"), new ParameterControl("Деньги") };
|
||
AllPanelCreate();
|
||
}
|
||
|
||
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
this.Close();
|
||
}
|
||
|
||
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
if (ErrorsShow.ShowQuestMessage("Хотите сохранить текущего персонажа?") == DialogResult.Yes)
|
||
{
|
||
SaveDataToFile();
|
||
}
|
||
}
|
||
}
|
||
}
|