import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/api_routes/http_status_manger.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/controllers/providers/api/service_requests_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/controllers/validator/validator.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/device/device.dart';
import 'package:test_sa/models/service_request/service_request.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/widgets/app_text_form_field.dart';
import 'package:test_sa/views/widgets/buttons/app_back_button.dart';
import 'package:test_sa/views/widgets/buttons/app_button.dart';
import 'package:test_sa/views/widgets/equipment/device_button.dart';
import 'package:test_sa/views/widgets/images/multi_image_picker.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/sound/record_sound.dart';
import 'package:test_sa/views/widgets/speech_to_text/speech_to_text.dart';
import 'package:test_sa/views/widgets/status/service_request/service_request_defect_types_mune.dart';
import 'package:test_sa/views/widgets/status/service_request/service_request_priority_mune.dart';
import 'package:test_sa/views/widgets/status/service_request/service_request_through_mune.dart';
import 'package:test_sa/views/widgets/status/service_request/service_request_types_mune.dart';
import 'package:test_sa/views/widgets/titles/app_sub_title.dart';

import '../../../widgets/date_and_time/date_picker.dart';
import '../../../widgets/status/service_request/service_request_first_action.dart';
import '../../../widgets/status/service_request/service_request_loan_availability.dart';

class CreateRequestPage extends StatefulWidget {
  static const String id = "/create-request";
  final ServiceRequest serviceRequest;
  const CreateRequestPage({this.serviceRequest, Key key}) : super(key: key);

  @override
  CreateRequestPageState createState() => CreateRequestPageState();
}

class CreateRequestPageState extends State<CreateRequestPage> {
  double _height;
  UserProvider _userProvider;
  SettingProvider _settingProvider;
  ServiceRequestsProvider _serviceRequestsProvider;
  ServiceRequest _serviceRequest;
  final List<File> _deviceImages = [];
  bool _isLoading = false;
  bool _showDatePicker = false;
  Device _device;
  Subtitle _subtitle;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  TextEditingController _maintenanceController, _commentController;
  DateTime _dateTime;

