import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.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/preventive_maintenance_visits_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/subtitle.dart';
import 'package:test_sa/models/visits/visits_group.dart';
import 'package:test_sa/models/visits/visits_search.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/pages/user/visits/update_visits_group_sheet.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/buttons/app_icon_button.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/search/visits_search_bar.dart';
import 'package:test_sa/views/widgets/visits/visits_list.dart';

class PreventiveMaintenanceVisitsPage extends StatefulWidget {
  static final String id = "/preventive-maintenance-visits";
  @override
  _PreventiveMaintenanceVisitsPageState createState() => _PreventiveMaintenanceVisitsPageState();
}

class _PreventiveMaintenanceVisitsPageState extends State<PreventiveMaintenanceVisitsPage> with TickerProviderStateMixin {
  PreventiveMaintenanceVisitsProvider _visitsProvider;
  UserProvider _userProvider;
  SettingProvider _settingProvider;
  Subtitle _subtitle;

  @override
  Widget build(BuildContext context) {
    _visitsProvider = Provider.of<PreventiveMaintenanceVisitsProvider>(context);
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    _subtitle = AppLocalization.of(context).subtitle;
    return Scaffold(
      body: SafeArea(
        child: LoadingManager(
          isLoading: _visitsProvider.isLoading,
          isFailedLoading: _visitsProvider.visits == null,
          stateCode: _visitsProvider.stateCode,
          onRefresh: () async {
            //_visitsProvider.visitsSearch = VisitsSearch();
            _visitsProvider.reset();
            await _visitsProvider.getVisits(
              user: _userProvider.user,
              host: _settingProvider.host,
              //visitsSearch: _visitsSearch
            );
          },
          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.preventiveMaintenance,
                                  style: Theme.of(context).textTheme.headline6.copyWith(color: AColors.white, fontStyle: FontStyle.italic),
                                ),
                              ),
                            ),
                            AIconButton(
                              iconData: Icons.search,
                              color: AColors.secondaryColor,
                              buttonSize: 42,
                              backgroundColor: AColors.white,
                              onPressed: () async {
                                VisitsSearch _temp = await showModalBottomSheet(
                                    context: context,
                                    isScrollControlled: true,
                                    builder: (context) {
                                      return VisitsSearchDialog(
                                        initialSearchValue: _visitsProvider.visitsSearch,
                                      );
                                    });
                                if (_temp != null) {
                                  _visitsProvider.visitsSearch = _temp;
                                  _visitsProvider.reset();
                                  setState(() {});
                                  await _visitsProvider.getVisits(
                                    user: _userProvider.user,
                                    host: _settingProvider.host,
                                    //visitsSearch: _visitsSearch
                                  );
                                }
                              },
                            ),
                            SizedBox(
                              width: 16,
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                  Visibility(
                    visible: _visitsProvider.visitsSearch.toMap().isNotEmpty,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: AButton(
                        text: _subtitle.clearSearch,
                        onPressed: () {
                          _visitsProvider.visitsSearch = VisitsSearch();
                          _visitsProvider.reset();
                          setState(() {});
                        },
                      ),
                    ),
                  ),
                  Expanded(
                    child: VisitsList(
                      nextPage: _visitsProvider.nextPage,
                      onLazyLoad: () async {
                        await _visitsProvider.getVisits(
                          user: _userProvider.user,
                          host: _settingProvider.host,
                          //visitsSearch: _visitsSearch
                        );
                      },
                      onEditGroup: (visits) async {
                        VisitsGroup _group = await showModalBottomSheet(
                          isScrollControlled: true,
                          context: context,
                          builder: (context) {
                            return UpdateVisitsGroupSheet(
                              visits: visits,
                              title: _subtitle.updatePreventiveMaintenance,
                            );
                          },
                        ) as VisitsGroup;
                        if (_group != null) {
                          showDialog<void>(
                            context: context,
                            barrierDismissible: false,
                            builder: (BuildContext context) {
                              return CupertinoAlertDialog(
                                title: Text(_subtitle.updatingDots),
                                content: Center(child: CircularProgressIndicator()),
                              );
                            },
                          );
                          int status = await _visitsProvider.updateGroupOfVisits(user: _userProvider.user, host: _settingProvider.host, group: _group);
                          Navigator.of(context).pop();
                          if (status >= 200 && status < 300) {
                            Fluttertoast.showToast(
                              msg: _subtitle.preventiveMaintenanceUpdatedSuccessfully,
                              toastLength: Toast.LENGTH_LONG,
                              gravity: ToastGravity.BOTTOM,
                            );
                          } else {
                            Fluttertoast.showToast(
                              msg: HttpStatusManger.getStatusMessage(status: status, subtitle: _subtitle),
                              toastLength: Toast.LENGTH_LONG,
                              gravity: ToastGravity.BOTTOM,
                            );
                          }
                        }
                      },
                      visits: _visitsProvider.visits,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
