import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/controllers/providers/api/service_requests_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/service_request/service_request.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/models/user.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/pages/user/requests/report/create_service_report.dart';
import 'package:test_sa/views/widgets/loaders/image_loader.dart';
import 'package:test_sa/views/widgets/requests/request_status.dart';

class ServiceRequestItem extends StatelessWidget {
  final int index;
  final ServiceRequest request;
  final Function(ServiceRequest) onPressed;
  const ServiceRequestItem({Key key, this.request, this.onPressed, this.index}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Subtitle _subtitle = AppLocalization.of(context).subtitle;
    User _user = Provider.of<UserProvider>(context, listen: false).user;
    final servicesProvider = Provider.of<ServiceRequestsProvider>(context, listen: false);
    final settingProvider = Provider.of<SettingProvider>(context, listen: false);
    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: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
          primary: itemColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(AppStyle.getBorderRadius(context)),
          ),
        ),
        //padding: EdgeInsets.symmetric(vertical: 8,horizontal: 8),
        onPressed: () {
          onPressed(request);
        },
        child: Row(
          children: [
            //Placeholder(color: onItemColor,fallbackWidth: 80,fallbackHeight: 80,),
            _user.type == UsersTypes.normal_user && request.devicePhotos.isEmpty
                ? SizedBox.shrink()
                : SizedBox(
                    width: 80,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        request.devicePhotos?.isEmpty != false
                            ? SizedBox.shrink()
                            : Column(
                                children: [
                                  SizedBox(
                                    height: 80,
                                    width: 80,
                                    child: ImageLoader(
                                      url: request.devicePhotos.first,
                                      boxFit: BoxFit.cover,
                                    ),
                                  ),
                                  SizedBox(
                                    height: 24,
                                  ),
                                ],
                              ),

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