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/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/models/service_request/service_request_search.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/widgets/buttons/app_back_button.dart';
import 'package:test_sa/views/widgets/buttons/app_icon_button.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/requests/service_request_list.dart';
import 'package:test_sa/views/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 {
  ServiceRequestsProvider _serviceRequestsProvider;
  UserProvider _userProvider;
  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(hospitalId: _userProvider.user.clientId);
          },
          child: Stack(
            children: [
              Column(
                children: [
                  Container(
                    color: AColors.primaryColor,
                    padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 4),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            ABackButton(),
                            Expanded(
                              child: Center(
                                child: Text(
                                  _subtitle.serviceRequests,
                                  style: Theme.of(context).textTheme.headline6.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(() {});
                                }
                              },
                            ),
                            SizedBox(
                              width: 16,
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                  Expanded(
                    child: ServiceRequestsList(
                      nextPage: _serviceRequestsProvider.nextPage,
                      onLazyLoad: () async {
                        await _serviceRequestsProvider.getRequests(hospitalId: _userProvider.user.clientId);
                      },
                      requests: _serviceRequestsProvider.serviceRequests,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
