import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/device_transfer_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/models/device/device_transfer.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/device_transfer/update_device_transfer.dart';
import 'package:test_sa/views/widgets/buttons/app_back_button.dart';
import 'package:test_sa/views/widgets/buttons/app_small_button.dart';
import 'package:test_sa/views/widgets/device_trancfer/device_transfer_info_section.dart';
import 'package:test_sa/views/widgets/loaders/app_loading.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/requests/info_row.dart';

import '../../../models/enums/user_types.dart';

class DeviceTransferDetails extends StatefulWidget {
  final DeviceTransfer model;

  const DeviceTransferDetails({Key key, this.model}) : super(key: key);

  @override
  State<DeviceTransferDetails> createState() => _DeviceTransferDetailsState();
}

class _DeviceTransferDetailsState extends State<DeviceTransferDetails> {
  DeviceTransfer _model;
  bool _isSender = false;
  bool _isReceiver = false;
  UserProvider _userProvider;
  bool _isLoading = false;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    _model = widget.model;
    super.initState();
  }

  Future<void> _getDeviceTransfer() async {
    _model = await Provider.of<DeviceTransferProvider>(context).getRequestById(assetTransferId: widget.model.id);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: SafeArea(
        child: FutureBuilder(
          future: _getDeviceTransfer(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const ALoading();
            } else {
              _userProvider = Provider.of<UserProvider>(context);
              _isSender = _userProvider.user.userID == _model.sender?.userId;
              _isReceiver = _userProvider.user.userID == _model.receiver?.userId;
              return Form(
                key: _formKey,
                child: LoadingManager(
                  isLoading: _isLoading,
                  isFailedLoading: false,
                  stateCode: 200,
                  onRefresh: () async {},
                  child: Column(
                    children: [
                      Container(
                        color: Theme.of(context).colorScheme.primary,
                        padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 4),
                        child: Row(
                          children: [
                            const ABackButton(),
                            Expanded(
                              child: Center(
                                child: Text(
                                  context.translation.details,
                                  style: Theme.of(context).textTheme.headline6.copyWith(color: AColors.white, fontStyle: FontStyle.italic),
                                ),
                              ),
                            ),
                            const SizedBox(width: 58),
                          ],
                        ),
                      ),
                      Expanded(
                        child: SingleChildScrollView(
                          padding: EdgeInsets.all(16 * AppStyle.getScaleFactor(context)),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              RequestInfoRow(
                                title: "Transfer Id",
                                info: _model.title?.toString(),
                              ),
                              RequestInfoRow(
                                title: context.translation.assetName,
                                info: _model.device.assetName,
                              ),
                              RequestInfoRow(
                                title: context.translation.assetSN,
                                info: _model.device.serialNumber,
                              ),
                              RequestInfoRow(
                                title: context.translation.assetNumber,
                                info: _model.device.number,
                              ),
                              RequestInfoRow(
                                title: context.translation.destinationSite,
                                info: _model.device.destSiteName,
                              ),
                              RequestInfoRow(
                                title: context.translation.building,
                                info: _model.device.destBuildingName,
                              ),
                              RequestInfoRow(
                                title: context.translation.floor,
                                info: _model.device.destFloor,
                              ),
                              RequestInfoRow(
                                title: context.translation.department,
                                info: _model.device.destDepartmentName,
                              ),
                              RequestInfoRow(
                                title: context.translation.room,
                                info: _model.device.destRoom,
                              ),
                              RequestInfoRow(
                                title: context.translation.actions,
                                info: "",
                              ),
                              const SizedBox(height: 8),
                              Row(
                                children: [
                                  Expanded(
                                    child: Text("Sender", style: Theme.of(context).textTheme.headline6),
                                  ),
                                  if (_userProvider.user?.type == UsersTypes.engineer)
                                    ASmallButton(
                                      text: context.translation.edit,
                                      onPressed: (_isSender)
                                          ? (_model.sender.status?.name == "Closed"
                                              ? null
                                              : () {
                                                  Navigator.of(context).push(MaterialPageRoute(
                                                      builder: (_) => UpdateDeviceTransfer(
                                                            model: _model,
                                                            isSender: true,
                                                          )));
                                                })
                                          : null,
                                    ),
                                ],
                              ),
                              const SizedBox(height: 12),
                              DeviceTransferInfoSection(
                                info: _model.sender,
                                isSender: true,
                              ),
                              const SizedBox(height: 8),
                              Row(
                                children: [
                                  Expanded(
                                    child: Text("Receiver", style: Theme.of(context).textTheme.headline6),
                                  ),
                                  if (_userProvider.user?.type == UsersTypes.engineer)
                                    ASmallButton(
                                      text: context.translation.edit,
                                      onPressed: (_isReceiver)
                                          ? (_model.receiver.status?.name == "Closed"
                                              ? null
                                              : () {
                                                  Navigator.of(context).push(MaterialPageRoute(
                                                      builder: (_) => UpdateDeviceTransfer(
                                                            model: _model,
                                                            isSender: false,
                                                          )));
                                                })
                                          : null,
                                    ),
                                ],
                              ),
                              const SizedBox(height: 12),
                              DeviceTransferInfoSection(
                                info: _model.receiver,
                                isSender: false,
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            }
          },
        ),
      ),
    );
  }
}
