import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/gas_refill_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/extensions/context_extension.dart';
import 'package:test_sa/views/widgets/gas_refill/gas_refill_list.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';

import '../../../../new_views/common_widgets/default_app_bar.dart';

class TrackGasRefillPage extends StatefulWidget {
  static const String id = "/track-gas-refill";

  const TrackGasRefillPage({Key? key}) : super(key: key);

  @override
  State<TrackGasRefillPage> createState() => _TrackGasRefillPageState();
}

class _TrackGasRefillPageState extends State<TrackGasRefillPage> with TickerProviderStateMixin {
  GasRefillProvider? _gasRefillProvider;
  late UserProvider _userProvider;
  late SettingProvider _settingProvider;
  bool mostRecent = true;

  @override
  Widget build(BuildContext context) {
    if (_gasRefillProvider == null) {
      _gasRefillProvider = Provider.of<GasRefillProvider>(context);
      _gasRefillProvider!.reset();
    }
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);

    return Scaffold(
      appBar: DefaultAppBar(title: context.translation.gasRefillRequest),
      body: SafeArea(
        child: LoadingManager(
          isLoading: _gasRefillProvider!.isLoading,
          isFailedLoading: _gasRefillProvider!.items == null,
          stateCode: _gasRefillProvider!.stateCode,
          onRefresh: () async {
            _gasRefillProvider!.reset();
            await _gasRefillProvider!.getRequests(user: _userProvider.user!, host: _settingProvider.host!, mostRecent: mostRecent);
          },
          child: Stack(
            children: [
              Column(
                children: [
                  Expanded(
                    child: GasRefillList(
                      nextPage: _gasRefillProvider!.nextPage,
                      onLazyLoad: () async {
                        await _gasRefillProvider!.getRequests(user: _userProvider.user!, host: _settingProvider.host!, mostRecent: mostRecent);
                      },
                      items: _gasRefillProvider?.items ?? [],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
