
import 'package:bus_infotainment/singletons/live_information.dart';
import 'package:bus_infotainment/tfl_datasets.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:text_scroll/text_scroll.dart';

class pages_Routes extends StatefulWidget {

  @override
  State<pages_Routes> createState() => _pages_RoutesState();
}

class _pages_RoutesState extends State<pages_Routes> {
  final TextEditingController _controller = TextEditingController(text: "");

  @override
  Widget build(BuildContext context) {

    LiveInformation liveInformation = LiveInformation();

    List<Widget> routes = [];
    routes.add(SizedBox(height: 10));
    for (BusRoute route in liveInformation.busSequences!.routes.values) {

      if (!route.routeNumber.toLowerCase().contains(_controller.text.toLowerCase())) {
        continue;
      }

      routes.add(_Route(route, this));
      routes.add(SizedBox(height: 10));
    }

    return Container(
        color: Theme.of(context).colorScheme.background,
        width: double.infinity,
        // padding: const EdgeInsets.symmetric(horizontal: 10),
        child: Column(

          children: [



            Container(

              padding: const EdgeInsets.all(10),



              decoration: BoxDecoration(
                color: Theme.of(context).colorScheme.background,
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.5),
                    spreadRadius: 2,
                    blurRadius: 4,
                  ),
                ],
              ),

              child: TextField(
                controller: _controller,
                onChanged: (String value) {
                  setState(() {});
                },
              ),
            ),

            Expanded(
              child: ListView(
                children: routes,
              ),
            ),

          ],

        )
    );

  }
}

class _Route extends StatelessWidget {

  final BusRoute route;
  final _pages_RoutesState tfL_Dataset_TestState;

  const _Route(this.route, this.tfL_Dataset_TestState);

  @override
  Widget build(BuildContext context) {

    List<Widget> Variants = [];

    for (BusRouteVariant variant in route.routeVariants.values) {
      Variants.add(const SizedBox(height: 10));
      Variants.add(_Variant(route, variant, tfL_Dataset_TestState));
    }

    return Container(

      alignment: Alignment.center,

      decoration: BoxDecoration(
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.5),
            spreadRadius: 2,
            blurRadius: 4,
          ),
        ],
        color: Colors.grey.shade900,
        borderRadius: BorderRadius.circular(10),
      ),

      margin: const EdgeInsets.symmetric(horizontal: 10),
      padding: const EdgeInsets.all(10),

      width: 100,

      child: Column(

        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,

        children: [

          Container(

            decoration: BoxDecoration(
              color: Colors.grey.shade800,
              borderRadius: BorderRadius.circular(5),
            ),

            padding: const EdgeInsets.all(8),

            child: Text(
              "Route: ${route.routeNumber}",
              style: GoogleFonts.montserrat(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                  height: 1
              ),

            ),

          ),

          ListView(
            children: Variants,
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
          ),

        ],

      ),

    );
  }
}

class _Variant extends StatelessWidget {

  final BusRoute route;
  final BusRouteVariant variant;

  final _pages_RoutesState tfL_Dataset_TestState;

  const _Variant(this.route, this.variant, this.tfL_Dataset_TestState);

  @override
  Widget build(BuildContext context) {

    return Container(

        child: Stack(
          children: [

            Container(

                decoration: BoxDecoration(
                  color: Colors.grey.shade800,
                  borderRadius: BorderRadius.circular(5),
                ),

                padding: const EdgeInsets.all(8),

                child: Column(

                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,

                  children: [

                    Row(
                      children: [
                        Text(
                          "Start:",
                          style: GoogleFonts.montserrat(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                            color: Colors.white,
                            height: 1,
                          ),
                        ),

                        SizedBox(width: 5),

                        Expanded(
                          child: TextScroll(
                            "${variant.busStops.first.formattedStopName}",
                            mode: TextScrollMode.bouncing,
                            pauseBetween: const Duration(seconds: 2),
                            pauseOnBounce: const Duration(seconds: 2),
                            style: GoogleFonts.montserrat(
                              fontSize: 15,
                              fontWeight: FontWeight.normal,
                              color: Colors.white,
                              height: 1,
                            ),
                          ),
                        ),

                      ],
                    ),

                    SizedBox(height: 5),

                    Row(
                      children: [
                        Text(
                          "End:",
                          style: GoogleFonts.montserrat(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                            color: Colors.white,
                          ),
                        ),

                        SizedBox(width: 5),

                        Expanded(
                          child: TextScroll(
                            "${variant.busStops.last.formattedStopName}",
                            mode: TextScrollMode.bouncing,
                            pauseBetween: const Duration(seconds: 2),
                            pauseOnBounce: const Duration(seconds: 2),
                            style: GoogleFonts.montserrat(
                              fontSize: 15,
                              fontWeight: FontWeight.normal,
                              color: Colors.white,
                              height: 1,
                            ),
                          ),
                        ),

                      ],
                    ),



                  ],
                )
            ),

            Positioned.fill(
              child: ElevatedButton(
                  onPressed: () {
                    print("Variant: ${variant.routeVariant}");

                    // Open dialog
                    showDialog(
                        context: context,
                        builder: (BuildContext context) {
                          return AlertDialog(
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(5),
                            ),
                            contentPadding: const EdgeInsets.only(
                              top: 15,
                              left: 15,
                              right: 15,
                              bottom: 8,

                            ),
                            content: Column(

                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [

                                Text(
                                  "Select the following route?",
                                  style: GoogleFonts.montserrat(
                                    fontSize: 15,
                                    fontWeight: FontWeight.w700,
                                    color: Colors.black,
                                  ),
                                ),

                                Text(
                                  "${route.routeNumber} to ${variant.busStops.last.formattedStopName}",
                                  style: GoogleFonts.montserrat(
                                    fontSize: 15,
                                    fontWeight: FontWeight.w500,
                                    color: Colors.black,
                                  ),
                                ),


                              ],

                            ),

                            actionsPadding: const EdgeInsets.only(
                              left: 15,
                              right: 15,
                              bottom: 15,
                            ),
                            actions: [
                              ElevatedButton(
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },

                                style: ElevatedButton.styleFrom(
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(5),
                                  ),
                                ),

                                child: const Text("Cancel"),
                              ),
                              ElevatedButton(
                                onPressed: () {
                                  Navigator.of(context).pop();

                                  LiveInformation liveInformation = LiveInformation();
                                  liveInformation.setRouteVariant(variant);
                                  tfL_Dataset_TestState.setState(() {});
                                },

                                style: ElevatedButton.styleFrom(
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(5),
                                  ),
                                ),

                                child: const Text("Confirm"),
                              ),
                            ],
                          );
                        }
                    );

                  },

                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.transparent,
                    shadowColor: Colors.transparent,
                    surfaceTintColor: Colors.transparent,
                    foregroundColor: Colors.transparent,

                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(0),
                    ),

                  ),


                  child: Container()
              ),
            )

          ],
        )

    );

  }




}