
import 'dart:async';

import 'package:bus_infotainment/singletons/live_information.dart';
import 'package:bus_infotainment/utils/delegates.dart';
import 'package:flutter/material.dart';
import 'package:text_scroll/text_scroll.dart';

class ibus_display extends StatefulWidget {

  bool hasBorder = true;

  ibus_display({
    this.hasBorder = true
  });

  @override
  State<ibus_display> createState() => _ibus_displayState();
}

class _ibus_displayState extends State<ibus_display> {

  String topLine = "*** NO MESSAGE ***";

  late final ListenerReceipt<AnnouncementQueueEntry> _receipt;

  _ibus_displayState(){

    LiveInformation liveInformation = LiveInformation();
    _receipt = liveInformation.announcementDelegate.addListener((value) {

      if (topLine == value.displayText){
        return;
      }

      topLine = value.displayText;
      setState(() {

      });
    });
    topLine = liveInformation.currentAnnouncement;

  }

  String _padString(String input){

    if (input.length < 40){
      print("Input is too short");
      return input;
    }

    String prefix = "";
    String suffix = "";
    for (int i = 0; i < 80; i++){
      prefix += " ";
    }

    return prefix + input + suffix;

  }

  @override
  void dispose() {

    LiveInformation().announcementDelegate.removeListener(_receipt);

    super.dispose();

  }

  @override
  Widget build(BuildContext context) {


     return Container(

       width: double.infinity,

       child: FittedBox(
         alignment: Alignment.center,

         child: Stack(
           children: [
             Container(

               // width: double.infinity,
               // height: 100,

               decoration: BoxDecoration(
                 color: Colors.black,
                 border: widget.hasBorder ? Border.all(color: Colors.grey.shade900, width: 2) : null,
               ),

               clipBehavior: Clip.hardEdge,

               child: Transform.scale(
                 scale: 1.3,
                 transformHitTests: false,
                 child: Transform.translate(

                   offset: Offset(0, 4),

                   child: Column(

                      children: [
                        Transform.translate(
                          offset: Offset(0, 5),
                          child: Container(
                            alignment: Alignment.center,
                            width: 32*4*3,
                            child: TextScroll(
                              _padString(topLine),
                              velocity: Velocity(pixelsPerSecond: Offset(120, 0)),
                              style: const TextStyle(
                                fontSize: 20,
                                color: Colors.orange,
                                fontFamily: "ibus",
                                shadows: [
                                  Shadow(
                                    color: Colors.orange,
                                    blurRadius: 5,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),

                        Transform.translate(
                          offset: Offset(0, -7),
                          child: Text(
                            "",
                            style: const TextStyle(
                              fontSize: 20,
                              color: Colors.orange,
                              fontFamily: "ibus",
                              height: 1.5
                            ),
                          ),
                        )
                      ],
                   ),
                 ),
               )

             ),

             Positioned.fill(
               child: Container(
                 decoration: BoxDecoration(
                   color: Colors.transparent,
                   border: widget.hasBorder ? Border.all(color: Colors.grey.shade900, width: 2) : null,
                 ),
               ),
             )
           ],
         ),
       ),
     );
  }
}