import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import '../theme/app_theme.dart';
import 'top_drag_area.dart';
import 'window_chrome.dart';
import 'window_controls.dart';


class WindowChromeOverlay extends StatelessWidget {


  final bool onDarkSurface;

  const WindowChromeOverlay({super.key, this.onDarkSurface = false});

  @override
  Widget build(BuildContext context) {
    if (!WindowChrome.isDesktop) {
      return const SizedBox.shrink();
    }
    final AppTokens tokens = context.appTokens;
    return Positioned(
      top: 0,
      left: 0,
      right: 0,
      height: tokens.titleBarHeight,
      child: _HitTestPassThrough(
        child: Stack(
          children: [
            const Positioned.fill(
              child: TopDragArea(child: SizedBox.expand()),
            ),
            Positioned(
              top: tokens.chromeInset,
              right: tokens.chromeInset,
              child: onDarkSurface
                  ? const WindowControls.forDarkSurface()
                  : const WindowControls.forShellOverlay(),
            ),
          ],
        ),
      ),
    );
  }
}


class WindowChromeHost extends StatelessWidget {
  final Widget child;
  final bool onDarkSurface;

  const WindowChromeHost({
    super.key,
    required this.child,
    this.onDarkSurface = false,
  });

  @override
  Widget build(BuildContext context) {
    if (!WindowChrome.isDesktop) {
      return child;
    }
    return Stack(
      fit: StackFit.expand,
      children: [
        child,
        WindowChromeOverlay(onDarkSurface: onDarkSurface),
      ],
    );
  }
}


class _HitTestPassThrough extends SingleChildRenderObjectWidget {
  const _HitTestPassThrough({required super.child});

  @override
  RenderObject createRenderObject(BuildContext context) =>
      _RenderHitTestPassThrough();
}

class _RenderHitTestPassThrough extends RenderProxyBox {
  @override
  bool hitTest(BoxHitTestResult result, {required Offset position}) {
    hitTestChildren(result, position: position);
    return false;
  }
}
