import 'dart:math';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_last_calls_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_status_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/lookup.dart';
import 'package:test_sa/models/service_report.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/status/single_status_menu.dart';
class ServiceReportStatusMenu extends StatefulWidget {
  final Function(Lookup) onSelect;
  final ServiceReport report;

  const ServiceReportStatusMenu({Key key, this.onSelect, this.report}) : super(key: key);

  @override
  State<ServiceReportStatusMenu> createState() => _ServiceReportStatusMenuState();
}

class _ServiceReportStatusMenuState extends State<ServiceReportStatusMenu> {
  bool firstTime = true;
  @override
  Widget build(BuildContext context) {
    SettingProvider settingProvider = Provider.of<SettingProvider>(context);
    UserProvider userProvider = Provider.of<UserProvider>(context);
    ServiceReportStatusProvider menuProvider = Provider.of<ServiceReportStatusProvider>(context);
    ServiceReportLastCallsProvider callsLastSituationsProvider = Provider.of<ServiceReportLastCallsProvider>(context);
    if(firstTime){
      callsLastSituationsProvider.reset();

      firstTime = false;
    }
    return LoadingManager(
        isLoading: menuProvider.isLoading == true || callsLastSituationsProvider.isLoading == true,
        isFailedLoading: menuProvider.statuses == null || callsLastSituationsProvider.calls == null,
        stateCode: menuProvider.stateCode == null || callsLastSituationsProvider.stateCode == null ? null:
        max(menuProvider.stateCode ?? 0,callsLastSituationsProvider.stateCode ?? 0),
        onRefresh: () async {
          await callsLastSituationsProvider.getCalls(
              user: userProvider.user,
              host: settingProvider.host,
              serviceStatus: widget.report.status?.id
          );
          if(menuProvider.stateCode == null){
            menuProvider.reset();
            await menuProvider.getTypes(
                user: userProvider.user,
                host: settingProvider.host
            );
            setState(() {});
          }
        },
        child: SingleStatusMenu(
          statuses: menuProvider.statuses,
          initialStatus: widget.report.status,
          onSelect: (status){
            widget.report.callLastSituation = null;
            callsLastSituationsProvider.getCalls(
              user: userProvider.user,
              host: settingProvider.host,
              serviceStatus: status.id
            );

            widget.onSelect(status);
          },
        )
    );
  }
}
