using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Dvb.Subtitling.Model;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Skyscraper.Drawing
{
    static class PaletteExporter
    {
        public static void ExportPalette(string outputFileName, Color[] colors, bool includeAlpha, PaletteFormat fmt)
        {
            ExportPalette(new FileInfo(outputFileName), colors, includeAlpha, fmt);
        }
        public static void ExportPalette(FileInfo fi, Color[] colors, bool includeAlpha, PaletteFormat fmt)
        {
            if (fi.Exists)
                fi.Delete();
            FileStream fs = fi.OpenWrite();
            ExportPalette(fs, colors, includeAlpha, fmt);
            fs.Flush(true);
            fs.Close();
        }
        public static void ExportPalette(Stream output, Color[] colors, bool includeAlpha, PaletteFormat fmt)
        {
            for (int i = 0; i < colors.Length; i++)
            {
                switch (fmt)
                {
                    case PaletteFormat.Raw:
                        output.WriteByte(colors[i].Pb);
                        output.WriteByte(colors[i].Y);
                        output.WriteByte(colors[i].Pr);
                        if (includeAlpha)
                            output.WriteByte(colors[i].T);
                        break;
                    default:
                        throw new NotImplementedException(fmt.ToString());
                }
            }
        }
    }
}
