import 'package:ecosim/src/benchmark/benchmark_sim.dart';
import 'package:ecosim/src/benchmark/benchmark_sim_painter.dart';
import 'package:ecosim/src/common/widgets/simulation_overlay.dart';
import 'package:flutter/material.dart';
import 'package:show_fps/show_fps.dart';

class BenchmarkSimulationScreen extends StatefulWidget {
  const BenchmarkSimulationScreen({
    super.key,
    required this.boids,
  });

  final int boids;

  @override
  State<BenchmarkSimulationScreen> createState() =>
      _BenchmarkSimulationScreenState();
}

class _BenchmarkSimulationScreenState extends State<BenchmarkSimulationScreen>
    with SingleTickerProviderStateMixin {
  // AnimationController? _controller;
  // Animation<double>? _animation;
  // PointerIndicator pointerIndicator = PointerIndicator(0.0, 0.0);
  // final DateTime _initialTime = DateTime.now();
  // double previous = 0.0;
  double pointerx = 0.0;
  double pointery = 0.0;
  // double get currentTime =>
  //     DateTime.now().difference(_initialTime).inMilliseconds / 1000.0;

  late BenchmarkSimulation simulation;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      simulation = BenchmarkSimulation(
        this,
        context,
        startBoidsCount: widget.boids,
        startFoodCount: 100,
        startPoisonCount: 0,
        presetDna: [
          // Food Weight
          1,
          // Poison Weight
          1,
          // Food perception
          100,
          // Poison perception
          1,
        ],
      );
      setState(() {});
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return ShowFPS(
      child: SimulationOverlay(
        screenSize: const Size(1000, 1000),
        child: MouseRegion(
          onHover: (event) {
            // setState(
            //   () {
            //     pointerx = event.position.dx;
            //     pointery = event.position.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: MediaQuery.of(context).scale().size,
                  size: const Size(1000, 1000),
                  painter: BenchmarkSimulationPainter(
                    simulation.pointerIndicator,
                    pointerx,
                    pointery,
                    simulation.boids,
                    simulation.foods,
                    simulation.poison,
                  ),
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
