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/pages/feedback/appointment_history.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';

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

  Future sendCOCItem(
      {String title,
      String details,
      String cOCTypeName,
      String attachment,
      AppointmentHistory 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 = 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.identificationNo =
        int.parse(user.patientIdentificationNo);

    final Map<String, dynamic> body = _requestInsertCOCItem.toJson();

    if (appointHistory != null) {
      body['AppoinmentNo'] = appointHistory.appointmentNo;
      body['AppointmentDate'] =
          DateUtil.convertDateToString(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'] = user.mobileNumber;
    body['Searching_type'] = '1';

    await baseAppClient.post(GET_STATUS_FOR_COCO,
        onSuccess: (dynamic response, int statusCode) {
      cOCItemList = [];
      response['ListCOCItems'].forEach((cOC) {
        cOCItemList.add(COCItem.fromJson(cOC));
      });
    }, 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(AppointmentHistory.fromJson(appoint));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }
}
