import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../api/login_api_client.dart';
import '../../../controllers/localization/localization.dart';
import '../../../controllers/providers/api/device_transfer_provider.dart';
import '../../../controllers/providers/settings/setting_provider.dart';
import '../../../controllers/providers/user_provider.dart';
import '../../../models/device/device_transfer.dart';
import '../../../models/device/device_transfer_info.dart';
import '../../../models/subtitle.dart';
import '../../app_style/colors.dart';
import '../../app_style/sizing.dart';
import '../../widgets/buttons/app_back_button.dart';
import '../../widgets/buttons/app_small_button.dart';
import '../../widgets/device_trancfer/device_transfer_info_section.dart';
import '../../widgets/loaders/loading_manager.dart';
import '../../widgets/requests/info_row.dart';
import 'update_device_transfer.dart';

class DeviceTransferDetails extends StatefulWidget {
  final DeviceTransfer model;
  const DeviceTransferDetails({Key? key, required this.model}) : super(key: key);

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

class _DeviceTransferDetailsState extends State<DeviceTransferDetails> {
  final DeviceTransferInfo _model = DeviceTransferInfo();
  bool _isSender = false;
  bool _isReceiver = false;
  UserProvider? _userProvider;
  SettingProvider? _settingProvider;
  DeviceTransferProvider? _deviceTransferProvider;
  bool _isLoading = false;
  Subtitle? _subtitle;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

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

  @override
  Widget build(BuildContext context) {
    _subtitle = AppLocalization.of(context)?.subtitle;
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    _deviceTransferProvider = Provider.of<DeviceTransferProvider>(context);
    _isSender = UserApiClient().user?.id == widget.model.sender?.userId;
    _isReceiver = UserApiClient().user?.id == widget.model.receiver?.userId;
    return Scaffold(
      key: _scaffoldKey,
      body: SafeArea(
        child: 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: [
                      ABackButton(),
                      Expanded(
                        child: Center(
                          child: Text(
                            _subtitle?.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: _subtitle!.title,
                          info: widget.model.title,
                        ),
                        RequestInfoRow(
                          title: _subtitle?.device,
                          info: widget.model.device?.serialNumber,
                        ),
                        const SizedBox(height: 8),
                        Row(
                          children: [
                            Expanded(
                              child: Text("Sender", style: Theme.of(context).textTheme.headline6),
                            ),
                            if (_isSender)
                              ASmallButton(
                                text: _subtitle?.edit ?? "",
                                onPressed: () {
                                  Navigator.of(context).push(MaterialPageRoute(
                                      builder: (_) => UpdateDeviceTransfer(
                                            model: widget.model,
                                            isSender: _isSender,
                                          )));
                                },
                              ),
                          ],
                        ),
                        const SizedBox(height: 12),
                        DeviceTransferInfoSection(
                          info: widget.model.sender!,
                        ),
                        const SizedBox(height: 8),
                        Row(
                          children: [
                            Expanded(
                              child: Text("Receiver", style: Theme.of(context).textTheme.headline6),
                            ),
                            if (_isReceiver)
                              ASmallButton(
                                text: _subtitle?.edit ?? "",
                                onPressed: () {
                                  Navigator.of(context).push(MaterialPageRoute(
                                      builder: (_) => UpdateDeviceTransfer(
                                            model: widget.model,
                                            isSender: _isSender,
                                          )));
                                },
                              ),
                          ],
                        ),
                        const SizedBox(height: 12),
                        DeviceTransferInfoSection(
                          info: widget.model.receiver!,
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
