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

class MihAppWindow extends StatefulWidget {
  final String windowTitle;
  final List<Widget> windowBody;
  final List<Widget> windowTools;
  final void Function() onWindowTapClose;
  final bool fullscreen;
  const MihAppWindow({
    super.key,
    required this.fullscreen,
    required this.windowTitle,
    required this.windowTools,
    required this.onWindowTapClose,
    required this.windowBody,
  });

  @override
  State<MihAppWindow> createState() => _MihAppWindowState();
}

class _MihAppWindowState extends State<MihAppWindow> {
  late double windowTitleSize;
  late double horizontralWindowPadding;
  late double vertticalWindowPadding;
  late double windowWidth;
  late double windowHeight;
  late double width;
  late double height;

  void checkScreenSize() {
    // print("screen width: $width");
    // print("screen height: $height");
    if (MzanziInnovationHub.of(context)!.theme.screenType == "desktop") {
      setState(() {
        windowTitleSize = 25;
        horizontralWindowPadding = width / 7;
        vertticalWindowPadding = 25;
        windowWidth = width;
        windowHeight = height;
      });
    } else {
      setState(() {
        windowTitleSize = 20;
        horizontralWindowPadding = 10;
        vertticalWindowPadding = 10;
        windowWidth = width;
        windowHeight = height;
      });
    }
  }

  Widget getWidnowClose() {
    return Container(
      alignment: Alignment.centerRight,
      child: IconButton(
        onPressed: widget.onWindowTapClose,
        icon: Icon(
          Icons.close,
          color: MzanziInnovationHub.of(context)!.theme.errorColor(),
          size: 35,
        ),
      ),
    );
  }

  Widget getWidnowTools() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      mainAxisSize: MainAxisSize.max,
      children: widget.windowTools,
    );
  }

  Widget getWidnowTitle() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Expanded(
          child: Text(
            widget.windowTitle,
            textAlign: TextAlign.center,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
              fontSize: windowTitleSize,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ],
    );
  }

  Widget getWidnowHeader() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: [
        getWidnowTools(),
        Expanded(
          flex: 2,
          child: getWidnowTitle(),
        ),
        getWidnowClose(),
      ],
    );
  }

  Widget getWidnowBody() {
    if (widget.fullscreen) {
      return Expanded(
        child: Padding(
          padding: const EdgeInsets.all(10.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: widget.windowBody,
          ),
        ),
      );
    } else {
      return Padding(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: widget.windowBody,
        ),
      );
    }
  }

  Widget createWindow(Widget header, Widget body) {
    Widget visibleItems;
    if (widget.fullscreen) {
      visibleItems = Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          header,
          //const Divider(),
          body,
        ],
      );
    } else {
      visibleItems = SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            header,
            //const Divider(),
            body,
          ],
        ),
      );
    }
    return Dialog(
      insetPadding: EdgeInsets.symmetric(
        horizontal: horizontralWindowPadding,
        vertical: vertticalWindowPadding,
      ),
      insetAnimationCurve: Easing.emphasizedDecelerate,
      insetAnimationDuration: Durations.short1,
      child: Container(
        //padding: const EdgeInsets.all(10),
        width: windowWidth,
        //height: windowHeight,
        decoration: BoxDecoration(
          color: MzanziInnovationHub.of(context)!.theme.primaryColor(),
          borderRadius: BorderRadius.circular(25.0),
          border: Border.all(
              color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
              width: 5.0),
        ),
        child: visibleItems,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    setState(() {
      width = size.width;
      height = size.height;
    });
    checkScreenSize();
    return createWindow(
      getWidnowHeader(),
      getWidnowBody(),
    );
  }
}
