Files
LeanderShiftPlannerV2/LeanderShiftPlannerV2/FileIO/ShiftPlanIO.cs
2025-11-16 04:34:34 +01:00

177 lines
6.9 KiB
C#

using System.Collections.Generic;
using LeanderShiftPlannerV2.Model;
using SkiaSharp;
using System.IO;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using LeanderShiftPlannerV2.Util;
namespace LeanderShiftPlannerV2.FileIO
{
public static class ShiftPlanIO
{
private static void CheckFileSystem()
{
if (!Directory.Exists(Constants.DataPath)) Directory.CreateDirectory(Constants.DataPath);
if (!Directory.Exists(Constants.ExportPath)) Directory.CreateDirectory(Constants.ExportPath);
if (!Directory.Exists(Constants.ShiftPlanPath)) Directory.CreateDirectory(Constants.ShiftPlanPath);
}
public static SKImage GenerateShiftPlanPng(ShiftPlan shiftPlan, string font)
{
Dictionary<(int, int), List<string>> names = GetNamesDict(shiftPlan);
const int rows = 4;
const int columns = 6;
const int cellWidth = 150;
const int cellHeight = 200;
using (SKBitmap bitmap = new SKBitmap(1000, 850))
using (SKCanvas canvas = new SKCanvas(bitmap))
{
SKFont smallTextFont = new SKFont(SKTypeface.FromFile($"Resources/font/{font}")) { Size = 24 };
SKPaint rectColorGray = new SKPaint
{ Color = SKColors.LightGray, Style = SKPaintStyle.Stroke, StrokeWidth = 5 };
SKPaint textColorGray = new SKPaint { Color = SKColors.Gray };
SKPaint textColorBlack = new SKPaint { Color = SKColors.Black };
// Fill Background
canvas.Clear(SKColors.White);
// Draw Topic
using (SKFont topic = new SKFont(SKTypeface.FromFile($"Resources/font/{font}")))
{
topic.Size = 40;
string text1 = "Schichtplan ";
string text2 = "IT-Service";
float text1Width = topic.MeasureText(text1);
float textX = 50;
float textY = 100;
canvas.DrawText(text1, textX, textY, topic, textColorBlack);
canvas.DrawText(text2, textX + text1Width, textY, topic, textColorGray);
}
// Draw Headers
string[] headers = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"];
for (int j = 1; j < columns; j++)
{
string text = headers[j - 1];
float textWidth = smallTextFont.MeasureText(text);
float textX = j * cellWidth + (cellWidth - textWidth) / 2;
float textY = (cellHeight - smallTextFont.Size);
canvas.DrawText(text, textX, textY, smallTextFont, textColorBlack);
}
// Draw Start-times
string[] time = [" ", "Ab 9 Uhr", "Ab 14 Uhr", "Ab 16 Uhr"];
for (int i = 1; i < rows; i++)
{
string text = time[i];
float textWidth = smallTextFont.MeasureText(text);
float textX = (cellWidth - textWidth) / 2;
float textY = i * cellHeight + (cellHeight + smallTextFont.Size) / 2;
canvas.DrawText(text, textX, textY, smallTextFont, textColorBlack);
}
// Draw ShiftPlan
for (int i = 1; i < rows; i++)
{
for (int j = 1; j < columns; j++)
{
int x = j * cellWidth;
int y = i * cellHeight;
// Draw Rectangles
canvas.DrawRect(new SKRect(x, y, x + cellWidth, y + cellHeight), rectColorGray);
// Draw Text
int k = 0;
foreach (string name in names[(i, j)])
{
k++;
string text = name;
float textWidth = smallTextFont.MeasureText(text);
float textX = x + (cellWidth - textWidth) / 2;
float textY = y + 100 - (names[(i, j)].Count * smallTextFont.Size / 2) +
k * smallTextFont.Size;
//float textY = y + k * textColor.TextSize;
canvas.DrawText(text, textX, textY, smallTextFont, textColorBlack);
}
}
}
SKImage image = SKImage.FromBitmap(bitmap);
return image;
}
}
public static void ExportShiftPlan(SKImage image, int id, string font)
{
CheckFileSystem();
using SKData data = image.Encode(SKEncodedImageFormat.Png, 100);
if (!Directory.Exists(Constants.ShiftPlanPath + $"/{font}/")) Directory.CreateDirectory(Constants.ShiftPlanPath + $"/{font}/");
using FileStream dataStream = File.OpenWrite(Constants.ShiftPlanPath + $"/{font}/" + $"ShiftPlan_{id}.png");
data.SaveTo(dataStream);
}
public static IImage ConvertToIImage(SKImage image)
{
SKBitmap skBitmap = SKBitmap.FromImage(image);
// Convert SKBitmap to a byte array
MemoryStream ms = new MemoryStream();
skBitmap.Encode(ms, SKEncodedImageFormat.Png, 100);
ms.Seek(0, SeekOrigin.Begin);
// Create a Bitmap from the byte array
Bitmap bitmap = new Bitmap(ms);
// Convert Bitmap to IImage
return bitmap;
}
private static Dictionary<(int, int), List<string>> GetNamesDict(ShiftPlan shiftPlan)
{
Dictionary<(int, int), List<string>> result = new Dictionary<(int, int), List<string>>();
int i = 0, j = 1;
foreach ((Shift, Shift, Shift) shift in shiftPlan.Shifts)
{
i++;
result[(j, i)] = new List<string>();
if (shift.Item1 is Shift)
{
foreach (Person person in shift.Item1.Persons)
{
result[(j, i)].Add(person.FirstName);
}
}
j++;
result[(j, i)] = new List<string>();
if (shift.Item2 is Shift)
{
foreach (Person person in shift.Item2.Persons)
{
result[(j, i)].Add(person.FirstName);
}
}
j++;
result[(j, i)] = new List<string>();
if (shift.Item3 is Shift)
{
foreach (Person person in shift.Item3.Persons)
{
result[(j, i)].Add(person.FirstName);
}
}
j = 1;
}
return result;
}
}
}