using System.IO;
using System.IO.Compression;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using AmorousModStudio.Core;
class Program {
 [STAThread] static void Main() {
  string root=Path.Combine(AppContext.BaseDirectory,"Новые текстуры с прозрачностью");Directory.CreateDirectory(root);
  string input=Path.Combine(root,"Прозрачная новая.png"),output=Path.Combine(root,"Новая текстура.xnb");
  byte[] pixels=new byte[29*17*4];for(int i=0;i<pixels.Length;i+=4){pixels[i]=170;pixels[i+1]=80;pixels[i+2]=220;pixels[i+3]=(byte)(new[]{0,64,128,255}[(i/4)%4]);}
  WritePng(input,29,17,pixels);TextureService.Create(input,output);
  if(File.ReadAllBytes(output)[0]!=0x1f)throw new Exception("New texture outergzip missing");
  string exported=TextureService.Export(output,Path.Combine(root,"PNG roundtrip"));
  byte[] expected=Read(input),actual=Read(exported);if(expected.Length!=actual.Length||expected.Zip(actual).Any(p=>Math.Abs(p.First-p.Second)>1))throw new Exception("Pixel mismatch");
  string large=Path.Combine(root,"too-wide.png");WritePng(large,8193,1,new byte[8193*4]);bool rejected=false;try{TextureService.Create(large,Path.Combine(root,"invalid.xnb"));}catch(InvalidDataException){rejected=true;}if(!rejected||File.Exists(Path.Combine(root,"invalid.xnb")))throw new Exception("Oversize accepted");
  Console.WriteLine("PASS Create actual29x17 ColorXNB: semitransparentpixels+gzip+Unicode/spaces+reexport verification;8193px rejected");
 }
 static void WritePng(string path,int w,int h,byte[] pixels){var frame=BitmapSource.Create(w,h,96,96,PixelFormats.Bgra32,null,pixels,w*4);var png=new PngBitmapEncoder();png.Frames.Add(BitmapFrame.Create(frame));using var f=File.Create(path);png.Save(f);}
 static byte[] Read(string path){using var f=File.OpenRead(path);var d=BitmapDecoder.Create(f,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.OnLoad);var frame=new FormatConvertedBitmap(d.Frames[0],PixelFormats.Pbgra32,null,0);var data=new byte[frame.PixelWidth*frame.PixelHeight*4];frame.CopyPixels(data,frame.PixelWidth*4,0);return data;}
}
