package com.app.simplitend.patientprofile.medicalinfo.mvvm;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;

import com.app.simplitend.R;
import com.app.simplitend.patientprofile.ProfileContracts;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.RequestBody;

public class MedicalInfoViewModel extends ViewModel {

    private static final String TAG = "MedicalInfoViewModel";

    private final MedicalIntoRepository medicalIntoRepository;

    public MedicalInfoViewModel(){
        medicalIntoRepository = MedicalIntoRepository.getMedicalIntoRepository();
    }

    public void addNUpdateMedicalInfo(Map<String, RequestBody> body,
                                      int patient_id,
                                      String token,
                                      @NonNull ProfileContracts.AddMedicalIntoCallback medicalIntoCallback){
        medicalIntoRepository.addNUpdateMedicalInfo(body, patient_id, token, medicalIntoCallback);
    }

    public void getMedicalInfo(int patient_id,
                               String token,
                               @NonNull ProfileContracts.GetMedicationInfoCallback medicationInfoCallback){
        medicalIntoRepository.getMedicalInfo(patient_id, token, medicationInfoCallback);
    }

    public ArrayList<String> loadCountryCodeDropDown(Context context) {

        ArrayList<String> countryCodeList = new ArrayList<>();

        try {

            String countryCodeStr = readCountryCodes(context);
            JSONArray jsonArray = new JSONArray(countryCodeStr);

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject code = jsonArray.getJSONObject(i);

                countryCodeList.add(code.getString("dial_code"));

            }

        } catch (Exception e) {

            // if cannot load all country codes showing only india's dial code
            countryCodeList.add("+91");
            countryCodeList.add("+1");

            Log.e(TAG, "loadCountryCodeDropDown: ", e);
        }

        return countryCodeList;
    }

    public String readCountryCodes(Context context) throws IOException {
        StringBuilder returnString = new StringBuilder();

        InputStream fIn = context.getResources().openRawResource(R.raw.country_code);
        InputStreamReader isr = new InputStreamReader(fIn);
        BufferedReader input = new BufferedReader(isr);

        String line = "";

        while ((line = input.readLine()) != null) {
            returnString.append(line);
        }

        try {
            isr.close();

            if (fIn != null) fIn.close();

            input.close();

        } catch (Exception e) {
            Log.e(TAG, "readCountryCodes: ", e);
        }

        return returnString.toString();
    }

}
