import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/ppm_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/all_requests_and_count_model.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/views/pages/user/ppm/update_ppm/edit_ppm.dart';
import 'package:test_sa/views/widgets/loaders/app_loading.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';

import '../../../../controllers/providers/api/user_provider.dart';
import '../../../../models/enums/user_types.dart';
import '../../../../models/ppm/ppm.dart';
import '../../../../new_views/app_style/app_color.dart';
import '../../../../new_views/common_widgets/app_filled_button.dart';
import '../../../widgets/requests/request_status.dart';

class PpmDetailsPage extends StatefulWidget {
  static const String id = "/visit-details";
  final RequestsDetails request;

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

  @override
  _PpmDetailsPageState createState() {
    return _PpmDetailsPageState();
  }
}

class _PpmDetailsPageState extends State<PpmDetailsPage> {
  UserProvider? userProvider;

  PpmProvider? ppmProvider;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    userProvider ??= Provider.of<UserProvider>(context, listen: false);
    ppmProvider ??= Provider.of<PpmProvider>(context, listen: false);

    return Scaffold(
      appBar: DefaultAppBar(title: context.translation.preventiveMaintenance),
      body: SafeArea(
        child: FutureBuilder(
            future: ppmProvider!.getPpmById(widget.request.id!),
            builder: (context, snap) {
              if (snap.connectionState == ConnectionState.waiting) {
                return const ALoading();
              } else if (snap.hasData) {
                Ppm ppm = snap.data as Ppm;

                return Column(children: [
                  SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            if (widget.request.priority != null)
                              StatusLabel(
                                label: widget.request.priority,
                                textColor: AppColor.getRequestStatusTextColorByName(context, widget.request.priority!),
                                backgroundColor: AppColor.getRequestStatusColorByName(context, widget.request.priority!),
                              ),
                            if (widget.request.priority != null) 8.width,
                            StatusLabel(
                              label: ppm.visitStatusName,
                              id: ppm.visitStatusId!.toInt(),
                              textColor: AppColor.getRequestStatusTextColorByName(context, ppm.visitStatusName!),
                              backgroundColor: AppColor.getRequestStatusColorByName(context, ppm.visitStatusName!),
                            ),
                            1.width.expanded,
                          ],
                        ),
                        8.height,
                        ppm.assetName!.heading5(context),
                        8.height,
                        '${context.translation.assetNumber}:  ${ppm.assetNumber}'.bodyText(context),
                        '${context.translation.assetSN}: ${ppm.assetSerialNo}'.bodyText(context),
                        '${context.translation.requestNo}: ${ppm.visitCode}'.bodyText(context),
                        const Divider().defaultStyle(context),
                        '${context.translation.expectDate}:  ${ppm.expectedDate?.toAssetDetailsFormat ?? ""}'.bodyText(context),
                        '${context.translation.actualDate}: ${ppm.actualDate?.toAssetDetailsFormat ?? ""}'.bodyText(context),
                        const Divider().defaultStyle(context),
                        '${context.translation.assignedTo}:  ${ppm.assignedToName ?? ""}'.bodyText(context),
                        '${context.translation.engineerName}:  ${ppm.assignedEmployeeName ?? ""}'.bodyText(context),
                        '${context.translation.site}:  ${ppm.siteName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context),
                        '${context.translation.building}:  ${ppm.buildingName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context),
                        '${context.translation.floor}:  ${ppm.floorName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context),
                        '${context.translation.department}:  ${ppm.departmentName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context),
                        '${context.translation.room}:  ${(ppm.roomName ?? "").cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context),
                      ],
                    ).toShadowContainer(context).paddingAll(16),
                  ).expanded,
                  if (userProvider!.user!.type == UsersTypes.engineer && (ppm.visitStatusId != 270 && ppm.visitStatusId != 269))
                    AppFilledButton(
                      onPressed: () async {
                        await Navigator.of(context).push(MaterialPageRoute(builder: (_) => EditPpm(ppm: ppm, details: widget.request)));
                        setState(() {});
                      },
                      label: context.translation.updateRequest,
                    ).paddingAll(16)
                ]);
              }
              return NoDataFound(message: context.translation.noDataFound);
            }),
      ),
    );
  }
}
