import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:hmg_patient_app/models/Authentication/authenticated_user.dart';
import 'package:hmg_patient_app/models/Request.dart';
import 'package:hmg_patient_app/models/SmartWatch/HealthData.dart';
import 'package:hmg_patient_app/services/authentication/auth_provider.dart';
import 'package:hmg_patient_app/uitl/app_shared_preferences.dart';
import 'package:flutter/material.dart';

class SmartWatchIntegrationService extends BaseService {
  AppSharedPreferences sharedPref = AppSharedPreferences();
  AppGlobal appGlobal = new AppGlobal();

  AuthenticatedUser authUser = new AuthenticatedUser();
  AuthProvider authProvider = new AuthProvider();

  Future<Map> getLastPatientRecord(BuildContext context) async {
    Map<String, dynamic> request;

    if (await this.sharedPref.getObject(USER_PROFILE) != null) {
      var data = AuthenticatedUser.fromJson(await this.sharedPref.getObject(USER_PROFILE));
      authUser = data;
    }

    Request req = appGlobal.getPublicRequest();

    request = {
      "IPAdress": "10.20.10.20",
      "VersionID": req.VersionID,
      "Channel": req.Channel,
      "generalid": 'Cs2020@2016\$2958',
      "PatientOutSA": authUser.outSA,
      "TokenID": "",
      "DeviceTypeID": req.DeviceTypeID,
      "SessionID": "YckwoXhUmWBsnHKEKig",
      "PatientID": authUser.patientID != null ? authUser.patientID : 0,
      "License": true
    };

    dynamic localRes;

    await baseAppClient.post(GET_PATIENT_LAST_RECORD, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> insertPatientHealthData(List<healthData> healthData, BuildContext context) async {
    Map<String, dynamic> request;
    if (await this.sharedPref.getObject(USER_PROFILE) != null) {
      var data = AuthenticatedUser.fromJson(await this.sharedPref.getObject(USER_PROFILE));
      authUser = data;
    }
    request = {
      "Med_InsertTransactionsInputsList": healthData,
    };
    dynamic localRes;
    await baseAppClient.post(INSERT_PATIENT_HEALTH_DATA, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }
}
