using Aaru.CommonTypes.Enums;
using Aaru.Images;
using Aaru.Localization;

namespace Aaru.Core.Image;

public partial class Convert
{
    /// <summary>
    ///     Creates output image file with specified parameters
    ///     Calls the output format plugin's Create() method with sector count and format options
    ///     Shows progress indicator during file creation
    /// </summary>
    /// <returns>Error code if creation fails</returns>
    private ErrorNumber CreateOutputImage()
    {
        if(_aborted) return ErrorNumber.NoError;

        InitProgress?.Invoke();
        PulseProgress?.Invoke(UI.Invoke_Opening_image_file);

        bool created = _outputImage.Create(_outputPath,
                                           _mediaType,
                                           _parsedOptions,
                                           _inputImage.Info.Sectors,
                                           _negativeSectors,
                                           _overflowSectors,
                                           _inputImage.Info.SectorSize);

        if(_outputImage is AaruFormat aif && _errorRecovery > 0) aif.SetErasureCodingAuto((byte)_errorRecovery);

        EndProgress?.Invoke();

        if(created) return ErrorNumber.NoError;

        StoppingErrorMessage?.Invoke(string.Format(UI.Error_0_creating_output_image, _outputImage.ErrorMessage));

        return ErrorNumber.CannotCreateFormat;
    }
}