import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/models/subtitle.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/images_list.dart';
import 'package:test_sa/views/widgets/loaders/image_loader.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/regular_visits_provider.dart';
import '../../../../controllers/providers/api/user_provider.dart';
import '../../../../models/enums/user_types.dart';

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

  const VisitDetailsPage({Key key, this.visit}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    Subtitle _subtitle = AppLocalization.of(context).subtitle;
    final regularVisitsProvider = Provider.of<RegularVisitsProvider>(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: [
                  ABackButton(),
                  Expanded(
                    child: Center(
                      child: Text(
                        _subtitle.visitInformation,
                        style: Theme.of(context).textTheme.headline6.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: () async {
                        Navigator.of(context).push(MaterialPageRoute(
                            builder: (_) => EditPentry(
                                  visit: visit,
                                  pentry: visit.pentry,
                                )));
                      },
                    ),
                  SizedBox(width: 42)
                ],
              ),
            ),
            Expanded(
              child: ListView(
                padding: EdgeInsets.symmetric(horizontal: 16),
                children: [
                  SizedBox(
                    height: 8,
                  ),
                  MaterialButton(
                    padding: EdgeInsets.zero,
                    onPressed: () {
                      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,
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 8,
                  ),
                  SizedBox(
                    height: 60 * AppStyle.getScaleFactor(context),
                    child: ImagesList(
                      images: visit.images,
                    ),
                  ),
                  SizedBox(
                    height: 8,
                  ),
                  RequestInfoRow(
                    title: _subtitle.code,
                    info: visit.serialNumber,
                  ),
                  RequestInfoRow(
                    title: "Asset Number",
                    info: visit.deviceNumber,
                  ),
                  RequestInfoRow(
                    title: _subtitle.deviceSN,
                    info: visit.deviceSerialNumber,
                  ),
                  RequestInfoRow(
                    title: _subtitle.expectDate,
                    info: visit.expectDate,
                  ),
                  RequestInfoRow(
                    title: _subtitle.actualDate,
                    info: visit.actualDate,
                  ),
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          "${_subtitle.status} :  ",
                          style: Theme.of(context).textTheme.subtitle1.copyWith(fontWeight: FontWeight.bold),
                          textScaleFactor: AppStyle.getScaleFactor(context),
                        ),
                      ),
                      VisitStatusLabel(
                        visit: visit,
                      ),
                    ],
                  ),
                  Divider(
                    color: Theme.of(context).primaryColor,
                  ),
                  RequestInfoRow(
                    title: _subtitle.contactStatus,
                    info: visit.assignTo,
                  ),
                  RequestInfoRow(
                    title: _subtitle.engineerName,
                    info: visit.employName,
                  ),
                  RequestInfoRow(
                    title: _subtitle.hospital,
                    content: visit.hospitalName,
                  ),
                  RequestInfoRow(
                    title: _subtitle.deviceName,
                    content: visit.deviceEnglishName,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
