import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/visits/visit.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/visits/pantry/edit_pentry.dart';
import 'package:test_sa/views/widgets/buttons/app_back_button.dart';
import 'package:test_sa/views/widgets/buttons/app_icon_button.dart';
import 'package:test_sa/views/widgets/images/files_list.dart';
import 'package:test_sa/views/widgets/images/images_list.dart';
import 'package:test_sa/views/widgets/requests/info_row.dart';
import 'package:test_sa/views/widgets/visits/visit_status.dart';

import '../../../../controllers/providers/api/user_provider.dart';
import '../../../../models/enums/user_types.dart';
import '../../../widgets/loaders/image_loader.dart';

class VisitDetailsPage extends StatelessWidget {
  static const String id = "/visit-details";
  final Visit visit;

  const VisitDetailsPage({Key key, this.visit}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    UserProvider userProvider = Provider.of<UserProvider>(context);

    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Container(
              color: AColors.primaryColor,
              padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 4),
              child: Row(
                children: [
                  const ABackButton(),
                  Expanded(
                    child: Center(
                      child: Text(
                        context.translation.visitInformation,
                        style: Theme.of(context).textTheme.titleLarge.copyWith(color: AColors.white, fontStyle: FontStyle.italic),
                      ),
                    ),
                  ),
                  if (userProvider?.user?.type == UsersTypes.engineer)
                    AIconButton(
                      iconData: Icons.edit,
                      color: AColors.white,
                      buttonSize: 42,
                      backgroundColor: AColors.green,
                      onPressed: visit?.status?.name == "Closed" || visit?.status?.name == "Complete"
                          ? null
                          : () async {
                              Navigator.of(context).push(
                                MaterialPageRoute(
                                  builder: (_) => EditPentry(
                                    visit: visit,
                                    pentry: visit.pentry,
                                  ),
                                ),
                              );
                            },
                    ),
                  const SizedBox(width: 42)
                ],
              ),
            ),
            Expanded(
              child: ListView(
                padding: const EdgeInsets.symmetric(horizontal: 16),
                children: [
                  const SizedBox(height: 8),
                  // MaterialButton(
                  //   padding: EdgeInsets.zero,
                  //   onPressed: () {
                  //     if ((visit.images?.isNotEmpty ?? false) && (visit.images?.first?.isNotEmpty ?? false)) {
                  //       Navigator.of(context).push(
                  //         MaterialPageRoute(
                  //           builder: (_) => Scaffold(
                  //             body: InteractiveViewer(
                  //               child: Center(
                  //                 child: ImageLoader(
                  //                   url: visit.images.first,
                  //                   boxFit: BoxFit.contain,
                  //                 ),
                  //               ),
                  //             ),
                  //           ),
                  //         ),
                  //       );
                  //     }
                  //   },
                  //   child: SizedBox(
                  //     height: 140 * AppStyle.getScaleFactor(context),
                  //     width: MediaQuery.of(context).size.width,
                  //     child: ImageLoader(
                  //       url: visit.images.isEmpty ? " " : visit.images.first,
                  //       boxFit: BoxFit.cover,
                  //     ),
                  //   ),
                  // ),
                  // const SizedBox(
                  //   height: 8,
                  // ),
                  if (visit?.images?.isNotEmpty ?? false)
                    SizedBox(
                      height: 60 * AppStyle.getScaleFactor(context),
                      child: FilesList(
                        images: visit.images,
                      ),
                    ).paddingOnly(top: 4, bottom: 4),
                  const SizedBox(
                    height: 8,
                  ),
                  RequestInfoRow(
                    title: context.translation.code,
                    info: visit.serialNumber,
                  ),
                  RequestInfoRow(
                    title: "Asset Number",
                    info: visit.deviceNumber,
                  ),
                  RequestInfoRow(
                    title: context.translation.deviceName,
                    content: visit.deviceEnglishName,
                  ),
                  RequestInfoRow(
                    title: context.translation.deviceSN,
                    info: visit.deviceSerialNumber,
                  ),
                  RequestInfoRow(
                    title: context.translation.expectDate,
                    info: visit.expectDate,
                  ),
                  RequestInfoRow(
                    title: context.translation.actualDate,
                    info: visit.actualDate,
                  ),
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          "${context.translation.status} :  ",
                          style: Theme.of(context).textTheme.titleMedium.copyWith(fontWeight: FontWeight.bold),
                          textScaleFactor: AppStyle.getScaleFactor(context),
                        ),
                      ),
                      VisitStatusLabel(
                        visit: visit,
                      ),
                    ],
                  ),
                  Divider(
                    color: Theme.of(context).primaryColor,
                  ),
                  RequestInfoRow(
                    title: context.translation.contactStatus,
                    info: visit.assignTo,
                  ),
                  RequestInfoRow(
                    title: context.translation.engineerName,
                    info: visit.employName,
                  ),
                  RequestInfoRow(
                    title: context.translation.hospital,
                    content: visit.hospitalName,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
