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/service_requests_provider.dart';
import '../../../../controllers/providers/settings/setting_provider.dart';
import '../../../../controllers/providers/user_provider.dart';
import '../../../../models/service_request/service_request_search.dart';
import '../../../../models/subtitle.dart';
import '../../../app_style/colors.dart';
import '../../../widgets/buttons/app_back_button.dart';
import '../../../widgets/buttons/app_icon_button.dart';
import '../../../widgets/loaders/loading_manager.dart';
import '../../../widgets/requests/service_request_list.dart';
import '../../../widgets/search/service_request_search_bar.dart';

class ServiceRequestsPage extends StatefulWidget {
  static final String id = "/service-requests";

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

class _ServiceRequestsPageState extends State<ServiceRequestsPage> with TickerProviderStateMixin {
  late ServiceRequestsProvider _serviceRequestsProvider;
  late UserProvider _userProvider;
  late SettingProvider _settingProvider;
  bool _expandedSearch = false;
  bool _firstTime = true;

  @override
  Widget build(BuildContext context) {
    _serviceRequestsProvider = Provider.of<ServiceRequestsProvider>(context);
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    Subtitle? subtitle = AppLocalization.of(context)?.subtitle;
    if (_firstTime) {
      _serviceRequestsProvider.reset();
      _firstTime = false;
    }

    return Scaffold(
      body: SafeArea(
        child: LoadingManager(
          isLoading: _serviceRequestsProvider.isLoading,
          isFailedLoading: _serviceRequestsProvider.serviceRequests == null,
          stateCode: _serviceRequestsProvider.stateCode,
          onRefresh: () async {
            _serviceRequestsProvider.reset();
            await _serviceRequestsProvider.getRequests(
              user: UserApiClient().user!,
              host: _settingProvider.host ?? "",
              hospitalId: UserApiClient().user!.hospital!.id,
            );
          },
          child: Stack(
            children: [
              Column(
                children: [
                  Container(
                    color: AColors.primaryColor,
                    padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 4),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            const ABackButton(),
                            Expanded(
                              child: Center(
                                child: Text(
                                  subtitle?.serviceRequests ?? '',
                                  style: Theme.of(context).textTheme.titleLarge?.copyWith(
                                        color: AColors.white,
                                        fontStyle: FontStyle.italic,
                                      ),
                                ),
                              ),
                            ),
                            AIconButton(
                              key: ValueKey(_expandedSearch),
                              iconData: Icons.search,
                              color: AColors.secondaryColor,
                              buttonSize: 42,
                              backgroundColor: AColors.white,
                              onPressed: () async {
                                ServiceRequestSearch? temp = await showModalBottomSheet(
                                  context: context,
                                  isScrollControlled: true,
                                  builder: (context) {
                                    return ServiceRequestsSearchDialog(
                                      initialSearchValue: _serviceRequestsProvider.search!,
                                    );
                                  },
                                );
                                if (temp != null) {
                                  _serviceRequestsProvider.search = temp;
                                  _serviceRequestsProvider.reset();
                                  setState(() {});
                                }
                              },
                            ),
                            const SizedBox(width: 16)
                          ],
                        ),
                      ],
                    ),
                  ),
                  Expanded(
                    child: ServiceRequestsList(
                      nextPage: _serviceRequestsProvider.nextPage!,
                      onLazyLoad: () async {
                        await _serviceRequestsProvider.getRequests(
                          user: UserApiClient().user,
                          host: _settingProvider.host ?? "",
                          hospitalId: UserApiClient().user?.hospital?.id,
                        );
                      },
                      requests: _serviceRequestsProvider.serviceRequests ?? [],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
