import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:supplier_new/orders/unloading_completed_otp.dart';

import '../common/settings.dart';
import 'all_orders.dart';

TextStyle fts(double s, Color c, FontWeight w) =>
    GoogleFonts.inter(fontSize: s, color: c, fontWeight: w);

class UnloadingInProgressScreen extends StatefulWidget {
  final Duration? startFrom;
  var details;

  UnloadingInProgressScreen({
    this.details,
    this.startFrom
  });
  /// If you want to start from a preset time (e.g., 12 minutes), pass Duration(minutes: 12)


  @override
  State<UnloadingInProgressScreen> createState() => _UnloadingInProgressScreenState();
}

class _UnloadingInProgressScreenState extends State<UnloadingInProgressScreen> {
  late Duration _elapsed;
  Timer? _timer;

  @override
  void initState() {
    super.initState();
    _elapsed = widget.startFrom ?? Duration.zero;
    _timer = Timer.periodic(const Duration(seconds: 1), (_) {
      setState(() => _elapsed += const Duration(seconds: 1));
    });
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  String _fmt(Duration d) {
    final h = d.inHours.toString().padLeft(2, '0');
    final m = (d.inMinutes % 60).toString().padLeft(2, '0');
    final s = (d.inSeconds % 60).toString().padLeft(2, '0');
    return '$h:$m:$s';
  }

  void _onComplete() {
    _timer?.cancel();
    // Optionally show a toast/snackbar, then navigate
    // ScaffoldMessenger.of(context).showSnackBar(
    //   SnackBar(content: Text('Unloading marked complete at ${_fmt(_elapsed)}')),
    // );

    Navigator.pushReplacement(
      context,
      MaterialPageRoute(builder: (_) => UnloadingCompleteScreen(details: widget.details,)),
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          Navigator.pushAndRemoveUntil(
            context,
            MaterialPageRoute(builder: (_) =>  AllOrders(navigationFrom:"")),
                (route) => false,
          );
          return false;
        },

        child:Scaffold(
          backgroundColor: Colors.white,
          appBar: AppSettings.appBarWithNotificationIcon(widget.details.building_name, widget.details.type_of_water, widget.details.capacity, context),

          body: SafeArea(
            child: Center(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 24),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('Unloading in-progress',
                        style: fontTextStyle(24, const Color(0xFF000000), FontWeight.w500)),
                    const SizedBox(height: 12),
                    Text(
                      _fmt(_elapsed),
                      style: fontTextStyle(40, const Color(0xFF000000), FontWeight.w700),
                    ),
                    const SizedBox(height: 24),
                    SizedBox(
                      width: double.infinity,
                      height: 48,
                      child: ElevatedButton(
                        onPressed: _onComplete,
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Colors.black,
                          foregroundColor: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(24),
                          ),
                        ),
                        child: Text('Unloading Complete',
                            style: fontTextStyle(14, const Color(0xFFFFFFFF), FontWeight.w400)),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ));
  }
}
