
import 'dart:convert';

import 'package:bus_infotainment/backend/live_information.dart';
import 'package:bus_infotainment/pages/settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:shared_preferences/shared_preferences.dart';

class InitialStartup extends StatefulWidget {

  @override
  State<InitialStartup> createState() => _InitialStartupState();
}

// Get the current version from /assets/version.txt
Future<String> GetVersion() async {
  return await rootBundle.loadString("assets/version.txt");
}

class _InitialStartupState extends State<InitialStartup> {
  bool AllowPassage = false;


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

    SharedPreferences.getInstance().then((prefs) {
      String? base64String = prefs.getString("announcements");

      if (base64String != null) {
        print("Found previois announcement file");
        Uint8List ByteList = base64Decode(base64String);

        LiveInformation().announcementModule.setBundleBytes(ByteList);

        Navigator.pushNamed(context, "/application");



      } else {
        print("No previous announcement file found");
      }

    });



  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Container(

        alignment: Alignment.center,

        child: Column(

          mainAxisSize: MainAxisSize.min,

          children: [

            PermissionsSetup(),

            SizedBox(
              height: 8,
            ),

            AnnouncementUpload(
              onUploaded: () {
                AllowPassage = true;
                setState(() {

                });
              },

            ),

            if (AllowPassage)
            Container(

              margin: EdgeInsets.all(8),

              height: 32,
              width: double.infinity,
              child: ElevatedButton(
                  onPressed: (){
                    Navigator.pushNamed(context, "/application");
                  },

                  // make the corner radius 4, background color match the theme, and text colour white, fill to width of parent
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.green,
                    // foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(4)
                    ),
                  ),

                  child: Text(
                      "Continue to application...",
                      style: GoogleFonts.interTight(
                          fontSize: 15,
                          fontWeight: FontWeight.w600,
                          color: Colors.white,
                          letterSpacing: 0.5
                      )
                  )
              ),
            ),

            SizedBox(
              height: 8,
            ),

            FutureBuilder(
              future: GetVersion(),
              builder: (context, snapshot){
                if (snapshot.hasData){
                  return Text(
                    "Version ${snapshot.data}",
                    style: GoogleFonts.inter(
                      fontSize: 12,
                      fontWeight: FontWeight.w400,
                      color: Colors.grey
                    ),
                  );
                } else {
                  return Text(
                    "Version -.-.-",
                    style: GoogleFonts.inter(
                      fontSize: 12,
                      fontWeight: FontWeight.w400,
                      color: Colors.grey
                    ),
                  );
                }
              },
            )

          ],



        )

      ),
    );
  }
}