import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:intl/intl.dart' show DateFormat;
import 'package:krow/core/entity/staff_contact_entity.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/widgets/staff_position_details_widget.dart';

import '../../../../../../core/presentation/styles/theme.dart';

class StaffInvoiceContactInfoPopup {
  static Future<void> show(
    BuildContext context,
    StaffContact staff, {
    String? date,
    required double hours,
    required double subtotal,
  }) async {
    return showDialog<void>(
        context: context,
        builder: (context) {
          return Center(
            child: _StaffPopupWidget(
              staff,
              date: date,
              hours: hours,
              subtotal: subtotal,
            ),
          );
        });
  }
}

class _StaffPopupWidget extends StatelessWidget {
  final StaffContact staff;
  final String? date;

  final double hours;

  final double subtotal;

  const _StaffPopupWidget(this.staff,
      {this.date, required this.hours, required this.subtotal});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 12),
        margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
        // margin: const EdgeInsets.symmetric(horizontal: 12),
        decoration: KwBoxDecorations.white24,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: _ongoingBtn(context),
        ),
      ),
    );
  }

  List<Widget> _ongoingBtn(BuildContext context) {
    return [
      const Gap(32),
      StaffPositionAvatar(
        imageUrl: staff.photoUrl,
        userName: '${staff.firstName} ${staff.lastName}',
        status: staff.status,
      ),
      const Gap(16),
      StaffContactsWidget(staff: staff),
      StaffPositionDetailsWidget(
          staff: staff, date: date, hours: hours, subtotal: subtotal),
      const Gap(12),
    ];
  }
}

class StaffPositionDetailsWidget extends StatelessWidget {
  final StaffContact staff;
  final String? date;

  final double hours;

  final double subtotal;

  const StaffPositionDetailsWidget({
    super.key,
    required this.staff,
    this.date,
    required this.hours,
    required this.subtotal,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      margin: const EdgeInsets.all(12),
      decoration: KwBoxDecorations.primaryLight8,
      child: Column(
        children: [
          _textRow('Date',
              DateFormat('MM.dd.yyyy').format(DateTime.parse(date ?? ''))),
          _textRow(
              'Start Time',
              DateFormat('hh:mm a').format(
                  DateFormat('yyyy-MM-dd hh:mm:ss').parse(staff.startAt))),
          _textRow(
              'End Time',
              DateFormat('hh:mm a').format(
                  DateFormat('yyyy-MM-dd hh:mm:ss').parse(staff.endAt))),
          if (staff.breakIn.isNotEmpty && staff.breakOut.isNotEmpty)
            _textRow(
                'Break',
                DateTime.parse(staff.breakIn)
                        .difference(DateTime.parse(staff.breakOut))
                        .inMinutes
                        .toString() +
                    ' minutes'),
          _textRow('Rate (\$/h)', '\$${staff.rate.toStringAsFixed(2)}/h'),
          _textRow('Hours', '${hours.toStringAsFixed(2)}'),
          _textRow('Subtotal', '\$${subtotal.toStringAsFixed(2)}'),
        ],
      ),
    );
  }

  Widget _textRow(String title, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: AppTextStyles.bodySmallReg.copyWith(
              color: AppColors.blackGray,
            ),
          ),
          Text(
            value,
            style: AppTextStyles.bodySmallMed,
          ),
        ],
      ),
    );
  }
}
