import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../../../api/login_api_client.dart';
import '../../../controllers/localization/localization.dart';
import '../../../models/enums/user_types.dart';
import '../../../models/service_request/service_request.dart';
import '../../../models/subtitle.dart';
import '../../app_style/colors.dart';
import '../../app_style/sizing.dart';
import '../../pages/user/requests/report/create_service_report.dart';
import '../loaders/image_loader.dart';
import 'request_status.dart';

class ServiceRequestItem extends StatelessWidget {
  final int index;
  final ServiceRequest request;
  final Function(ServiceRequest) onPressed;

  const ServiceRequestItem({
    Key? key,
    required this.request,
    required this.onPressed,
    required this.index,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Subtitle? subtitle = AppLocalization.of(context)?.subtitle;
    Color itemColor = index % 2 == 0 ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.onPrimary;
    Color onItemColor = index % 2 != 0 ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.onPrimary;

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
          backgroundColor: itemColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(
              AppStyle.getBorderRadius(context),
            ),
          ),
        ),
        onPressed: () {
          onPressed(request);
        },
        child: Row(
          children: [
            UserApiClient().user?.type == UsersTypes.normal_user && (request.devicePhotos?.isEmpty ?? true)
                ? const SizedBox.shrink()
                : SizedBox(
                    width: 80,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        request.devicePhotos?.isEmpty ?? true
                            ? const SizedBox.shrink()
                            : Column(
                                children: [
                                  SizedBox(
                                    height: 80,
                                    width: 80,
                                    child: ImageLoader(
                                      url: request.devicePhotos?.first ?? '',
                                      boxFit: BoxFit.cover,
                                    ),
                                  ),
                                  const SizedBox(
                                    height: 24,
                                  ),
                                ],
                              ),

                        UserApiClient().user?.type == UsersTypes.engineer
                            ? Material(
                                color: onItemColor,
                                elevation: 6,
                                shape: const CircleBorder(),
                                child: IconButton(
                                  icon: Icon(
                                    Icons.description,
                                    color: itemColor,
                                    size: 32,
                                  ),
                                  onPressed: () {
                                    Navigator.of(context).push(
                                      MaterialPageRoute(
                                        builder: (_) => CreateServiceReport(
                                          request: request,
                                        ),
                                      ),
                                    );
                                  },
                                ),
                              )
                            : const SizedBox.shrink(),
                        //SizedBox(height: 8,),
                      ],
                    ),
                  ),
            const SizedBox(
              width: 8,
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Expanded(
                                  child: request.requestCode == null
                                      ? const SizedBox.shrink()
                                      : Text(
                                          request.requestCode ?? "-----",
                                          style: Theme.of(context).textTheme.titleLarge?.copyWith(
                                                color: onItemColor,
                                                fontSize: 16,
                                                fontWeight: FontWeight.bold,
                                              ),
                                        ),
                                ),
                                request.engineerName == null
                                    ? const SizedBox.shrink()
                                    : Text(
                                        request.engineerName ?? "",
                                        style: Theme.of(context).textTheme.titleSmall?.copyWith(
                                              color: onItemColor,
                                              fontSize: 12,
                                              fontWeight: FontWeight.normal,
                                            ),
                                      ),
                              ],
                            ),
                            Divider(
                              color: onItemColor,
                            ),
                            Row(
                              children: [
                                Expanded(
                                  child: request.deviceModel == null
                                      ? const SizedBox.shrink()
                                      : Text(
                                          request.deviceModel ?? "Model not available",
                                          style: Theme.of(context).textTheme.titleSmall?.copyWith(
                                                color: onItemColor,
                                                fontSize: 12,
                                                fontWeight: FontWeight.normal,
                                              ),
                                        ),
                                ),
                                request.engineerMobile == null
                                    ? const SizedBox.shrink()
                                    : Text(
                                        request.engineerMobile ?? '',
                                        style: Theme.of(context).textTheme.titleMedium?.copyWith(
                                              color: onItemColor,
                                              fontSize: 12,
                                              fontWeight: FontWeight.normal,
                                            ),
                                      ),
                              ],
                            ),
                            Divider(
                              color: onItemColor,
                            ),
                            Row(
                              children: [
                                Expanded(
                                  child: request.deviceSerialNumber == null
                                      ? const SizedBox.shrink()
                                      : Text(
                                          request.deviceSerialNumber ?? '',
                                          style: Theme.of(context).textTheme.titleSmall?.copyWith(
                                                color: onItemColor,
                                                fontSize: 12,
                                                fontWeight: FontWeight.normal,
                                              ),
                                        ),
                                ),
                              ],
                            ),
                            request.deviceEnName == null
                                ? const SizedBox.shrink()
                                : Row(
                                    children: [
                                      Expanded(
                                        child: Text(
                                          request.deviceEnName ?? '',
                                          style: Theme.of(context).textTheme.titleMedium?.copyWith(
                                                color: onItemColor,
                                                fontSize: 12,
                                                fontWeight: FontWeight.normal,
                                              ),
                                        ),
                                      ),
                                    ],
                                  ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  Divider(
                    color: onItemColor,
                  ),
                  Center(
                    child: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 8),
                      child: Text(
                        request.maintenanceIssue ?? "No maintenance issue found",
                        style: Theme.of(context).textTheme.titleMedium?.copyWith(color: onItemColor),
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                  Divider(color: onItemColor),
                  Row(
                    children: [
                      Text(
                        request.date ?? "Date not available",
                        style: Theme.of(context).textTheme.titleSmall?.copyWith(color: onItemColor),
                      ),
                      const Spacer(),
                      StatusLabel(
                        label: request.statusLabel,
                        color: AColors.getRequestStatusColor(
                          request.statusValue ?? 0,
                        ),
                      ),
                    ],
                  ),
                  request.nextVisitDate == null
                      ? const SizedBox.shrink()
                      : Column(
                          children: [
                            Divider(
                              color: onItemColor,
                            ),
                            Row(
                              children: [
                                Text(
                                  subtitle?.nextVisitDate ?? '',
                                  style: Theme.of(context).textTheme.titleSmall?.copyWith(color: onItemColor),
                                ),
                                const Spacer(),
                                Text(
                                  DateFormat('EE dd/MM/yyyy').format(request.nextVisitDate!),
                                  style: Theme.of(context).textTheme.titleSmall?.copyWith(color: onItemColor),
                                ),
                              ],
                            ),
                          ],
                        ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
