import 'dart:convert';

import 'package:flutter/widgets.dart';
import 'package:http/http.dart';
import 'package:test_sa/controllers/api_routes/api_manager.dart';
import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/new_models/dashboard_count.dart';
import 'package:test_sa/models/new_models/dashboard_detail.dart' as DD;

class DashBoardProvider extends ChangeNotifier {
  bool isAllCountLoading = false;
  bool isLoading = false;
  bool _isDetailLoading = false;

  int _status = 0;

  int get status => _status;

  set status(int value) {
    _status = value;
    notifyListeners();
  }

  int _currentListIndex = 0;

  int get currentListIndex => _currentListIndex;

  set currentListIndex(int value) {
    _currentListIndex = value;
    notifyListeners();
  }

  int? stateCode;

  DashboardCount? dashboardCount;

  DD.DashboardDetail? _requestDetailList;

  DD.DashboardDetail? get requestDetailList => _requestDetailList;

  List<CategoryTabs> tabs = [];

  void setTabs({required UsersTypes userType, required BuildContext  context}) {
    print('user type i got in method is ${userType}');
    tabs = CategoryTabs.getTabs(userType: userType, context: context);
  }

  set requestDetailList(DD.DashboardDetail? value) {
    _requestDetailList = value;
    notifyListeners();
  }

  final pageItemNumber = 10;
  int pageNum = 1;
  bool nextPage = true;

  void reset() {
    pageNum = 1;
    nextPage = true;
    stateCode = null;
    _requestDetailList = null;
    dashboardCount = null;
  }

  void resetRequestDataList() {
    pageNum = 1;
    nextPage = true;
    stateCode = null;
    _requestDetailList = null;
  }

  void getAllRequests(UsersTypes userTypes) {
    getDashBoardCount(usersType: userTypes);
    // getRequestDetail(usersType: userTypes);
  }

  Future<int> getDashBoardCount({required UsersTypes usersType}) async {
    isAllCountLoading = true;
    notifyListeners();
    String url = '';
    if (usersType == UsersTypes.engineer) {
      url = URLs.engineerDashboardCountUrl;
    } else {
      url = URLs.nurseDashboardCountUrl;
    }
    Response response;
    try {
      response = await ApiManager.instance.postWithOutBody(url);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        dashboardCount = DashboardCount.fromJson(json.decode(response.body));
      }

      isAllCountLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isAllCountLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getWorkOrderById(int id) async {
    isLoading = true;
    notifyListeners();
    Response response;
    try {
      response = await ApiManager.instance.get(URLs.getWorkOrderByIdUrl + "?workOrderId=$id");

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        dashboardCount = DashboardCount.fromJson(json.decode(response.body));
      }

      isAllCountLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isAllCountLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  void getHighPriorityRequests({bool showLoader = true, required UsersTypes usersType}) {
    getRequestDetail(showLoader: showLoader, usersType: usersType, isHighPriority: true);
  }

  void getOverdueRequests({bool showLoader = true, required UsersTypes usersType}) {
    getRequestDetail(showLoader: showLoader, usersType: usersType, isOverdue: true);
  }

  void getOpenRequests({bool showLoader = true, required UsersTypes usersType}) {
    getRequestDetail(showLoader: showLoader, usersType: usersType, status: 1);
  }

  void getInProgressRequests({bool showLoader = true, required UsersTypes usersType}) {
    getRequestDetail(showLoader: showLoader, usersType: usersType, status: 2);
  }

  void getCompletedRequests({bool showLoader = true, required UsersTypes usersType}) {
    getRequestDetail(showLoader: showLoader, usersType: usersType, status: 3);
  }

  void getAcknowledgeRequests({bool showLoader = true, required UsersTypes usersType}) {
    getRequestDetail(showLoader: showLoader, usersType: usersType, status: 3);
  }

  void getClosedRequests({bool showLoader = true, required UsersTypes usersType}) {
    getRequestDetail(showLoader: showLoader, usersType: usersType, status: 4);
  }

  Future<int> getRequestDetail({bool showLoader = true, required UsersTypes usersType, int? status, bool isHighPriority = false, bool isOverdue = false}) async {
    if (showLoader) {
      isDetailLoading = showLoader;
      notifyListeners();
    }

    print('i am called...${usersType}');

    Response response;
    String url = '';
    if (usersType == UsersTypes.engineer) {
      url = URLs.engineerDashboardDetailsUrl;
    } else {
      url = URLs.nurseDashboardDetailsUrl;
    }
    try {
      Map<String, dynamic> body = {"pageNumber": pageNum, "pageSize": pageItemNumber};
      if (status != null) body["statusValue"] = status;
      if (isHighPriority) body["isHighPriority"] = true;
      if (isOverdue) body["isOverdue"] = true;
      response = await ApiManager.instance.post(url, body: body);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (_requestDetailList == null) {
          _requestDetailList = DD.DashboardDetail.fromJson(json.decode(response.body));
        } else {
          _requestDetailList!.data!.addAll(DD.DashboardDetail.fromJson(json.decode(response.body)).data ?? []);
        }
        if (_requestDetailList!.data!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
      } else {
        requestDetailList = null;
      }
      if (showLoader) {
        isDetailLoading = false;
      }
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      if (showLoader) {
        isDetailLoading = false;
      }
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  bool get isDetailLoading => _isDetailLoading;

  set isDetailLoading(bool value) {
    _isDetailLoading = value;
    notifyListeners();
  }
}

class CategoryTabs {
  String label;
  int tag;

  CategoryTabs(this.label, this.tag);

  static List<CategoryTabs> getTabs({required UsersTypes userType, required BuildContext context}) {
    List<CategoryTabs> tabs = [];
    if (userType == UsersTypes.engineer) {
      tabs.add(CategoryTabs('New Request', 1));
      tabs.add(CategoryTabs('In Progress', 2));
      tabs.add(CategoryTabs('Completed', 3));
      return tabs;
    }
    tabs.add(CategoryTabs('Open Request', 1));
    tabs.add(CategoryTabs('In Progress', 2));
    tabs.add(CategoryTabs('Acknowledged', 3));
    tabs.add(CategoryTabs('Rejected', 6));
    return tabs;
  }
}
