2024-10-06 12:32:33 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
2024-10-08 09:53:24 +03:00
|
|
|
|
using System.Security.AccessControl;
|
2024-10-06 12:32:33 +03:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
2024-10-08 09:53:24 +03:00
|
|
|
|
using System.Xml.Linq;
|
2024-10-06 14:05:10 +03:00
|
|
|
|
using GtaVUsersInfo.Helpers;
|
2024-10-08 09:53:24 +03:00
|
|
|
|
using GtaVUsersInfo.Controls;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.IO;
|
2024-10-08 11:44:50 +03:00
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using GtaVUsersInfo.Forms;
|
2024-10-06 12:32:33 +03:00
|
|
|
|
|
|
|
|
|
namespace GtaVUsersInfo
|
|
|
|
|
{
|
|
|
|
|
public partial class MainForm : Form
|
|
|
|
|
{
|
2024-10-08 09:53:24 +03:00
|
|
|
|
private List<Control> debugControlsList;
|
2024-10-08 14:41:32 +03:00
|
|
|
|
|
2024-10-08 09:53:24 +03:00
|
|
|
|
private List<Car> carList;
|
2024-10-08 14:41:32 +03:00
|
|
|
|
|
2024-10-08 11:44:50 +03:00
|
|
|
|
private List<Control> parameters;
|
2024-10-08 14:41:32 +03:00
|
|
|
|
private List<CarItem> cars;
|
2024-10-08 09:53:24 +03:00
|
|
|
|
|
2024-10-06 12:32:33 +03:00
|
|
|
|
public MainForm()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-10-08 09:53:24 +03:00
|
|
|
|
|
|
|
|
|
debugControlsList = new List<Control> { parserButton, readJsonButton };
|
|
|
|
|
|
|
|
|
|
this.visible(debugControlsList, false);
|
|
|
|
|
|
2024-10-08 11:44:50 +03:00
|
|
|
|
carList = LoadCarsFromJson(Resources.carJsonPath);
|
2024-10-08 14:41:32 +03:00
|
|
|
|
|
|
|
|
|
parameters = new List<Control> { new ParameterControl("Имя"), new ParameterControl("Деньги") };
|
|
|
|
|
cars = new List<CarItem>();
|
|
|
|
|
|
|
|
|
|
carsItems.ContextMenuStrip = new CarPanelMenuStrip(addNewCar).Menu;
|
2024-10-08 11:44:50 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MainForm_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-10-08 14:41:32 +03:00
|
|
|
|
AllPanelCreate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AllPanelCreate()
|
|
|
|
|
{
|
|
|
|
|
ParameterPanelCreate();
|
|
|
|
|
CarPanelCreate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ParameterPanelCreate()
|
|
|
|
|
{
|
|
|
|
|
parametersPanel.Controls.Clear();
|
|
|
|
|
|
2024-10-08 11:44:50 +03:00
|
|
|
|
foreach (Control param in parameters)
|
|
|
|
|
{
|
|
|
|
|
param.Dock = DockStyle.Top;
|
|
|
|
|
parametersPanel.Controls.Add(param);
|
2024-10-08 14:41:32 +03:00
|
|
|
|
parametersPanel.Controls.SetChildIndex(param, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CarPanelCreate()
|
|
|
|
|
{
|
|
|
|
|
carsItems.Controls.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (CarItem car in cars)
|
|
|
|
|
{
|
|
|
|
|
carsItems.Controls.Add(car);
|
2024-10-08 11:44:50 +03:00
|
|
|
|
}
|
2024-10-08 09:53:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>();
|
|
|
|
|
}
|
2024-10-06 12:32:33 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 09:53:24 +03:00
|
|
|
|
#region Methods
|
|
|
|
|
|
|
|
|
|
private void visible(List<Control> controls, bool isVisible)
|
|
|
|
|
{
|
|
|
|
|
controls.ForEach(control => { control.Visible = isVisible; control.Enabled = isVisible; });
|
2024-10-06 14:05:10 +03:00
|
|
|
|
}
|
2024-10-06 12:32:33 +03:00
|
|
|
|
|
2024-10-08 09:53:24 +03:00
|
|
|
|
#endregion
|
|
|
|
|
|
2024-10-06 14:05:10 +03:00
|
|
|
|
async private void parserButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
using (var parser = new CarParser())
|
|
|
|
|
{
|
|
|
|
|
var cars = await parser.ParseCarsFromPages(25);
|
2024-10-08 11:44:50 +03:00
|
|
|
|
parser.SaveToJson(cars, Resources.carJsonPath);
|
|
|
|
|
ErrorsShow.ShowInfoMessage($"Данные сохранены: {Resources.carJsonPath}");
|
2024-10-08 09:53:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 11:44:50 +03:00
|
|
|
|
carList = LoadCarsFromJson(Resources.carJsonPath);
|
2024-10-08 09:53:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void readJsonButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-10-08 11:44:50 +03:00
|
|
|
|
carList = LoadCarsFromJson(Resources.carJsonPath);
|
2024-10-08 09:53:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void debugCheck_CheckedChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.visible(debugControlsList, debugCheck.Checked);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addCar_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (carList.Count <= 0)
|
|
|
|
|
{
|
2024-10-08 11:44:50 +03:00
|
|
|
|
ErrorsShow.ShowErrorMessage($"Список автомобилей пуст, ошибка чтения файла: {Resources.carJsonPath}");
|
2024-10-08 09:53:24 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Car car = new Car();
|
|
|
|
|
|
2024-10-08 11:44:50 +03:00
|
|
|
|
using (AddCars form = new AddCars(carList, car))
|
2024-10-08 09:53:24 +03:00
|
|
|
|
{
|
2024-10-08 11:44:50 +03:00
|
|
|
|
form.ShowDialog();
|
2024-10-08 09:53:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (car.Name != null)
|
|
|
|
|
{
|
|
|
|
|
CarItem item = new CarItem(car);
|
2024-10-08 14:41:32 +03:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
CarItem carItem = menu.Car as CarItem;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
CarItem carItem = menu.Car;
|
|
|
|
|
if (carItem == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (cars.Contains(carItem))
|
|
|
|
|
{
|
|
|
|
|
cars.Remove(carItem);
|
|
|
|
|
CarPanelCreate();
|
2024-10-06 14:05:10 +03:00
|
|
|
|
}
|
2024-10-06 12:32:33 +03:00
|
|
|
|
}
|
2024-10-08 11:44:50 +03:00
|
|
|
|
|
|
|
|
|
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);
|
2024-10-08 14:41:32 +03:00
|
|
|
|
ParameterPanelCreate();
|
2024-10-08 11:44:50 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-06 12:32:33 +03:00
|
|
|
|
}
|
|
|
|
|
}
|