import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:syncrow_app/features/devices/model/offline_password_model.dart';
import 'package:syncrow_app/features/devices/model/temporary_password_model.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/font_manager.dart';

class DoorDialog extends StatefulWidget {
  final String? title;
  final TemporaryPassword? temporaryPassword;
  final OfflinePasswordModel? offline;

  const DoorDialog({
    super.key,
    this.title,
    this.offline,
    this.temporaryPassword,
  });

  @override
  DoorDialogState createState() => DoorDialogState();
}

class DoorDialogState extends State<DoorDialog> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final effectiveTime =
        widget.temporaryPassword?.effectiveTime ?? int.parse(widget.offline?.gmtStart);
    final invalidTime =
        widget.temporaryPassword?.invalidTime ?? int.parse(widget.offline?.gmtExpired);

    final DateTime effectiveDateTime =
        DateTime.fromMillisecondsSinceEpoch(effectiveTime * 1000, isUtc: false);
    String formattedDateEffectiveTime = DateFormat('yyyy-MM-dd').format(effectiveDateTime);
    String formattedTimeEffectiveTime = DateFormat('hh:mm a').format(effectiveDateTime);

    final DateTime expiredDateTime =
        DateTime.fromMillisecondsSinceEpoch(invalidTime * 1000, isUtc: false);
    String formattedDateExpiredDateTime = DateFormat('yyyy-MM-dd').format(expiredDateTime);
    String formattedTimeExpiredDateTime = DateFormat('hh:mm a').format(expiredDateTime);
    return Dialog(
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20),
        ),
        padding: const EdgeInsets.only(top: 20),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            BodyMedium(
              text: widget.title ?? '',
              style: context.bodyMedium.copyWith(
                color: ColorsManager.primaryColorWithOpacity,
                fontWeight: FontsManager.extraBold,
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(
                vertical: 15,
                horizontal: 50,
              ),
              child: Container(
                height: 1,
                width: double.infinity,
                color: ColorsManager.greyColor,
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(15.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Effective Date:'),
                      Text(formattedDateEffectiveTime),
                    ],
                  ),
                  SizedBox(height: 8),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Effective Time:'),
                      Text(formattedTimeEffectiveTime),
                    ],
                  ),
                  SizedBox(height: 16),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Expired Date:'),
                      Text(formattedDateExpiredDateTime),
                    ],
                  ),
                  SizedBox(height: 8),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Expired Time:'),
                      Text(formattedTimeExpiredDateTime),
                    ],
                  ),
                ],
              ),
            ),
            Container(
              height: 1,
              width: double.infinity,
              color: ColorsManager.greyColor,
            ),
            widget.temporaryPassword?.effectiveTime != null
                ? Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      InkWell(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Center(
                          child: BodyMedium(
                            text: 'Cancel',
                            style: context.bodyMedium.copyWith(color: ColorsManager.greyColor),
                          ),
                        ),
                      ),
                      Container(
                        height: 50,
                        width: 1,
                        color: ColorsManager.greyColor,
                      ),
                      InkWell(
                        onTap: () {
                          Navigator.pop(context, 'delete');
                        },
                        child: Center(
                          child: BodyMedium(
                            text: 'Delete Password',
                            style: context.bodyMedium
                                .copyWith(color: ColorsManager.primaryColorWithOpacity),
                          ),
                        ),
                      ),
                    ],
                  )
                : Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Container(
                        height: 50,
                        child: InkWell(
                          onTap: () {
                            Navigator.pop(context);
                          },
                          child: Center(
                            child: BodyMedium(
                              text: 'Cancel',
                              style: context.bodyMedium.copyWith(color: ColorsManager.greyColor),
                            ),
                          ),
                        ),
                      ),
                    ],
                  )
          ],
        ),
      ),
    );
  }
}
