import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/api_routes/api_manager.dart';
import 'package:test_sa/controllers/providers/api/all_requests_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/models/all_requests_and_count_model.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/new_views/pages/land_page/widgets/request_item_view_list.dart';
import 'package:test_sa/views/widgets/loaders/lazy_loading.dart';

class RequestsListPage extends StatelessWidget {
  static const String routeName = "/requests-list-page";
  final int index;

  RequestsListPage(this.index, {Key key}) : super(key: key);

  AllRequestsProvider _provider;

  @override
  Widget build(BuildContext context) {
    bool isEngineer = ApiManager.instance.user.type == UsersTypes.engineer;
    if (_provider == null) {
      _provider = Provider.of<AllRequestsProvider>(context, listen: false);
      _provider.resetRequestsApiData();
      callRequestApis();
    }
    String title = index == 0
        ? context.translation.highPriority
        : index == 1
            ? context.translation.overdue
            : index == 2
                ? (isEngineer ? context.translation.inProgress : context.translation.newR)
                : index == 3
                    ? context.translation.completed
                    : "";

    return Scaffold(
        appBar: DefaultAppBar(title: "$title ${context.translation.requests}"),
        body: Consumer<AllRequestsProvider>(builder: (context, snapshot, _) {
          bool isLoading = index == 0
              ? snapshot.isHighPriorityLoading
              : index == 1
                  ? snapshot.isOverdueLoading
                  : index == 2
                      ? snapshot.isOpenLoading
                      : index == 3
                          ? snapshot.isCompleteLoading
                          : false;

          List<RequestsDetails> list = index == 0
              ? snapshot.highPriorityRequests?.requestsDetails ?? []
              : index == 1
                  ? snapshot.overdueRequests?.requestsDetails?? []
                  : index == 2
                      ? snapshot.openRequests?.requestsDetails?? []
                      : index == 3
                          ? snapshot.completedRequests?.requestsDetails?? []
                          : [];
          return LazyLoading(
            child: RequestItemViewList(list, isLoading),
            nextPage: snapshot.nextPage,
            onLazyLoad: () {
              callRequestApis();
            },
          );

          RequestItemViewList(list, isLoading);
        }));
  }

  void callRequestApis() {
    if (index == 0) {
      _provider.getHighPriorityRequests();
    } else if (index == 1) {
      _provider.getOverdueRequests();
    } else if (index == 2) {
      _provider.getOpenRequests();
    } else {
      _provider.getCompletedRequests();
    }
  }
}
