// Copyright (c) 2024 Synty Studios Limited. All rights reserved.
//
// Use of this software is subject to the terms and conditions of the Synty Studios End User Licence Agreement (EULA)
// available at: https://syntystore.com/pages/end-user-licence-agreement
//
// For additional details, see the LICENSE.MD file bundled with this software.
using SQLite;
using System.Collections.Generic;
namespace Synty.SidekickCharacters.Database.DTO
{
[Table("sk_part_image")]
public class SidekickPartImage
{
private SidekickPart _part;
[PrimaryKey, AutoIncrement, Column("id")]
public int ID { get; set; }
[Column("ptr_part")]
public int PtrPart { get; set; }
[Column("img_data")]
public byte[] ImageData { get; set; }
[Column("img_width")]
public int Width { get; set; }
[Column("img_height")]
public int Height { get; set; }
[Ignore]
public SidekickPart Part
{
get => _part;
set
{
_part = value;
PtrPart = value.ID;
}
}
///
/// Gets a specific Part Image by its database ID.
///
/// The Database Manager to use.
/// The id of the required Part Image.
/// The specific Part Image if it exists; otherwise null.
public static SidekickPartImage GetByID(DatabaseManager dbManager, int id)
{
SidekickPartImage partImage = dbManager.GetCurrentDbConnection().Find(id);
Decorate(dbManager, partImage);
return partImage;
}
///
/// Gets a specific Part Image for a specific part.
///
/// The Database Manager to use.
/// The Part to get the image for.
/// The specific Part Image for a specific part if it exists; otherwise null.
public static SidekickPartImage GetByPart(DatabaseManager dbManager, SidekickPart part)
{
SidekickPartImage partImage = dbManager.GetCurrentDbConnection().Table()
.FirstOrDefault(pi => pi.PtrPart == part.ID);
Decorate(dbManager, partImage);
return partImage;
}
///
/// Gets a list of all the Part Images in the database.
///
/// The Database Manager to use.
/// A list of all part images in the database.
public static List GetAll(DatabaseManager dbManager)
{
List partImages = dbManager.GetCurrentDbConnection().Table().ToList();
foreach (SidekickPartImage partImage in partImages)
{
Decorate(dbManager, partImage);
}
return partImages;
}
///
/// Ensures that the given preset has its nice DTO class properties set
///
/// The Database Manager to use.
/// The part image to decorate
private static void Decorate(DatabaseManager dbManager, SidekickPartImage partImage)
{
if (partImage != null)
{
if (partImage.Part == null && partImage.PtrPart >= 0)
{
partImage.Part ??= SidekickPart.GetByID(dbManager, partImage.PtrPart);
}
}
}
///
/// Updates or Inserts this item in the Database.
///
/// The database manager to use.
public void Save(DatabaseManager dbManager)
{
if (ID <= 0)
{
dbManager.GetCurrentDbConnection().Insert(this);
// in theory this could return a different ID, but in practice it's highly unlikely
ID = (int) SQLite3.LastInsertRowid(dbManager.GetCurrentDbConnection().Handle);
}
dbManager.GetCurrentDbConnection().Update(this);
}
///
/// Deletes this item from the database
///
/// The database manager to use.
public void Delete(DatabaseManager dbManager)
{
dbManager.GetCurrentDbConnection().Delete(ID);
}
}
}