import 'dart:async';

import 'package:ecosim/src/ecosim/ecosystem_screen.dart';
import 'package:ecosim/src/ecosim/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

import 'ecosystem_sim.dart';
import '../common/widgets/linechart_painter.dart';
import '../common/widgets/simulation_overlay.dart';
import 'ecosystem_painter.dart';

// Screen that runs the Ecosystem Visualizer
// Only runs the visualization of the Simulation
class EcosystemSimulationVisualizer extends ConsumerWidget {
  const EcosystemSimulationVisualizer({
    super.key,
    required this.simulation,
    required this.worldTickTimer,
    required this.worldReplicateBoidsTimer,
    required this.worldReplicateConsumablesTimer,
  });

  final EcosystemSimulation simulation;
  final Timer worldTickTimer;
  final Timer worldReplicateBoidsTimer;
  final Timer worldReplicateConsumablesTimer;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final showDebugInfos = ref.watch(showDebugInfosProvider);

    return SimulationOverlay(
      worldTickTimer: worldTickTimer,
      graphs: GraphWidgets(
        simulation: simulation,
      ),
      drawer: Drawer(
        shape: const Border(
          right: BorderSide(
            width: 2,
            color: Colors.white,
            style: BorderStyle.solid,
          ),
        ),
        child: GraphWidgets(
          simulation: simulation,
        ),
      ),
      screenSize: simulation.screenSize,
      child: GestureDetector(
        onTapDown: (details) {},
        onTapUp: (details) {},
        onVerticalDragUpdate: (details) {},
        onHorizontalDragUpdate: (details) {},
        child: AnimatedBuilder(
          animation: simulation,
          builder: (context, _) {
            const Color.fromARGB(255, 83, 83, 83);
            return CustomPaint(
              size: simulation.screenSize,
              painter: EcosystemSimulationPainter(
                pointerIndicator: simulation.pointerIndicator,
                x: 0,
                y: 0,
                boids: simulation.boids,
                predators: simulation.predators,
                foods: simulation.food,
                poison: simulation.poison,
                screenSize: simulation.screenSize,
                drawDebugInfos: showDebugInfos,
              ),
            );
          },
        ),
      ),
    );
  }
}

class GraphWidgets extends StatelessWidget {
  const GraphWidgets({
    super.key,
    required this.simulation,
  });

  final EcosystemSimulation simulation;

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);

    return ScrollConfiguration(
      behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
      child: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: AnimatedBuilder(
            animation: simulation,
            builder: (context, _) {
              final stats = getStats(
                simulation.boids,
                simulation.predators,
              );
              return Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Graphs',
                    style: theme.textTheme.h4,
                  ),
                  Center(
                    child: Text(
                      'Number of Boids',
                      style: theme.textTheme.p,
                    ),
                  ),
                  LineChartPainter(
                    value1: stats.numBoids.toDouble(),
                    colorValue1: Colors.green,
                    titleValue1: "Boids",
                    value2: stats.numPredators.toDouble(),
                    colorValue2: Colors.purple,
                    titleValue2: "Predators",
                    minY: 0,
                  ),
                  const SizedBox(height: 16),
                  Center(
                    child: Text(
                      'Average Lifespan',
                      style: theme.textTheme.p,
                    ),
                  ),
                  LineChartPainter(
                    value1: stats.avgLifespan,
                    colorValue1: Colors.white,
                    titleValue1: "Lifespan in seconds",
                  ),
                  const SizedBox(height: 16),
                  Center(
                    child: Text(
                      'Average Perception Radius',
                      style: theme.textTheme.p,
                    ),
                  ),
                  LineChartPainter(
                    value1: stats.foodPerceptionRadius,
                    colorValue1: Colors.green,
                    titleValue1: "Food",
                    value2: stats.poisonPerceptionRadius,
                    colorValue2: Colors.red,
                    titleValue2: "Poison",
                    value3: stats.predatorPerceptionRadius,
                    colorValue3: Colors.purple,
                    titleValue3: "Predator",
                  ),
                  const SizedBox(height: 16),
                  Center(
                    child: Text(
                      'Affinity',
                      style: theme.textTheme.p,
                    ),
                  ),
                  LineChartPainter(
                    value1: stats.foodAffinity,
                    colorValue1: Colors.green,
                    titleValue1: "Food Affinity",
                    value2: stats.poisonAffinity,
                    colorValue2: Colors.red,
                    titleValue2: "Poison Affinity",
                    value3: stats.predatorAversion,
                    colorValue3: Colors.purple,
                    titleValue3: "Predator Affinity",
                  ),
                  const SizedBox(height: 16),
                  Center(
                    child: Text(
                      'Average Steering Force',
                      style: theme.textTheme.p,
                    ),
                  ),
                  LineChartPainter(
                    value1: stats.steeringForce,
                    colorValue1: Colors.white,
                    titleValue1: "Steering Force",
                  ),
                  const SizedBox(height: 16),
                  Center(
                    child: Text(
                      'Average Speed',
                      style: theme.textTheme.p,
                    ),
                  ),
                  LineChartPainter(
                    value1: stats.speed,
                    colorValue1: Colors.white,
                    titleValue1: "Speed",
                  ),
                  const SizedBox(height: 16),
                  Center(
                    child: Text(
                      'Consumables',
                      style: theme.textTheme.p,
                    ),
                  ),
                  LineChartPainter(
                    value1: simulation.food.length.toDouble(),
                    colorValue1: Colors.green,
                    titleValue1: "Food",
                    value2: simulation.poison.length.toDouble(),
                    colorValue2: Colors.red,
                    titleValue2: "Poison",
                  ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}
