
import 'dart:async';

import 'package:bus_infotainment/backend/live_information.dart';
import 'package:bus_infotainment/utils/delegates.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.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 ***";
  String bottomLine = "";

  late final ListenerReceipt<AnnouncementQueueEntry> _receipt;


  _ibus_displayState(){

    LiveInformation liveInformation = LiveInformation();
    _receipt = liveInformation.announcementModule.onAnnouncement.addListener((value) {

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

      topLine = value.displayText;
      setState(() {

      });
    });
    topLine = liveInformation.announcementModule.currentAnnouncement?.displayText ?? liveInformation.announcementModule.defaultText;



  }



  String _padString(String input){

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

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

    input = input.replaceAll("©", "(c)");

    return prefix + input + suffix;

  }

  @override
  void dispose() {

    LiveInformation().announcementModule.onAnnouncement.removeListener(_receipt);



    super.dispose();

  }

  @override
  Widget build(BuildContext context) {


     return Container(

       width: double.infinity,
       color: Colors.black,

       child: FittedBox(
         alignment: Alignment.center,

         child: Stack(
           children: [

             Positioned.fill(
               child: Container(

                 alignment: Alignment.bottomCenter,

                 margin: EdgeInsets.all(1),

                 child: Text(
                   "© ImBenji.net - all rights reserved  |  © Transport for London - all rights reserved",
                   style: GoogleFonts.teko(
                     fontSize: 10,
                     color: Colors.white.withOpacity(0.01),
                     height: 1,
                     shadows: [
                       Shadow(
                         color: Colors.black,
                         blurRadius: 5,
                       )
                     ]
                   )
                 ),

               ),
             ),

             Container(

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

               decoration: BoxDecoration(
                 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(180, 0)),
                              style: const TextStyle(
                                fontSize: 20,
                                color: Colors.orange,
                                fontFamily: "ibus",
                                shadows: [
                                  Shadow(
                                    color: Colors.orange,
                                    blurRadius: 5,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),

                        Transform.translate(
                          offset: Offset(0, -3.5),
                          child: Container(
                            alignment: Alignment.center,
                            width: 32*4*3,
                            child: _timeComponent(),
                          ),
                        ),
                      ],
                   ),
                 ),
               )

             ),



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

class _timeComponent extends StatefulWidget {

  @override
  State<_timeComponent> createState() => _timeComponentState();
}

class _timeComponentState extends State<_timeComponent> {

  late Timer timeTimer;

  String bottomLine = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    bottomLine = _getTime();

    timeTimer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (mounted){
        setState(() {
          bottomLine = _getTime();
        });
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

    timeTimer.cancel();
  }

  String _getTime(){
    // Get the current time HH:MM AM/PM
    DateTime now = DateTime.now();

    bool is24Hour = false;

    // if 16 then 4...

    String timeString = "${now.hour % 12}:${now.minute.toString().padLeft(2, "0")} ${now.hour < 12 ? "AM" : "PM"}";

    return timeString;
  }

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

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

    input = input.replaceAll("©", "(c)");

    return prefix + input + suffix;
  }

  @override
  Widget build(BuildContext context) {
    return TextScroll(
      _padString(bottomLine),
      velocity: Velocity(pixelsPerSecond: Offset(120, 0)),
      style: const TextStyle(
        fontSize: 20,
        color: Colors.orange,
        fontFamily: "ibus",
        shadows: [
          Shadow(
            color: Colors.orange,
            blurRadius: 5,
          ),
        ],
      ),
    );
  }
}