import 'dart:convert';
import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:http/http.dart';
import 'package:test_sa/controllers/api_routes/api_manager.dart';
import 'package:test_sa/controllers/api_routes/http_status_manger.dart';
import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/models/call_request_for_work_order_model.dart';
import 'package:test_sa/models/issue.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/service_report.dart';
import 'package:test_sa/models/service_request/service_request.dart';
import 'package:test_sa/models/service_request/service_request_search.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:test_sa/models/timer_model.dart';

import '../../../models/service_request/search_work_order.dart';
import '../../../models/service_request/sub_work_order_details.dart';
import '../../../models/user.dart';

class ServiceRequestsProvider extends ChangeNotifier {
  // number of items call in each request
  final pageItemNumber = 10;

  //reset provider data
  void reset() {
    workOrders = [];
    serviceRequests = null;
    nextPage = true;
    stateCode = null;
  }

  // state code of current request to defied error message
  // like 400 customer request failed
  // 500 service not available
  int stateCode;

  // true if there is next page in product list and false if not
  bool nextPage = true;

  // list of user requests
  List<ServiceRequest> serviceRequests;
  List<SearchWorkOrder> workOrders = [];

  // when requests in-process _loading = true
  // done _loading = true
  // failed _loading = false
  bool isLoading;

  ServiceRequestSearch search = ServiceRequestSearch();

