using System.IO;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace AmorousModStudio;
public class MainWindow:Window {}
public partial class App:Application {
 protected override void OnStartup(StartupEventArgs e) {
  base.OnStartup(e);ShutdownMode=ShutdownMode.OnExplicitShutdown;try{Run(e.Args[0]);Shutdown(0);}catch(Exception ex){Directory.CreateDirectory(e.Args[0]);File.WriteAllText(Path.Combine(e.Args[0],"error.txt"),ex.ToString());Shutdown(1);}
 }
 static IEnumerable<T> Descendants<T>(DependencyObject node) where T:DependencyObject {if(node is T match)yield return match;for(int i=0;i<VisualTreeHelper.GetChildrenCount(node);i++)foreach(var child in Descendants<T>(VisualTreeHelper.GetChild(node,i)))yield return child;}
 static void Layout(FrameworkElement root,int w,int h){root.Width=w;root.Height=h;root.Measure(new Size(w,h));root.Arrange(new Rect(0,0,w,h));root.UpdateLayout();Dispatcher.CurrentDispatcher.Invoke(()=>{},DispatcherPriority.Render);}
 static double Render(FrameworkElement root,string path,int w,int h){Layout(root,w,h);var bitmap=new RenderTargetBitmap(w,h,96,96,PixelFormats.Pbgra32);bitmap.Render(root);var png=new PngBitmapEncoder();png.Frames.Add(BitmapFrame.Create(bitmap));using(var file=File.Create(path))png.Save(file);byte[] pixels=new byte[w*h*4];bitmap.CopyPixels(pixels,w*4,0);int bright=0;for(int i=0;i<pixels.Length;i+=4)if(pixels[i]>230&&pixels[i+1]>230&&pixels[i+2]>230&&pixels[i+3]>240)bright++;return(double)bright/(w*h);}
 void Run(string output){
  Directory.CreateDirectory(output);var startup=new MainWindow{Style=(Style)FindResource(typeof(MainWindow))};startup.ApplyTemplate();if(startup.Background is not SolidColorBrush brush||brush.Color!=Color.FromRgb(16,18,28))throw new Exception("Root window startup background is not dark");startup.Close();var root=new StackPanel{Background=(Brush)FindResource("CanvasBrush"),Margin=new Thickness(0)};
  root.Children.Add(new TextBlock{Text="AMS DARK THEME — disabled controls",Margin=new Thickness(12),FontSize=20});
  var list=new ListBox{Height=155,Margin=new Thickness(12),IsEnabled=false};for(int i=0;i<30;i++)list.Items.Add("Asset / Method #"+i);list.SelectedIndex=1;root.Children.Add(list);
  var text=new TextBox{Text="Disabled text stays dark and readable",Margin=new Thickness(12),IsEnabled=false};root.Children.Add(text);
  var tabs=new TabControl{Height=180,Margin=new Thickness(12),IsEnabled=false};tabs.Items.Add(new TabItem{Header="Disabled tab",Content=new TextBox{Text="Content / JSON\nSecond line",AcceptsReturn=true}});root.Children.Add(tabs);
  var scroll=new ScrollViewer{Height=90,Margin=new Thickness(12),VerticalScrollBarVisibility=ScrollBarVisibility.Visible,HorizontalScrollBarVisibility=ScrollBarVisibility.Visible,IsEnabled=false,Content=new Border{Width=1400,Height=600,Background=(Brush)FindResource("PanelBrush")}};root.Children.Add(scroll);
  double disabled=Render(root,Path.Combine(output,"disabled-controls.png"),800,650);if(disabled>.03)throw new Exception("Bright disabled surface ratio "+disabled);
  list.IsEnabled=true;Layout(root,800,650);var listScroll=Descendants<ScrollViewer>(list).First();listScroll.ScrollToVerticalOffset(20);root.UpdateLayout();if(listScroll.VerticalOffset<=0)throw new Exception("ListBox logical scroll broken");
  scroll.IsEnabled=true;scroll.ScrollToVerticalOffset(130);scroll.ScrollToHorizontalOffset(250);root.UpdateLayout();if(scroll.VerticalOffset!=130||scroll.HorizontalOffset!=250)throw new Exception("Pixel scroll broken");
  var bar=Descendants<ScrollBar>(scroll).First(x=>x.Orientation==Orientation.Vertical);var thumb=Descendants<Thumb>(bar).First();thumb.RaiseEvent(new DragStartedEventArgs(0,0){RoutedEvent=Thumb.DragStartedEvent});thumb.RaiseEvent(new DragDeltaEventArgs(0,10){RoutedEvent=Thumb.DragDeltaEvent});root.UpdateLayout();if(scroll.VerticalOffset<=130)throw new Exception("Thumb drag command not routed: "+scroll.VerticalOffset);thumb.RaiseEvent(new DragCompletedEventArgs(0,10,false){RoutedEvent=Thumb.DragCompletedEvent});
  var combo=new ComboBox{Width=300,ItemsSource=new[]{"First popup item","Second item","Third item"},SelectedIndex=1};combo.Style=(Style)FindResource(typeof(ComboBox));combo.ApplyTemplate();Layout(combo,300,40);var popup=(Popup)combo.Template.FindName("PART_Popup",combo);var child=(FrameworkElement)popup.Child;popup.Child=null;
  var popupRoot=new Border{Background=(Brush)FindResource("RaisedBrush"),Child=child};double drop=Render(popupRoot,Path.Combine(output,"dark-popup.png"),330,210);if(drop>.03)throw new Exception("Bright popup ratio "+drop);
  double normal=Render(root,Path.Combine(output,"scroll-enabled.png"),800,650);
  File.WriteAllText(Path.Combine(output,"theme-smoke.json"),JsonSerializer.Serialize(new{Status="PASS",DisabledBrightRatio=disabled,PopupBrightRatio=drop,EnabledBrightRatio=normal,LogicalListScrolling=true,PixelScrolling=true,ThumbEventRouting=true,RootWindowStyleDark=true,WindowShow=false,PopupCapture="Detached actual template child; no popup HWND opened"},new JsonSerializerOptions{WriteIndented=true}));
 }
}
