import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.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/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/models/device/device_transfer.dart';
import 'package:test_sa/models/device/device_transfer_info.dart';
import 'package:test_sa/models/subtitle.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/loading_manager.dart';
import 'package:test_sa/views/widgets/requests/info_row.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> {

  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 = _userProvider.user.id == widget.model.sender?.userId;
    _isReceiver = _userProvider.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: [
                      const 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 || true)
                              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 || true)
                            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,
                        ),

                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
