import 'package:ecosim/src/seeking/seeking_sim.dart';
import 'package:ecosim/src/common/widgets/simulation_overlay.dart';
import 'package:flutter/material.dart';

import 'seeking_sim_painter.dart';

class SeekingScreen extends StatefulWidget {
  const SeekingScreen({super.key});

  @override
  State<SeekingScreen> createState() => _SeekingScreenState();
}

class _SeekingScreenState extends State<SeekingScreen>
    with SingleTickerProviderStateMixin {
  double pointerx = 200.0;
  double pointery = 200.0;

  late SeekingSimulation simulation;

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

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      simulation = SeekingSimulation(this, context);
      setState(() {});
    });
  }

  void pointerUpdate(details) {
    pointerx = details.localPosition.dx;
    pointery = details.localPosition.dy;
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return SimulationOverlay(
      screenSize: screenSize,
      child: MouseRegion(
        onHover: (event) {
          // setState(
          //   () {
          //     pointerx = event.localPosition.dx;
          //     pointery = event.localPosition.dy;
          //   },
          // );
        },
        child: GestureDetector(
          onTapDown: pointerUpdate,
          onTapUp: pointerUpdate,
          onVerticalDragUpdate: pointerUpdate,
          onHorizontalDragUpdate: pointerUpdate,
          child: AnimatedBuilder(
            animation: simulation,
            builder: (context, widget) {
              // var curr = currentTime;
              // var dt = curr - previous;
              // previous = curr;
              return CustomPaint(
                size: screenSize,
                painter: SeekingSimulationPainter(
                  simulation.pointerIndicator,
                  pointerx,
                  pointery,
                  simulation.boids,
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
