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/all_requests_and_count_model.dart';
import 'package:test_sa/models/enums/user_types.dart';

import '../../../models/search_all_requests_model.dart';

class AllRequestsProvider extends ChangeNotifier {
  bool isAllLoading = false;
  bool isOpenLoading = false;
  bool isInProgressLoading = false;
  bool isCompleteLoading = false;
  bool isOverdueLoading = false;
  bool isHighPriorityLoading = false;
  bool isCalendarLoading = false;

  int stateCode;

  AllRequestsAndCount allRequestsAndCount;
  AllRequestsAndCount openRequests;
  AllRequestsAndCount inProgressRequests;
  AllRequestsAndCount completedRequests;
  AllRequestsAndCount overdueRequests;
  AllRequestsAndCount highPriorityRequests;
  AllRequestsAndCount calendarRequests;

  void getRequests() {
    getHighPriorityRequests();
    getOverdueRequests();
    getOpenRequests();
    getInProgressRequests();
    getCompletedRequests();
  }

  Future<int> getAllRequests(BuildContext context, {int typeTransaction, SearchAllRequestsModel search}) async {
    if (isAllLoading == true) return -2;
    isAllLoading = true;
    if (allRequestsAndCount == null) notifyListeners();
    Response response;
    try {
      final type = typeTransaction == null
          ? search?.typeTransaction == null || search.typeTransaction.isEmpty
              ? [1, 2, 3, 4]
              : search.typeTransaction
          : [typeTransaction];
      List<int> status = (search?.statuses == null || search.statuses.isEmpty) ? [1, 2, 3, 4] : search.statuses;
      Map<String, dynamic> body = {
        "typeTransaction": type,
        "statusTransaction": status,
        "priority": [0, 1],
        "displayData": [],
        if (search?.requestNumber?.value?.isNotEmpty ?? false) "requestNumber": search.requestNumber.value,
        if (search?.assetName?.value?.isNotEmpty ?? false) "assetName": search.assetName.value,
        if (search?.assetNo?.value?.isNotEmpty ?? false) "assetNumber": search.assetNo.value,
        if (search?.sn?.value?.isNotEmpty ?? false) "assetSerialNo": search.sn.value,
        if (search?.model?.value?.isNotEmpty ?? false) "model": search.model.value,
        if (search?.manufacture?.value?.isNotEmpty ?? false) "manufacturer": search.manufacture.value,
        if (search?.startDate != null) "from": search.startDate.toIso8601String(),
        if (search?.endDate != null) "to": search.endDate.toIso8601String(),
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        allRequestsAndCount = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]);
        notifyListeners();
      } else {
        allRequestsAndCount = null;
      }
      isAllLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isAllLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getCalendarRequests({@required DateTime from, DateTime to}) async {
    if (isCalendarLoading == true) return -2;
    isCalendarLoading = true;
    notifyListeners();
    if (isCalendarLoading == null) notifyListeners();
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [1, 2, 3, 4],
        "priority": [0, 1],
        "displayData": [],
        "from": from.toIso8601String(),
        "to": to?.toIso8601String() ?? from.toIso8601String(),
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        calendarRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]);
        notifyListeners();
      }
      isCalendarLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isCalendarLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getHighPriorityRequests() async {
    if (isHighPriorityLoading == true) return -2;
    isHighPriorityLoading = true;
    if (highPriorityRequests == null) notifyListeners();
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1],
        "statusTransaction": [1, 2, 4],
        "priority": [1],
        "displayData": []
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        highPriorityRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]);
        notifyListeners();
      }
      isHighPriorityLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isHighPriorityLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getOverdueRequests() async {
    if (isOverdueLoading == true) return -2;
    isOverdueLoading = true;
    if (overdueRequests == null) notifyListeners();
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [1, 2, 4],
        "priority": [],
        "displayData": [1]
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
          overdueRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]);
        notifyListeners();
      }
      isOverdueLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isOverdueLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getOpenRequests() async {
    if (isOpenLoading == true) return -2;
    isOpenLoading = true;
    if (openRequests == null) notifyListeners();
    Response response;
    try {
      bool isEngineer = ApiManager.instance.user.type == UsersTypes.engineer;
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [isEngineer ? 2 : 1],
        "priority": [],
        "displayData": []
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        openRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]);
        notifyListeners();
      }
      isOpenLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isOpenLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getInProgressRequests() async {
    if (isInProgressLoading == true) return -2;
    isInProgressLoading = true;
    if (inProgressRequests == null) notifyListeners();
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [2],
        "priority": [],
        "displayData": []
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        inProgressRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]);
        notifyListeners();
      }
      isInProgressLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isInProgressLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getCompletedRequests() async {
    if (isCompleteLoading == true) return -2;
    isCompleteLoading = true;
    if (completedRequests == null) notifyListeners();
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [4],
        "priority": [],
        "displayData": []
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        completedRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]);
        notifyListeners();
      }
      isCompleteLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isCompleteLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

// Future<int> getCloseRequests() async {
//   if (isCloseLoading == true) return -2;
//   isCloseLoading = true;
//   if (closeRequests == null) notifyListeners();
//   Response response;
//   try {
//     Map<String, dynamic> body = {
//       "typeTransaction": [1, 2, 3, 4],
//       "statusTransaction": [4],
//       "priority": [],
//       "displayData": []
//     };
//     response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);
//
//     stateCode = response.statusCode;
//     if (response.statusCode >= 200 && response.statusCode < 300) {
//       closeRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"]);
//       notifyListeners();
//     }
//     isCloseLoading = false;
//     notifyListeners();
//     return response.statusCode;
//   } catch (error) {
//     isCloseLoading = false;
//     stateCode = -1;
//     notifyListeners();
//     return -1;
//   }
// }
}