  /// return -2 if request in progress
  /// return -1 if error happen when sending request
  /// return state code if request complete may be 200, 404 or 403
  /// for more details check http state manager
  /// lib\controllers\http_status_manger\http_status_manger.dart
  Future<int> getRequests({@required int hospitalId}) async {
    if (isLoading == true) return -2;
    isLoading = true;
    if (serviceRequests?.isEmpty ?? false) notifyListeners();
    Response response;
    try {
      Map<String, dynamic> body = {};
      body.addAll(search.toMap());
      body["pageNumber"] = (serviceRequests?.length ?? 0) ~/ pageItemNumber + 1;
      body["pageSize"] = pageItemNumber;

      response = await ApiManager.instance.post(
        URLs.getServiceRequests,
        body: body,
      );

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List requestsListJson = json.decode(response.body)["data"];
        List<ServiceRequest> serviceRequestsPage = requestsListJson.map((request) => ServiceRequest.fromJson(request)).toList();
        serviceRequests ??= [];
        serviceRequests.addAll(serviceRequestsPage);
        notifyListeners();
        if (serviceRequestsPage.length == pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
      }
      isLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<ServiceRequest> getSingleServiceRequest({
    @required String requestId,
    @required String host,
    @required User user,
    @required AppLocalizations subtitle,
  }) async {
    String userData = '';
    if (user != null) {
      userData += "&uid=" + user.id;
      userData += "&token=" + user.token;
    }

    Response response;
    try {
      response = await get(Uri.parse(
        host +
            URLs.getSingleServiceRequest +
            '?call_nid=$requestId'
                '$userData',
      ));
    } catch (error) {
      throw (HttpStatusManger.getStatusMessage(status: -1, subtitle: subtitle));
    }

    // If the call to the server was successful, parse the JSON.
    if (response.statusCode >= 200 && response.statusCode < 300) {
      // If the call to the server was successful, parse the JSON.
      List jsonList = json.decode(utf8.decode(response.bodyBytes).replaceAll("\\", ""));
      List<ServiceRequest> _requests = jsonList.map((i) => ServiceRequest.fromJson(i)).toList();
      return _requests[0];
    } else {
      throw (HttpStatusManger.getStatusMessage(status: response.statusCode, subtitle: subtitle));
    }
  }

  Future<int> createRequest({
    @required String host,
    @required User user,
    @required ServiceRequest serviceRequest,
  }) async {
    var body = {
      "id": 0,
      "calNo": "",
      "callCreatedBy": {"id": user.userID, "name": user.userName ?? ""},
      "assets": serviceRequest.deviceId == null ? [] : [serviceRequest.deviceId],
      "requestedDate": DateTime.now().toIso8601String(),
      "requestedTime": DateTime.now().toIso8601String(),
      "client": user.clientId,
      "callComments": serviceRequest.callComments,
      if (serviceRequest.devicePhotos.isNotEmpty) "attachmentsCallRequest": serviceRequest.devicePhotos.map((e) => {"name": e}).toList(),
      "priority": serviceRequest.priority?.toMap(),
      "defectType": serviceRequest.defectType?.toMap(),
      "typeofRequest": serviceRequest.type?.toMap(),
      "requestedThrough": serviceRequest.requestedThrough?.toMap(),
      "reviewComment": null,
      if (serviceRequest.audio != null) "voiceNote": serviceRequest.audio,
      "callSiteContactPerson": [
        {
          "id": 0,
          // "employeeCode": user.email,
          "name": user.userName,
          "telephone": user.phoneNumber,
          // "job": "",
          "email": user.email,
          // "land": "",
          "contactUserId": user.userID,
        },
      ],
      "noofFollowup": 0,
      "status": null,
      "callLastSituation": null,
      "firstAction": null,
      "loanAvailablity": null,
      "comments": null,
      "firstActionDate": null,
      "visitDate": null,
      "callReview": null,
    };
    if (serviceRequest.audio != null) {
      body["voiceNote"] = serviceRequest.audio;
    }
    Response response;
    //try {
    response = await ApiManager.instance.post(URLs.createRequest, body: body);
    stateCode = response.statusCode;
    if (response.statusCode >= 200 && response.statusCode < 300) {
      if (serviceRequests != null) {
        var data = json.decode(utf8.decode(response.bodyBytes));
        if (data is List) {
          serviceRequests.insert(0, ServiceRequest.fromJson(data[0]));
        } else {
          if (data["data"] != null && data["data"] == true) {
           // serviceRequests.insert(0, ServiceRequest.fromJson(data[0]));
          }
        }
      }
      notifyListeners();
    }
    return response.statusCode;
    // } catch (error) {
    //   return -1;
    // }
  }

  Future<int> createIssueReport({
    @required String host,
    @required User user,
    @required Issue issue,
  }) async {
    Response response;
    Map<String, String> body = issue.toMap();
    body["uid"] = user.id;
    body["token"] = user.token;
    try {
      response = await post(
        Uri.parse(host + URLs.createReport),
        body: body,
      );

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {}
      return response.statusCode;
    } catch (error) {
      return -1;
    }
  }

  Future<int> updateDate({
    @required String host,
    @required User user,
    @required ServiceRequest request,
    DateTime date,
  }) async {
    Map<String, dynamic> serviceRequest = await getServiceRequestById(requestId: request.id) ?? "";
    final List callSiteContacts = (serviceRequest['callSiteContactPerson'] as List);
    final Map<String, dynamic> callSiteContactPerson = callSiteContacts.isEmpty ? {} : callSiteContacts[0];
    Response response;
    var body = {
      "id": request.id,
      "callNo": serviceRequest['callNo'],
      "callCreatedBy": serviceRequest['callCreatedBy'],
      "requestedDate": request.date ?? "",
      "requestedTime": request.date ?? "",
      "priority": request.priority?.toMap(),
      "defectType": request.defectType?.toMap(),
      "typeofRequest": request.type?.toMap(),
      "requestedThrough": request.requestedThrough?.toMap(),
      "voiceNote": request.audio,
      "assets": request.deviceId == null ? [] : [request.deviceId],
      "attachmentsCallRequest": request.devicePhotos?.map((e) => {"name": e})?.toList(),
      "assignedEmployee": {
        "id": request.engineerId,
        "name": request.engineerName,
      },
      "callSiteContactPerson": [
        {
          "id": callSiteContactPerson['id'] ?? 0,
          "employeeCode": callSiteContactPerson['employeeCode'],
          "name": callSiteContactPerson['name'] ?? user.userName,
          "telephone": callSiteContactPerson['telephone'] ?? user.phoneNumber,
          "job": callSiteContactPerson['job'],
          "email": callSiteContactPerson['email'] ?? user.email,
          "land": callSiteContactPerson['land'],
          "contactUserId": user.userID,
        },
      ],
      "callComments": request.callComments,
      "noofFollowup": 0,
      "status": request.statusId == null
          ? null
          : {
              "id": request.statusId,
              "name": request.statusLabel,
              "value": request.statusValue,
            },
      "callLastSituation": null,
      "firstAction": request.firstAction?.toMap(),
      "loanAvailablity": request.loanAvailability?.toMap(),
      "comments": request.reviewComment,
      "firstActionDate": date?.toIso8601String(),
      "visitDate": date?.toIso8601String() ?? "",
      "callReview": null,
      "reviewComment": null,
    };
    try {
      response = await ApiManager.instance.put(
        URLs.updateRequestDate,
        body: body,
      );
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // request.engineerName = employee.name;
        notifyListeners();
      }
      return response.statusCode;
    } catch (error) {
      return -1;
    }
  }

  Future<int> createServiceReport({
    @required String host,
    @required User user,
    @required ServiceReport report,
    @required ServiceRequest request,
  }) async {
    Response response;
    try {
      Map<String, dynamic> body = {
        "id": 0,
        "parentWOId": null,
        "workOrderNo": "",
        "workOrderYear": null,
        "workOrderSequennce": null,
        "callRequest": {
          "id": request.id,
        },
        "assetType": report.assetType?.toMap(),
        "assignedEmployee": {"id": report.engineer?.id, "name": report.engineer?.name ?? ""},
        "visitDate": report.returnToService?.toIso8601String() ?? "",
        // "assistantEmployees": [
        //   {"id": report.engineer.id, "name": report.engineer.name},
        // ],
        "supplier": null,
        "vendorTicketNumber": null,
        "contactPersonWorkOrders": [
          {
            "id": 0,
            // "employeeCode": "",
            "name": user.userName,
            "telephone": user.phoneNumber,
            // "job": "",
            "email": user.email,
            // "land": "",
            "contactUserId": user.userID,
          }
        ],
        "calllastSituation": report.callLastSituation?.toMap(),
        "currentSituation": null,
        "repairLocation": report.repairLocation?.toMap(),
        "reason": report.reason?.toMap(),
        "startofWorkTime": report.timer?.startAt?.toIso8601String() ?? "",
        "endofWorkTime": report.timer?.endAt?.toIso8601String() ?? "",
        "workingHours": report?.workingHours,
        "travelingHours": report.travelingHours,
        "travelingExpenses": report.travelingExpense ?? 0,
        if (report.faultDescription != null) "faultDescription": report.faultDescription.toJson(),
        "sparePartsWorkOrders": report.parts
            ?.map(
              (p) => {
                "id": p.reportPartID ?? 0,
                "sparePart": {"id": p.id, "partNo": p.partNo, "partName": p.partName},
                "qty": p.quantity
              },
            )
            ?.toList(),
        "reviewComment": "",
        "comment": report.comment,
        "attachmentsWorkOrder": request.devicePhotos?.map((e) => {"name": e})?.toList(),
        "equipmentStatus": report.equipmentStatus?.toMap(),
        "suppEngineerWorkOrders": null,
        "engSignature": report.signatureEngineer,
        "nurseSignature": report.signatureNurse,
        "woParentDto": null,
      };
      // body["uid"] = user.id;
      // body["token"] = user.token;
      response = await ApiManager.instance.post(URLs.createServiceReport, body: body);
      // response = await post(
      //     Uri.parse(
      //   host+URLs.createServiceReport),
      //   body: body,
      // );

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        reset();
        notifyListeners();
      }
      return response.statusCode;
    } catch (error) {
      print(error);
      return -1;
    }
  }

