import 'dart:io';

import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/feedback/COC_items.dart';
import 'package:diplomaticquarterapp/core/model/feedback/request_insert_coc_item.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/models/Appointments/AppoimentAllHistoryResultList.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';

class FeedbackService extends BaseService {
  List<COCItem> cOCItemList = List();
  RequestInsertCOCItem _requestInsertCOCItem = RequestInsertCOCItem();
  List<AppoitmentAllHistoryResultList> appointHistoryList = List();

  Future sendCOCItem({String title, String details, String cOCTypeName, String attachment, AppoitmentAllHistoryResultList appointHistory}) async {
    hasError = false;
    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');

    _requestInsertCOCItem.attachment = attachment;
    _requestInsertCOCItem.title = title;
    _requestInsertCOCItem.details = details;
    _requestInsertCOCItem.cOCTypeName = cOCTypeName;
    _requestInsertCOCItem.formTypeID = cOCTypeName;
    _requestInsertCOCItem.mobileNo = "966" + Utils.getPhoneNumberWithoutZero(user.mobileNumber);
    _requestInsertCOCItem.isUserLoggedIn = true;
    _requestInsertCOCItem.projectID = user.projectID;
    _requestInsertCOCItem.patientName = user.firstName + " " + user.lastName;
    _requestInsertCOCItem.fileName = "";
    _requestInsertCOCItem.appVersion = VERSION_ID;
    _requestInsertCOCItem.uILanguage = languageID; //TODO Change it to be dynamic
    _requestInsertCOCItem.browserInfo = Platform.localHostname;
    _requestInsertCOCItem.deviceInfo = Platform.localHostname;
    _requestInsertCOCItem.resolution = "400x847";
    _requestInsertCOCItem.projectID = 0;
    _requestInsertCOCItem.tokenID = "C0c@@dm!n?T&A&A@Barcha202029582948";
    _requestInsertCOCItem.identificationNo = int.parse(user.patientIdentificationNo);
    if (BASE_URL.contains('uat')) {
      _requestInsertCOCItem.forDemo = true;
    } else {
      _requestInsertCOCItem.forDemo = false;
    }
    final Map<String, dynamic> body = _requestInsertCOCItem.toJson();

    if (appointHistory != null) {
      body['AppoinmentNo'] = appointHistory.appointmentNo;
      body['AppointmentDate'] = appointHistory.appointmentDate;
      body['ClinicID'] = appointHistory.clinicID;
      body['ClinicName'] = appointHistory.clinicName;
      body['DoctorID'] = appointHistory.doctorID;
      body['DoctorName'] = appointHistory.doctorNameObj;
      body['ProjectName'] = appointHistory.projectName;
    }

    await baseAppClient.post(SEND_FEEDBACK, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getStatusCOC() async {
    hasError = false;
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['IdentificationNo'] = user.patientIdentificationNo;
    body['MobileNo'] = "966" + Utils.getPhoneNumberWithoutZero(user.mobileNumber);
    body['Searching_type'] = 1;

    if (BASE_URL.contains('uat')) {
      body['ForDemo'] = true;
    }
    await baseAppClient.post(GET_STATUS_FOR_COCO, onSuccess: (dynamic response, int statusCode) {
      cOCItemList = [];
      response['ListCOCItems'].forEach((cOC) {
        cOCItemList.add(COCItem.fromJson(cOC));
      });
      cOCItemList = cOCItemList.reversed.toList(); // Sort list in desc order
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future searchFeedback(String countryCode, String phoneNumber, int type, String idNumber) async {
    hasError = false;
    Map<String, dynamic> body = new Map<String, dynamic>();
    if (type == 1) {
      body['IdentificationNo'] = 0;
      body['PatientID'] = num.tryParse(idNumber);
      body['ItemID'] = 0;
    } else if (type == 2) {
      body['IdentificationNo'] = idNumber;
      body['PatientID'] = 0;
      body['ItemID'] = 0;
    } else {
      body['IdentificationNo'] = 0;
      body['PatientID'] = 0;
      body['ItemID'] = idNumber;
    }

    body['PatientOutSA'] = countryCode.contains("966") ? 0 : 1;

    body['MobileNo'] = countryCode + Utils.getPhoneNumberWithoutZero(phoneNumber);
    body['Searching_type'] = type;
    if (BASE_URL.contains('uat')) {
      body['ForDemo'] = true;
    }
    await baseAppClient.post(GET_STATUS_FOR_COCO, onSuccess: (dynamic response, int statusCode) {
      cOCItemList = [];
      response['ListCOCItems'].forEach((cOC) {
        cOCItemList.add(COCItem.fromJson(cOC));
      });
      cOCItemList = cOCItemList.reversed.toList(); // Sort list in desc order
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getPatentAppointmentHistory() async {
    hasError = false;
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['IsComingFromCOC'] = true;
    baseAppClient.post(GET_PATIENT_AppointmentHistory, onSuccess: (dynamic response, int statusCode) {
      appointHistoryList = [];
      response['AppoimentAllHistoryResultList'].forEach((appoint) {
        appointHistoryList.add(AppoitmentAllHistoryResultList.fromJson(appoint));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }
}
