import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/insert_user_activity_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_detail_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_detail_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_progress_for_month_data_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_progress_for_today_data_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_progress_for_week_data_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_progress_request_model.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';

class H2OService extends BaseService {
  List<UserProgressForTodayDataModel> userProgressForTodayDataList = List();
  List<UserProgressForWeekDataModel> userProgressForWeekDataList = List();
  List<UserProgressForMonthDataModel> userProgressForMonthDataList = List();
  UserProgressRequestModel userProgressRequestModel = UserProgressRequestModel();

  UserDetailModel userDetailModel = UserDetailModel();

  Future getUserDetail() async {
    userProgressRequestModel.progress = 1;
    userProgressRequestModel.mobileNumber = user.mobileNumber.substring(1);
    userProgressRequestModel.identificationNo = user.patientIdentificationNo;

    hasError = false;
    await baseAppClient.post(H2O_GET_USER_DETAIL, onSuccess: (dynamic response, int statusCode) {
      userDetailModel = UserDetailModel.fromJson(response["UserDetailData_New"]);
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: userProgressRequestModel.toJson());
  }

  Future updateUserDetail(UserDetailModel userDetail) async {
    userProgressRequestModel.progress = 1;
    userProgressRequestModel.mobileNumber = user.mobileNumber.substring(1);
    userProgressRequestModel.identificationNo = user.patientIdentificationNo;

    UserDetailRequestModel _requestModel = UserDetailRequestModel();

    _requestModel.activityID = userDetail.activityID.toString();
    _requestModel.channel = userProgressRequestModel.channel;
    _requestModel.dOB = userDetail.dOB;
    _requestModel.deviceTypeID = userProgressRequestModel.deviceTypeID;
    _requestModel.email = userDetail.emailID;
    _requestModel.firstName = userDetail.firstName;
    _requestModel.gender = userDetail.gender;
    _requestModel.height = userDetail.height;
    _requestModel.iPAdress = userProgressRequestModel.iPAdress;
    _requestModel.identificationNo = userProgressRequestModel.identificationNo;
    _requestModel.isHeightInCM = userDetail.isHeightInCM;
    _requestModel.isWeightInKG = userDetail.isWeightInKG;
    _requestModel.languageID = userProgressRequestModel.languageID;
    _requestModel.mobileNumber = userProgressRequestModel.mobileNumber;
    _requestModel.patientID = userProgressRequestModel.patientID;
    _requestModel.patientOutSA = userProgressRequestModel.patientOutSA;
    _requestModel.patientType = userProgressRequestModel.patientType;
    _requestModel.patientTypeID = userProgressRequestModel.patientOutSA;
    _requestModel.sessionID = userProgressRequestModel.sessionID;
    _requestModel.tokenID = userProgressRequestModel.tokenID;
    _requestModel.versionID = userProgressRequestModel.versionID;
    _requestModel.zipCode = userDetail.zipCode;
    _requestModel.weight = userDetail.weight;
    _requestModel.generalid = userProgressRequestModel.generalid;
    _requestModel.isDentalAllowedBackend = userProgressRequestModel.isDentalAllowedBackend;
    _requestModel.isNotificationOn = userDetail.isNotificationON;

    hasError = false;
    await baseAppClient.post(H2O_UPDATE_USER_DETAIL, onSuccess: (dynamic response, int statusCode) {
      userDetailModel = userDetail;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestModel.toJson());
  }

  Future getUserProgressForTodayData() async {
    userProgressRequestModel.progress = 1;
    userProgressRequestModel.mobileNumber = user.mobileNumber.substring(1);
    userProgressRequestModel.identificationNo = user.patientIdentificationNo;

    hasError = false;
    await baseAppClient.post(H2O_GET_USER_PROGRESS, onSuccess: (dynamic response, int statusCode) {
      userProgressForTodayDataList.clear();
      response['UserProgressForTodayData'].forEach((progressData) {
        userProgressForTodayDataList.add(UserProgressForTodayDataModel.fromJson(progressData));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: userProgressRequestModel.toJson());
  }

  Future getUserProgressForWeekData() async {
    userProgressRequestModel.progress = 2;
    userProgressRequestModel.mobileNumber = super.user.mobileNumber.substring(1);
    userProgressRequestModel.identificationNo = super.user.patientIdentificationNo;

    hasError = false;
    await baseAppClient.post(H2O_GET_USER_PROGRESS, onSuccess: (dynamic response, int statusCode) {
      userProgressForWeekDataList.clear();
      response['UserProgressForWeekData'].forEach((hospital) {
        userProgressForWeekDataList.add(UserProgressForWeekDataModel.fromJson(hospital));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: userProgressRequestModel.toJson());
  }

  Future getUserProgressForMonthData() async {
    userProgressRequestModel.progress = 3;
    userProgressRequestModel.mobileNumber = super.user.mobileNumber.substring(1);
    userProgressRequestModel.identificationNo = super.user.patientIdentificationNo;

    hasError = false;
    await baseAppClient.post(H2O_GET_USER_PROGRESS, onSuccess: (dynamic response, int statusCode) {
      userProgressForMonthDataList.clear();
      response['UserProgressForMonthData'].forEach((hospital) {
        userProgressForMonthDataList.add(UserProgressForMonthDataModel.fromJson(hospital));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: userProgressRequestModel.toJson());
  }

  Future insertUserActivity(InsertUserActivityRequestModel insertUserActivityRequestModel) async {
    hasError = false;
    await baseAppClient.post(H2O_INSERT_USER_ACTIVITY, onSuccess: (dynamic response, int statusCode) {
      userProgressForTodayDataList.clear();
      response['UserProgressForTodayData'].forEach((progressData) {
        userProgressForTodayDataList.add(UserProgressForTodayDataModel.fromJson(progressData));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: insertUserActivityRequestModel.toJson());
  }

  Future undoUserActivity() async {
    userProgressRequestModel.progress = 1;
    userProgressRequestModel.mobileNumber = user.mobileNumber.substring(1);
    userProgressRequestModel.identificationNo = user.patientIdentificationNo;

    hasError = false;
    await baseAppClient.post(H2O_UNDO_USER_ACTIVITY, onSuccess: (dynamic response, int statusCode) {
      userProgressForTodayDataList.clear();
      response['UserProgressForTodayData'].forEach((progressData) {
        userProgressForTodayDataList.add(UserProgressForTodayDataModel.fromJson(progressData));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: userProgressRequestModel.toJson());
  }
}