  Future<int> createSubWorkOrder({
    @required SearchWorkOrder workOrder,
  }) async {
    Response response;
    try {
      Map<String, dynamic> body = workOrder.toJson();
      response = await ApiManager.instance.post(URLs.createServiceReport, body: body);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        reset();
        notifyListeners();
      }
      return response.statusCode;
    } catch (error) {
      print(error);
      return -1;
    }
  }

  CallRequestForWorkOrder callRequestForWorkOrder;

  Future<CallRequestForWorkOrder> getCallRequestForWorkOrder({String callId}) async {
    Response response;
    try {
      response = await ApiManager.instance.get(
        URLs.getCallRequestForWorkOrder + "?callId=$callId",
      );
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        Map<String, dynamic> listJson = json.decode(response.body)["data"];
        log(listJson?.toString());
        callRequestForWorkOrder = CallRequestForWorkOrder.fromJson(listJson);
      }

      notifyListeners();
      return callRequestForWorkOrder;
    } catch (error) {
      print(error);
      return null;
    }
  }

  Future<List<SearchWorkOrder>> searchWorkOrders({@required String callId}) async {
    Response response;

    try {
      var body = {
        "pageSize": pageItemNumber,
        "pageNumber": ((workOrders?.length ?? 0) ~/ pageItemNumber) + 1,
        "callId": callId,
      };
      response = await ApiManager.instance.post(URLs.searchWorkOrders, body: body);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List workOrdersJson = json.decode(response.body)["data"];
        workOrders = workOrdersJson.map<SearchWorkOrder>((request) => SearchWorkOrder.fromJson(request)).toList();
        if (workOrders.length == pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
      }
      return workOrders;
    } catch (e) {
      return [];
    }
  }

  Future<SubWorkOrderDetails> getSubWorkOrderDetails({@required String parentId}) async {
    Response response;
    try {
      response = await ApiManager.instance.get(URLs.getSubWorkerInfoAccordingToPreviousStep + "?parentId=$parentId");
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        final workOrderJson = json.decode(response.body)["data"];
        return SubWorkOrderDetails.fromJson(workOrderJson);
      }
      return null;
    } catch (e) {
      print("getSubWorkOrderDetails [error] : $e");
      return null;
    }
  }

  Future<int> createDuplicatedReport({
    @required String host,
    @required User user,
    @required ServiceRequest request,
  }) async {
    Response response;
    String userData = '';
    if (user != null) {
      userData += "&uid=" + user.id;
      userData += "&token=" + user.token;
    }

    try {
      response = await get(
        Uri.parse(host + URLs.createDuplicatedReport + "?nid=${request.id}" + userData),
      );

      stateCode = response.statusCode;

      if (response.statusCode >= 200 && response.statusCode < 300) {
        reset();
        notifyListeners();
      }
      return response.statusCode;
    } catch (error) {
      return -1;
    }
  }

  Future<int> updateServiceReport({
    @required ServiceReport report,
    String host,
    User user,
    ServiceRequest request,
  }) async {
    Response response;
    //Map<String,dynamic> body = report.toMap(request);
    // body["uid"] = user.id;
    // body["token"] = user.token;
    // body["job_id"] = request.id;
    // body["report_id"] = request.reportID;
    // try {
    Map<String, dynamic> body = report.toMap(request);
    // body["uid"] = user.id;
    // body["token"] = user.token;
    response = await ApiManager.instance.put(URLs.updateServiceReport, body: body);
    // response = await post(
    //   Uri.parse(
    //       host+URLs.updateServiceReport),
    //   body: body,
    // );
    // stateCode = response.statusCode;

    if (response.statusCode >= 200 && response.statusCode < 300) {
      reset();
      notifyListeners();
    }
    return response.statusCode;
    // } catch (error) {
    //   print(error);
    //   return -1;
    // }
  }

  Future<int> updateWorkOrderDetails({@required SearchWorkOrder workOrder}) async {
    Response response;
    //Map<String,dynamic> body = report.toMap(request);
    // body["uid"] = user.id;
    // body["token"] = user.token;
    // body["job_id"] = request.id;
    // body["report_id"] = request.reportID;
    try {
      Map<String, dynamic> body = workOrder.toJson();
      // body["uid"] = user.id;
      // body["token"] = user.token;
      response = await ApiManager.instance.put(URLs.updateServiceReport, body: body);
      // response = await post(
      //   Uri.parse(
      //       host+URLs.updateServiceReport),
      //   body: body,
      // );
      // stateCode = response.statusCode;

      if (response.statusCode >= 200 && response.statusCode < 300) {
        reset();
        notifyListeners();
      }
      return response.statusCode;
    } catch (error) {
      return -1;
    }
  }

  Future<int> updateServiceReportTimer({
    @required String host,
    @required User user,
    @required TimerModel timer,
    @required ServiceRequest request,
  }) async {
    Response response;
    Map<String, dynamic> body = {};
    body["uid"] = user.id;
    body["token"] = user.token;
    body["job_id"] = request.id;
    body["start_time"] = (timer.startAt.millisecondsSinceEpoch / 1000).toStringAsFixed(0);
    body["end_time"] = (timer.endAt.millisecondsSinceEpoch / 1000).toStringAsFixed(0);
    body["working_hours"] = (timer.durationInSecond / 60 / 60).toStringAsFixed(5);
    body["report_id"] = request.reportID;
    try {
      response = await post(
        Uri.parse(host + URLs.updateServiceReport),
        body: body,
      );
      //stateCode = response.statusCode;

      if (response.statusCode >= 200 && response.statusCode < 300) {
        // reset();
        // notifyListeners();
      }
      return response.statusCode;
    } catch (error) {
      return -1;
    }
  }

  Future<ServiceReport> getSingleServiceReport({
    @required int reportId,
    @required String host,
    @required User user,
    @required AppLocalizations subtitle,
  }) async {
    Response response;
    try {
      response = await ApiManager.instance.get(
        URLs.getServiceReport + "?workOrderId=$reportId",
      );
    } catch (error) {
      throw (HttpStatusManger.getStatusMessage(status: -1, subtitle: subtitle));
    }

    // If the call to the server was successful, parse the JSON.
    if (response.statusCode >= 200 && response.statusCode < 300) {
      // If the call to the server was successful, parse the JSON.
      return ServiceReport.fromJson(json.decode(response.body)["data"], reportId);
    } else {
      throw (HttpStatusManger.getStatusMessage(status: response.statusCode, subtitle: subtitle));
    }
  }

  Future<ServiceRequest> getServiceRequestObjectById({@required String requestId}) async {
    try {
      Response response = await ApiManager.instance.get(URLs.getServiceRequestById + "?callRequestId=$requestId");
      if (response.statusCode >= 200 && response.statusCode < 300) {
        return ServiceRequest.fromJson(json.decode(response.body)["data"]);
      } else {
        return null;
      }
    } catch (error) {
      return null;
    }
  }

  Future<Map<String, dynamic>> getServiceRequestById({@required String requestId}) async {
    Response response;
    try {
      response = await ApiManager.instance.get(
        URLs.getServiceRequestById + "?callRequestId=$requestId",
      );
    } catch (error) {
      print(error);
      return {};
    }

    // If the call to the server was successful, parse the JSON.
    if (response.statusCode >= 200 && response.statusCode < 300) {
      // If the call to the server was successful, parse the JSON.
      return {
        "callNo": json.decode(response.body)["data"]["callNo"],
        "callCreatedBy": json.decode(response.body)["data"]["callCreatedBy"],
        "callSiteContactPerson": json.decode(response.body)["data"]["callSiteContactPerson"],
        "firstActionDate": json.decode(response.body)["data"]["firstActionDate"],
      };
    } else {
      return {};
    }
  }

  Future<List<SearchWorkOrder>> searchForWorkOrders(
    SearchWorkOrder search,
    String callerId,
    Lookup dateOperator,
    String site,
  ) async {
    Response response;
    try {
      var body = {
        "pageSize": pageItemNumber,
        "pageNumber": ((workOrders?.length ?? 0) ~/ pageItemNumber) + 1,
        if (callerId?.isNotEmpty ?? false) "callId": callerId,
        if (search.assetType != null) "assetSerialNo": search.assetType?.name,
        if (search.workOrderNo != null) "workOrderNo": search.workOrderNo,
        if (search.id != null) "workOrderNo": search.id?.toString(),
        if (search.calllastSituation != null) "callslastSituationWO": search.calllastSituation.toMap(),
        if (search.assignedEmployee != null)
          "assignedEmployees": [
            search.assignedEmployee.id,
          ],
        // "statusWO": [],
        if (site?.isNotEmpty ?? false) "site": site,
        if (search.visitDate != null && dateOperator != null) "visitDateSymbol": dateOperator?.toMap(),
        if (search.visitDate != null && dateOperator != null) "visitDateFrom": search.visitDate,
      };
      print(body);
      response = await ApiManager.instance.post(URLs.searchWorkOrders, body: body);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List workOrdersJson = json.decode(response.body)["data"];
        workOrders ??= [];
        workOrders.addAll(workOrdersJson.map<SearchWorkOrder>((request) => SearchWorkOrder.fromJson(request)).toList());
        if (workOrders.length == pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
      }
      return workOrders;
    } catch (e) {
      print(e);
      return [];
    }
  }
}