  @override
  void initState() {
    _maintenanceController = TextEditingController();
    _commentController = TextEditingController();
    if (widget.serviceRequest != null) {
      _serviceRequest = widget.serviceRequest;
      _device = _serviceRequest.device;
      _deviceImages.addAll(_serviceRequest.devicePhotos.map((e) => File(e)).toList());
      _showDatePicker = _serviceRequest.firstAction != null && _serviceRequest.firstAction.name == "Need a visit";
    } else {
      _serviceRequest = ServiceRequest();
    }
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    _height = MediaQuery.of(context).size.height;
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    _serviceRequestsProvider = Provider.of<ServiceRequestsProvider>(context);
    _subtitle = AppLocalization.of(context).subtitle;
    return Scaffold(
      key: _scaffoldKey,
      body: SafeArea(
        child: LoadingManager(
          isLoading: _isLoading,
          isFailedLoading: false,
          stateCode: 200,
          onRefresh: () async {},
          child: Form(
            key: _formKey,
            child: Stack(
              children: [
                ListView(
                  children: [
                    //AppNameBar(),
                    const SizedBox(height: 16),
                    Hero(
                      tag: "logo",
                      child: Image(
                        height: _height / 6,
                        image: const AssetImage("assets/images/logo.png"),
                      ),
                    ),
                    Center(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          widget.serviceRequest == null ? _subtitle.newServiceRequest : _subtitle.updateServiceRequest,
                          style: Theme.of(context).textTheme.headline5.copyWith(color: AColors.cyan, fontWeight: FontWeight.w600),
                        ),
                      ),
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        12.height,
                        _userProvider.user.clientId == null
                            ? const SizedBox.shrink()
                            : ATextFormField(
                                enable: false,
                                initialValue: _userProvider.user.clientName ?? _subtitle.noHospitalFound,
                                hintText: _subtitle.hospital,
                                prefixIconData: FontAwesomeIcons.hospital,
                                style: Theme.of(context).textTheme.subtitle1,
                              ),
                        12.height,
                        _userProvider.user.departmentId == null
                            ? const SizedBox.shrink()
                            : ATextFormField(
                                enable: false,
                                initialValue: _userProvider.user.departmentName ?? _subtitle.noUniteFound,
                                hintText: _subtitle.unite,
                                prefixIconData: FontAwesomeIcons.hospitalUser,
                                style: Theme.of(context).textTheme.subtitle1,
                              ),
                        12.height,
                        DeviceButton(
                          device: _device,
                          onDevicePick: (device) {
                            _device = device;
                            setState(() {});
                          },
                        ),
                        const SizedBox(
                          height: 8,
                        ),
                        const ASubTitle("Priority"),
                        const SizedBox(
                          height: 4,
                        ),
                        ServiceRequestPriorityMenu(
                          initialValue: _serviceRequest.priority,
                          onSelect: (status) {
                            _serviceRequest.priority = status;
                          },
                        ),
                        const SizedBox(
                          height: 8,
                        ),
                        const ASubTitle("Defect Type"),
                        const SizedBox(
                          height: 4,
                        ),
                        ServiceRequestDefectTypesMenu(
                          initialValue: _serviceRequest.defectType,
                          onSelect: (status) {
                            _serviceRequest.defectType = status;
                          },
                        ),
                        12.height,
                        const SizedBox(
                          height: 8,
                        ),
                        const ASubTitle("Type"),
                        const SizedBox(
                          height: 4,
                        ),
                        ServiceRequestTypesMenu(
                          initialValue: _serviceRequest.type,
                          onSelect: (status) {
                            _serviceRequest.type = status;
                          },
                        ),
                        const SizedBox(
                          height: 8,
                        ),
                        const ASubTitle("Through"),
                        const SizedBox(
                          height: 4,
                        ),
                        ServiceRequestedThroughMenu(
                          initialValue: _serviceRequest.requestedThrough,
                          onSelect: (status) {
                            _serviceRequest.requestedThrough = status;
                          },
                        ),
                        if (widget.serviceRequest != null) 12.height,
                        if (widget.serviceRequest != null) const ASubTitle("First Action"),
                        const SizedBox(
                          height: 4,
                        ),
                        if (widget.serviceRequest != null)
                          ServiceRequestedFirstAction(
                            initialValue: _serviceRequest.firstAction,
                            onSelect: (status) {
                              _dateTime = null;
                              _serviceRequest.firstAction = status;
                              _showDatePicker = _serviceRequest.firstAction != null && _serviceRequest.firstAction.name == "Need a visit";
                              _serviceRequestsProvider.notifyListeners();
                            },
                          ),
                        if (_showDatePicker) 12.height,
                        if (_showDatePicker)
                          ADatePicker(
                            date: _dateTime,
                            from: DateTime.now(),
                            onDatePicker: (date) {
                              _dateTime = date;
                              setState(() {});
                            },
                          ),
                        if (widget.serviceRequest != null) 12.height,
                        if (widget.serviceRequest != null) const ASubTitle("Loan Availability"),
                        const SizedBox(
                          height: 4,
                        ),
                        if (widget.serviceRequest != null)
                          ServiceRequestedLoanAvailability(
                            initialValue: _serviceRequest.loanAvailability,
                            onSelect: (status) {
                              _serviceRequest.loanAvailability = status;
                            },
                          ),
                        12.height,
                        if (widget.serviceRequest != null) const ASubTitle("Comments"),
                        const SizedBox(
                          height: 4,
                        ),
                        if (widget.serviceRequest != null)
                          ATextFormField(
                            controller: _commentController,
                            initialValue: _serviceRequest.reviewComment,
                            hintText: _subtitle.comment,
                            style: Theme.of(context).textTheme.titleMedium,
                            textInputType: TextInputType.multiline,
                            onSaved: (value) {
                              _serviceRequest.reviewComment = value;
                            },
                          ),
                        12.height,
                        MultiImagesPicker(
                          label: _subtitle.deviceImages,
                          images: _deviceImages,
                        ),
                        12.height,
                        SpeechToTextButton(controller: _maintenanceController),
                        12.height,
                        ATextFormField(
                          controller: _maintenanceController,
                          initialValue: _serviceRequest.callComments,
                          hintText: _subtitle.maintenanceIssue,
                          prefixIconData: FontAwesomeIcons.triangleExclamation,
                          style: Theme.of(context).textTheme.titleLarge,
                          textInputType: TextInputType.multiline,
                          validator: (value) => Validator.hasValue(value) ? null : _subtitle.maintenanceIssueRequired,
                          onSaved: (value) {
                            _serviceRequest.callComments = value;
                          },
                        ),
                        12.height,
                        RecordSound(onRecord: (audio) {
                          _serviceRequest.audio = audio;
                        }),
                        12.height,
                        if (widget.serviceRequest != null)
                          ATextFormField(
                            controller: _commentController,
                            initialValue: _serviceRequest.comments,
                            hintText: _subtitle.comment,
                            style: Theme.of(context).textTheme.titleMedium,
                            textInputType: TextInputType.multiline,
                            onSaved: (value) {
                              _serviceRequest.comments = value;
                            },
                          ),
                      ],
                    ).paddingOnly(left: 20, right: 20),
                    Padding(
                      padding: const EdgeInsets.all(20.0),
                      child: AButton(
                        text: widget.serviceRequest == null ? _subtitle.submit : _subtitle.update,
                        onPressed: () async {
                          if (!_formKey.currentState.validate()) return;
                          if (_device?.id == null) {
                            Fluttertoast.showToast(msg: _subtitle.pickDevice);
                            return;
                          }
                          if (_serviceRequest.firstAction.name == "Need a visit" && _dateTime == null) {
                            Fluttertoast.showToast(msg: _subtitle.noDateFound);
                            return;
                          }
                          _formKey.currentState.save();
                          _serviceRequest.deviceId = _device?.id;
                          _isLoading = true;
                          setState(() {});
                          _serviceRequest.devicePhotos = _deviceImages.map((e) => "${e.path.split("/").last}|${base64Encode(e.readAsBytesSync())}").toList();
                          if (_serviceRequest.audio != null) {
                            final file = File(_serviceRequest.audio);
                            _serviceRequest.audio = "${file.path.split("/").last}|${base64Encode(file.readAsBytesSync())}";
                          }
                          int status = 0;
                          if (widget.serviceRequest == null) {
                            status = await _serviceRequestsProvider.createRequest(
                              user: _userProvider.user,
                              host: _settingProvider.host,
                              serviceRequest: _serviceRequest,
                            );
                          } else {
                            status = await _serviceRequestsProvider.updateDate(
                              user: _userProvider.user,
                              host: _settingProvider.host,
                              request: _serviceRequest,
                              date: _dateTime,
                            );
                          }
                          _isLoading = false;
                          setState(() {});
                          if (status >= 200 && status < 300) {
                            Fluttertoast.showToast(
                              msg: _subtitle.requestCompleteSuccessfully,
                            );
                            Navigator.of(context).pop();
                          } else {
                            String errorMessage = HttpStatusManger.getStatusMessage(status: status, subtitle: _subtitle);
                            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                              content: Text(errorMessage),
                            ));
                          }
                        },
                      ),
                    ),
                  ],
                ),
                const ABackButton(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
